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



## burnthepc (Aug 12, 2007)

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:


```
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?


----------



## jpshortstuff (Oct 19, 2007)

burnthepc said:


> 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:

```
string test = "hello";					
MessageBox(hwnd, test.c_str(), "hello", 0);
```
What error message are you getting?


----------



## burnthepc (Aug 12, 2007)

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"


----------



## Shadow2531 (Apr 30, 2001)

These work with mingw:


```
#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
```


```
#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 (Aug 12, 2007)

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


----------

