Nick
Nick

Reputation: 19664

Wrapping my head around windows.h - const char* vs LPSTR

I am getting started with the following simple messagebox application. The problem is that when I run this application the text is Chinese. I clearly have an encoding issue. Can someone point me to somewhere I can learn about windows.h specific string typedefs?

//test.c

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, "Hello World", "Note", MB_OK);
    return 0;
}

Upvotes: 0

Views: 1264

Answers (3)

engf-010
engf-010

Reputation: 3929

When using the windows API (almost) every declaration with null-terminated strings uses TCHAR ,LPTSTR and LPCTSTR which are define as char ,char* and const char* when you're not building with UNICODE as character-set and are defined as wchar_t ,wchar_t* and const wchar_t* when you are building with the UNICODE character-set.

Also the UNICODE and/or _UNICODE definition(s) controll which function is being compiled when you call an windows-API function. Almost every function has two versions ,one for UNICODE and one for non-UNICODE.

for instance MessageBox is either translated to MessageBoxA (non-UNOCODE version) or MessageBoxW (UNOCDE-version).

Further more :

int WINAPI WinMain ( HINSTANCE hInstance ,HINSTANCE hPrevInstance ,LPSTR lpCmdLine, int nCmdShow ) 
{
// ...
}

is often defined as :

int WINAPI _tWinMain ( HINSTANCE hInstance ,HINSTANCE hPrevInstance ,LPTSTR lpCmdLine ,int nCmdShow ) 
{
// ...
}

for the last version you need to include <tchar.h> which has the right translation for _tWinMain (or _tmain when building consle-app).

Hope this clarefies things for you.

Upvotes: 3

Jeff Paquette
Jeff Paquette

Reputation: 7127

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, _T("Hello World"), _T("Note"), MB_OK);
    return 0;
}

The issue is most likely that you are building a UNICODE application and are passing pointers to char strings when the API wants pointers to wchar_t strings

Upvotes: 0

Laserallan
Laserallan

Reputation: 11312

It's most likely a wide char vs ordinary char issue. Try changing the code to:

#include <windows.h>

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
    LPSTR lpCmdLine, int nCmdShow)
{
    MessageBox(NULL, L"Hello World", L"Note", MB_OK);
    return 0;
}

If this solves your problem it means your project is configured for wide characters rather than ordinary chars. Adding an L in front of a string constant will make it a wide char constant. This is generally a good thing since it's way easier to manage internationalization for a wide char application.

These web pages covers the Windows API and unicode in more detail:

http://msdn.microsoft.com/en-us/library/ff381407(v=vs.85).aspx

http://msdn.microsoft.com/en-us/library/dd374089(v=vs.85).aspx

Upvotes: 0

Related Questions