Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
539 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...