Solved: Win32 API - sprintf() or is there a C++ way?

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

burnthepc

Thread Starter
Joined
Aug 12, 2007
Messages
305
Trying to learn a bit of Windows programming using the Win32 calls (ie not using MFC or anything fancy).

The book I'm using uses the C function sprintf to write to a buffer for use in Windows calls. I'm used to C++ Strings and was wondering if there's an easier way than the code below:

Code:
string message = "Screen size";

// other code that manipulates the string

	TCHAR cstr[ 1024 ];
	for(int i = 0; i < message.size(); ++i)
	{
		cstr[i] = message[i];
	}
	cstr[message.size()] = 0;

	MessageBox(NULL, cstr, TEXT("Screen size"), MB_OKCANCEL | MB_ICONINFORMATION);
The second param on message box won't seem to take message.c_str().

I could roll my own myString class to encompass a lot of the functionality I want. But I thought there must be an easier way to do it. How do you use the C++ strings class in the Windows C code?
 
Joined
Oct 19, 2007
Messages
177
The second param on message box won't seem to take message.c_str().
This is interesting, because I have always used c_str() and I have never had a problem.

This code compiles fine on my machine:
Code:
string test = "hello";					
MessageBox(hwnd, test.c_str(), "hello", 0);
What error message are you getting?
 

burnthepc

Thread Starter
Joined
Aug 12, 2007
Messages
305
The error for the above code is:

ello.cpp(30) : error C2664: 'MessageBoxW' : cannot convert parameter 2 from 'const char *' to 'LPCWSTR' Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

I figured this is because the code is compiling in Unicode and all text in the program is enclosed in TEXT("My c-string to be cast into Unicode")

There are different errors if I try to use the above macro, as in


MessageBox(hwnd, TEXT(message.c_str()), TEXT("hello"), 0);

hello.cpp(30) : error C2065: 'Lmessage' : undeclared identifier
hello.cpp(30) : error C2228: left of '.c_str' must have class/struct/union type is ''unknown-type''


This is because the macro puts a L before the text string to allow casting to Unicode. This then creates the Lmessage identifier and that's not the same as the message string identifyer. Therefore Lmessage is not a defined type.

I'm using Visual Studio 2008, C++ express edition as the IDE, the book telling me to do it this way is "Programming Windows, 5th Edition, by Charles Petzold"
 
Joined
Apr 30, 2001
Messages
2,636
These work with mingw:

Code:
#include <windows.h>
#include <string>
using namespace std;

int main() {
    const wstring message(L"Screen size");
    MessageBox(NULL, message.c_str(), message.c_str(), MB_OKCANCEL | MB_ICONINFORMATION);
}

// g++ -Wall -Wextra messagebox.cc -o messagebox -O3 -s -mwindows -DUNICODE
Code:
#include <windows.h>
#include <string>
using namespace std;

int main() {
    const string message("Screen size");
    MessageBox(NULL, message.c_str(), message.c_str(), MB_OKCANCEL | MB_ICONINFORMATION);
}

// g++ -Wall -Wextra messagebox2.cc -o messagebox2 -O3 -s -mwindows
 

burnthepc

Thread Starter
Joined
Aug 12, 2007
Messages
305
wstring message(L"Screen size")

That's what I need.

I didn't realise there were two instantiation of the string class. string (8bit ascii) and wstring (16bit Unicode).

Thanks for the help guys :)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 807,865 other people just like you!

Latest posts

Staff online

Top