LoadString Fails.

Jan 16, 2012 at 6:54am
I'm learning to add resources to my program

Cursor...OK
Icon...OK
Menu...OK

Now I failed as string tables.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <Windows.h>
#include "resource.h"

LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hprevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	char AddCaption[40];
	MSG Msg;
	HWND HWnd;
	WNDCLASSEX WndClsEx;

	LPCTSTR ClsName = L"BasicApp";
	LPCTSTR WndName = L"Cafeteria Shooting";

	LoadString(hInstance,IDS_APP_NAME,LPWSTR(AddCaption), 40);

	WndClsEx.cbSize = sizeof(WNDCLASSEX);
	WndClsEx.style = CS_HREDRAW || CS_VREDRAW;
	WndClsEx.cbClsExtra = 0;
	WndClsEx.cbWndExtra = 0;
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.lpszMenuName = MAKEINTRESOURCE(IDR_MAINFRAME);
	WndClsEx.lpfnWndProc = WinProc;
	WndClsEx.hCursor = LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
	WndClsEx.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	WndClsEx.hIconSm = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClsEx.hInstance = hInstance;
	

	RegisterClassEx(&WndClsEx);
	
	HWnd = CreateWindow(ClsName,LPWSTR(AddCaption),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

	if(!HWnd)
		return 0;

	ShowWindow(HWnd,SW_SHOWNORMAL);
	UpdateWindow(HWnd);

	while(GetMessage(&Msg,NULL,0,0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(WM_QUIT);
		break;
	default:
		return DefWindowProc(hWnd,uMsg,wParam,lParam);
	}

	return 0;
}


Line 16 it keeps saying IDS_APP_NAME is undefined.

But I compiled and the program's title had that string,but when I closed the program I got

Run-Time Check Failure #2 - Stack around the variable 'AddCaption' was corrupted.


Plz help.Thanks.
Jan 16, 2012 at 7:13am
You are using the char data type and then you typecast as LPWSTR, but that's not all: You also tell the LoadString() function you have 40 characters of space available! But in fact you only have 20.

So basically what you are doing is very wrong. A tip: If you have to typecast, most likely you are doing it wrong. Not 100% of the time ,but it is a good rule of thumb.

So the quick fix for you would be to change AddCaption from type char to type wchar_t.

But in reality you should re-read the MSDN Online documentation and note a peculiarity about the Unicode version of this function. You should take advantage of this peculiarity and code something like the LoadStringFromResource() function found here: http://msmvps.com/blogs/gdicanio/archive/2010/01/05/stl-strings-loading-from-resources.aspx .

But that's really not all. You will keep on bumping into problems because you seem to be unaware of how Windows.h functions in regards to ANSI/Unicode. So you should really read my response here: http://www.cplusplus.com/forum/general/56526/ .

Oh, and always ALWAYS check the return value of functions to make sure they are succeeding.
Last edited on Jan 16, 2012 at 7:13am
Jan 16, 2012 at 12:30pm
I see WindowAPI got a bunch of capitalized abbreviations so it would be disaster to know the meaning of them surfing many pages just to find out they're just typedefs of something.I need to find the full table of all the "window jargon" like LPCTSTR really,so hard...
Jan 16, 2012 at 1:05pm
I don't know which IDE you use, but in Visual studio you can trace this back easily.

In my setup I have the following in WinNT.h (automatically included with Windows.h)
typedef LPCWSTR PCTSTR, LPCTSTR;
Depending on your setup you may have this instead:
typedef LPCSTR PCTSTR, LPCTSTR, PCUTSTR, LPCUTSTR;

If I look up LPSWSTR I see:
typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR;

If I look up WCHAR I see:
typedef wchar_t WCHAR; // wc, 16-bit UNICODE character

So...
LPCTSTR is the same thing as wchar_t* if you have unicode configured
LPCTSTR is the same thing as char* if you have ANSI configured.
Jan 16, 2012 at 2:37pm
If you use Visual Studio then install VisualAssistX and you will get the definitions as tooltips. I think it works without VisualAssistX too, I have not tried ...
Jan 16, 2012 at 6:31pm
I've read the post,many thanks for that.Now I know about what's behind MS typedefs things and data types.Thanks to your post.And you.

Well,I use a non -A or -W function,and I used TChar please check if anything uncorrect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <Windows.h>
#include "resource.h"

LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam);

INT WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hprevInstance,LPSTR lpCmdLine,int nShowCmd)
{
	TCHAR AddCaption[40];
	MSG Msg;
	HWND HWnd;
	WNDCLASSEX WndClsEx;

	LPCTSTR ClsName = L"BasicApp";
	LPCTSTR WndName = L"Cafeteria Shooting";

	LoadString(hInstance,IDS_APP_NAME,AddCaption,40);

	WndClsEx.cbSize = sizeof(WNDCLASSEX);
	WndClsEx.style = CS_HREDRAW || CS_VREDRAW;
	WndClsEx.cbClsExtra = 0;
	WndClsEx.cbWndExtra = 0;
	WndClsEx.lpszClassName = ClsName;
	WndClsEx.lpszMenuName = MAKEINTRESOURCE(IDR_MAINFRAME);
	WndClsEx.lpfnWndProc = WinProc;
	WndClsEx.hCursor = LoadCursor(hInstance,MAKEINTRESOURCE(IDC_CURSOR1));
	WndClsEx.hIcon = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	WndClsEx.hIconSm = LoadIcon(hInstance,MAKEINTRESOURCE(IDI_ICON1));
	WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
	WndClsEx.hInstance = hInstance;
	

	RegisterClassEx(&WndClsEx);
	
	HWnd = CreateWindow(ClsName,AddCaption,WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,NULL,NULL,hInstance,NULL);

	if(!HWnd)
		return 0;

	ShowWindow(HWnd,SW_SHOWNORMAL);
	UpdateWindow(HWnd);

	while(GetMessage(&Msg,NULL,0,0))
	{
		TranslateMessage(&Msg);
		DispatchMessage(&Msg);
	}

	return Msg.wParam;
}

LRESULT CALLBACK WinProc(HWND hWnd,UINT uMsg,WPARAM wParam,LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_DESTROY:
		PostQuitMessage(WM_QUIT);
		break;
	default:
		return DefWindowProc(hWnd,uMsg,wParam,lParam);
	}

	return 0;
}
Jan 16, 2012 at 6:38pm
Line 13: Type is LPCTSTR, which is a TCHAR-derived type. You assign a wide char string (L-prepended). Incorrect. You mixed data types. it needs to be:

LPCTSTR ClsName = TEXT("BasicApp");

Line 14: Same as line 13.

I think that's it.
Jan 16, 2012 at 6:52pm
Line 13 and 14 ? OK.

Cuz I thought TCHAR is set to Unicode by default.But it's a bad habit though.Fixed.Thread closed.
Topic archived. No new replies allowed.