error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment

前端 未结 3 1476
无人共我
无人共我 2020-11-29 09:15
// include the basic windows header file
#include 
#include 

// the WindowProc function prototype LRESULT CALLBACK WindowProc(HWN         


        
相关标签:
3条回答
  • 2020-11-29 09:51

    I use this on my single source code file because I don't want to change my current project configuration

    #ifndef UNICODE
    #define UNICODE
    #define UNICODE_WAS_UNDEFINED
    #endif
    
    #include <Windows.h>
    
    #ifdef UNICODE_WAS_UNDEFINED
    #undef UNICODE
    #endif
    
    0 讨论(0)
  • 2020-11-29 09:54

    1) If you want to compile with UNICODE, then change the options. If you are compiling from IDE, the set the following propery Configuration Properties -> General -> Project Defaults -> Character Set -> Use Unicode Character Set.

    If compiling from command line use options /DUNICODE /D_UNICODE

    If you don't want to compile with UNICODE, just follow steps 2 & 3 below. In Char Set, do not chose UNICODE.

    2) Before

    #include <windows.h>
    

    add

    #include <tchar.h>
    

    3) Change

    wc.lpszClassName = L"WindowClass1";
    

    to

    wc.lpszClassName = _T("WindowClass1");
    

    If you want to compile with UNICODE, you could get by just by doing #1, but best to do all 3.

    If you want to compile without UNICODE, do #2 & #3 - don't do #1.

    0 讨论(0)
  • 2020-11-29 10:18

    Your project doesn't have the UNICODE preprocessor symbol defined, so Windows API functions that take pointers to strings expect char * and not wchar_t *. Change

    L"WindowClass1"
    

    to

    "WindowClass1"
    

    Do the same for the remaining string literals. Alternatively, change them to _T("WindowClass1"), this will expand to the correct type of string literal based on the UNICODE symbol being defined.


    My recommendation is to go to your project properties and change the Character Set setting to Unicode, and then use the wide char versions of all Windows API functions explicitly. For example, instead of CreateWindow, call CreateWindowW.

    EDIT:
    The project setting I suggested only applies to Visual Studio, not sure how to do that in Code::Blocks.

    0 讨论(0)
提交回复
热议问题