Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
537 views
in Technique[技术] by (71.8m points)

winapi - Win32 Application Programming C++, Error with parameter Type LPCWSTR

I am constructing a program using the Win32 Application on Visual Studio 2013. From the tutorials I have read, I know this following code is correct, but I don't know where to change the parameter type to read the following:

case WM_CREATE:
{
    hEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "Edit", "",
        WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL |
        ES_AUTOVSCROLL, 50, 100, 300, 300, hWnd, (HMENU)IDC_EDIT_BOX, 
        NULL, NULL);

The compiler highlights "Edit" and gives me this error:

Error 1 error C2664: 'HWND CreateWindowExW(DWORD,LPCWSTR,LPCWSTR,DWORD,int,int,int,int,HWND,HMENU,HINSTANCE,LPVOID)' : cannot convert argument 2 from 'const char [5]' to 'LPCWSTR'

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are compiling with UNICODE defined. That means that CreateWindowEx is an alias for CreateWindowExW, the wide character version. Either:

  1. Supply wide character text, or
  2. Call CreateWindowExA, or
  3. Compile for ANSI.

Personally I would suggest option 1. Your code would become:

CreateWindowEx(WS_EX_CLIENTEDGE, L"Edit", L"",
  WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOHSCROLL | ES_AUTOVSCROLL, 
  50, 100, 300, 300, hWnd, (HMENU)IDC_EDIT_BOX, NULL, NULL);

The L prefix to your string literals is used to specify a wide character literal.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...