Conversion from char[40] to LPWSTR

Hello there...
Here is a program I picked from a website which provides tutorials for Win32 Programming... I am totally new to Win32 Programming... This program is about using resource lists, and this particular example about using a string table from resources. When I compile this program in Visual Studio 2005 it returns the following error...

error C2664: 'LoadStringW' : cannot convert parameter 3 from 'char [40]' to 'LPWSTR'

FIRST OF ALL: What is 'LPWSTR'
SECONDLY: What is this error about? If all the tutorials from this website I am using run accurately, why not this one?
AND MAINLY: What is the solution of such an error? I shall send the program in the post below.
// Here is the program.




//---------------------------------------------------------------------------
#include <windows.h>
#include "resource.h"

//---------------------------------------------------------------------------
char AppCaption[40];
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;
WNDCLASSEX WndClsEx;
LPCTSTR ClsName = L"ResFund";

LoadString(hInstance, IDS_APP_NAME, AppCaption, 40);

// Create the application window
WndClsEx.cbSize = sizeof(WNDCLASSEX);
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(hInstance,
MAKEINTRESOURCE(IDI_RESFUND2));
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszMenuName = MAKEINTRESOURCE(IDR_MAINFRAME);
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;
WndClsEx.hIconSm = LoadIcon(hInstance,
MAKEINTRESOURCE(IDI_RESFUND2));

// Register the application
RegisterClassEx(&WndClsEx);

// Create the window object
hWnd = CreateWindowEx(0,
ClsName,
AppCaption,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

// Find out if the window was created
if( !hWnd ) // If the window was not created,
return FALSE; // stop the application

// Display the window to the user
ShowWindow(hWnd, nCmdShow);// SW_SHOWNORMAL);
UpdateWindow(hWnd);

// Decode and treat the messages
// as long as the application is running
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

// return Msg.wParam;
return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
// Process the left-over messages
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
// If something was not done, let it go
return 0;
}
//---------------------------------------------------------------------------
I shall be greatly obliged señor if anyone can solve my problem. Thanks a lot.
Your code is easier to read when you use [ code][ /code] tags.

Here is your program: I changed a few lines and removed the resource.h dependency.
LPCTSTR means nothing else but "long pointer to c string" which in turn is an obscure way of saying char*. For some reason your compiler is not smart enough to convert the const char* to LPCTSTR .


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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
// Here is the program.
//---------------------------------------------------------------------------
#include <windows.h>
//---------------------------------------------------------------------------
char AppCaption[40];
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT uMsg,
WPARAM wParam, LPARAM lParam);
//---------------------------------------------------------------------------
INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
HWND hWnd;

LPCTSTR ClsName =(LPCTSTR) "ResFund";

// Create the application window
WNDCLASSEX WndClsEx ={sizeof(WndClsEx)};// set first member, zero the rest;
WndClsEx.style = CS_HREDRAW | CS_VREDRAW;
WndClsEx.lpfnWndProc = WndProcedure;
WndClsEx.cbClsExtra = 0;
WndClsEx.cbWndExtra = 0;
WndClsEx.hIcon = LoadIcon(hInstance,0);
WndClsEx.hCursor = LoadCursor(NULL, IDC_ARROW);
WndClsEx.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
WndClsEx.lpszClassName = ClsName;
WndClsEx.hInstance = hInstance;

// Register the application
RegisterClassEx(&WndClsEx);

// Create the window object
hWnd = CreateWindowEx(0,
ClsName,
AppCaption,
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);

// Find out if the window was created
if( !hWnd ) // If the window was not created,
return FALSE; // stop the application

// Display the window to the user
ShowWindow(hWnd, nCmdShow);// SW_SHOWNORMAL);
UpdateWindow(hWnd);

// Decode and treat the messages
// as long as the application is running
while( GetMessage(&Msg, NULL, 0, 0) )
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}

// return Msg.wParam;
return 0;
}
//---------------------------------------------------------------------------
LRESULT CALLBACK WndProcedure(HWND hWnd, UINT Msg,
WPARAM wParam, LPARAM lParam)
{
switch(Msg)
{
case WM_DESTROY:
PostQuitMessage(WM_QUIT);
break;
default:
// Process the left-over messages
return DefWindowProc(hWnd, Msg, wParam, lParam);
}
// If something was not done, let it go
return 0;
}
//--------------------------------------------------------------------------- 


The solution to such an error 99% of the time is to put
(LPWSTR) x; in front of whatever the compiler fails to convert to LPWSTR
Last edited on
@tition

Oh I am so sorry! I am absolutely new to this forum and I am a dumb not to grasp small things like this... Next time I shall format my program as you directed... Many many thanks for correcting me.

P.S. If you could explain the problem exactly I shall be very thankful as I wish to understand the program thoroughly which unfortunately is not possible working only with tutorials... Thanks a lot.
I am brand new to windows programming as well (and a beginner overall), so take my words with a critical mind.

If you are using Visual studio, just hover the mouse over LPWSTR inside the studio. You will see a hint: "typedef WCHAR* LPWSTR " which in turn means: LPWSTR is the same as WCHAR*. Now, if you want to find out what WCHAR is, declare a WCHAR variable and hover the mouse over it, and so on. In the end you will go down to the root of it all.
Last edited on
@tition

No no! I mean that the edited program you presented, how and where have you repaired it, and with what logic actually... Just that... Thanks.

P.S. By the way, the program was about resource.h usage, and you have manipulated the program to be independent of resource.h. Isn't this simply possible to remove the error while using the resource file as well...?
Thanks a lot.
u have to then post the resource.h file so I can compile the program and see the error!

[Edit]: Based on your post, you should try editing this line:
LoadString(hInstance, IDS_APP_NAME, AppCaption, 40);
->change it to
LoadString(hInstance, IDS_APP_NAME, (LPWSTR)AppCaption, 40);
Last edited on
Oh K! But the resource file is not just text... It includes icons and menu-styles as well... If you could possibly provide me your e-mail address, I can send you the ZIP file of the project immediately... But for that I have to know which VS can you use to compile my program? Because I am compiling this project in Visual Studio 2005.
Just read your EDIT... I shall try. Thank you...
I would advise you to install the newest version of Visual Studio (2008).

Sorry mate, don't ask me for personal information... Anyways, I think you should be able to figure it on your own: just use (LPWSTR) for explicit type casting. If you don't know yet what explicit type casting is, try http://www.cplusplus.com/doc/tutorial/typecasting/ .
@tition

You are just great señor! It is working... Full fledge! Thanks a lot!
Last edited on
@tition
Thanks a lot... And sorry! I wasn't aware that I'm asking for personal information... Anyways! thanks a lot once again... My greatest problem of today has been solved... And you are the hero. ;)
I would recommend using the TEXT() macro over casts. This makes your code compile properly using the (char*) versions for non-unicode and the (wchar_t*) versions for UNICODE, and compiles the string literals appropriately.
 
LPCTSTR ClsName = TEXT( "ResFund" );

Alas.
Topic archived. No new replies allowed.