【问题标题】:error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment错误:无法在赋值中将 'const wchar_t [13]' 转换为 'LPCSTR {aka const char*}'
【发布时间】:2012-12-20 17:24:36
【问题描述】:
// include the basic windows header file
#include <windows.h>
#include <windowsx.h>

// the WindowProc function prototype LRESULT CALLBACK WindowProc(HWND hWnd,
                         UINT message,
                         WPARAM wParam,
                         LPARAM lParam);

// the entry point for any Windows program int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow) {
    // the handle for the window, filled by a function
    HWND hWnd;
    // this struct holds information for the window class
    WNDCLASSEX wc;

    // clear out the window class for use
    ZeroMemory(&wc, sizeof(WNDCLASSEX));

    // fill in the struct with the needed information
    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = (HBRUSH)COLOR_WINDOW;
    wc.lpszClassName = L"WindowClass1";

    // register the window class
    RegisterClassEx(&wc);

    // create the window and use the result as the handle
    hWnd = CreateWindowEx(NULL,
                          L"WindowClass1",    // name of the window class
                          L"Our First Windowed Program",   // title of the window
                          WS_OVERLAPPEDWINDOW,    // window style
                          300,    // x-position of the window
                          300,    // y-position of the window
                          500,    // width of the window
                          400,    // height of the window
                          NULL,    // we have no parent window, NULL
                          NULL,    // we aren't using menus, NULL
                          hInstance,    // application handle
                          NULL);    // used with multiple windows, NULL

    // display the window on the screen
    ShowWindow(hWnd, nCmdShow);

    // enter the main loop:

    // this struct holds Windows event messages
    MSG msg;

    // wait for the next message in the queue, store the result in 'msg'
    while(GetMessage(&msg, NULL, 0, 0))
    {
        // translate keystroke messages into the right format
        TranslateMessage(&msg);

        // send the message to the WindowProc function
        DispatchMessage(&msg);
    }

    // return this part of the WM_QUIT message to Windows
    return msg.wParam; }

// this is the main message handler for the program LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {
    // sort through and find what code to run for the message given
    switch(message)
    {
        // this message is read when the window is closed
        case WM_DESTROY:
            {
                // close the application entirely
                PostQuitMessage(0);
                return 0;
            } break;
    }

    // Handle any messages the switch statement didn't
    return DefWindowProc (hWnd, message, wParam, lParam); }

=============== 我使用 CodeBlock,此代码来自 Direct X 教程。

我收到以下错误:

||In function 'int WinMain(HINSTANCE, HINSTANCE, LPSTR, int)':|
error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment|
|49|warning: converting to non-pointer type 'DWORD {aka long unsigned int}' from NULL [-Wconversion-null]|
|49|error: cannot convert 'const wchar_t*' to 'LPCSTR {aka const char*}' for argument '2' to 'HWND__* CreateWindowExA(DWORD, LPCSTR, LPCSTR, DWORD, int, int, int, int, HWND, HMENU, HINSTANCE, LPVOID)'|
||=== Build finished: 2 errors, 1 warnings ===|

【问题讨论】:

  • 您是在为 Unicode 构建吗?你不应该改用LPCTSTR 吗?
  • 什么意思?我用 L 作为类名的前缀,以便它使用 ANSI 而不是 unicode。
  • L 表示宽字符字符串。

标签: c++ winapi compiler-errors


【解决方案1】:

您的项目没有定义 UNICODE 预处理器符号,因此采用指向字符串的指针的 Windows API 函数需要 char * 而不是 wchar_t *。改变

L"WindowClass1"

"WindowClass1"

对剩余的字符串文字执行相同的操作。或者,将它们更改为 _T("WindowClass1"),这将根据正在定义的 UNICODE 符号扩展为正确类型的字符串文字。


我的建议是转到您的项目属性并将Character Set 设置更改为Unicode,然后明确使用所有Windows API 函数的宽字符版本。例如,不要调用CreateWindow,而是调用CreateWindowW

编辑:
我建议的项目设置只适用于 Visual Studio,不知道如何在 Code::Blocks 中做到这一点。

【讨论】:

  • 显式调用 A 和 W 函数不是一个好主意,最好使用 tchar.h、_T 宏和不带 A 或 W 的函数名。这样更容易在编译或不编译之间切换UNICODE。
  • @user93353 除非您需要支持 Windows 95,否则我认为没有理由在两者之间切换。很有可能,如果您尝试同时支持两者,那么无论如何您都会在某个地方搞砸;恕我直言,这不值得麻烦。我曾经和你有同样的看法,直到我意识到在我编写的所有 Windows 软件中,甚至没有一个实例需要切换 Unicode 设置。
  • @user93353:我同意Praetorian。有一些新的 Windows API 在 ANSI 版本中甚至可用,因此最好始终使用 Unicode。
  • 在 Visual Studio 中,我运行了代码并在用 unicode 编译后得到了这个。该程序确实运行正常。 “game.exe”:已加载“C:\Users\me\Documents\Visual Studio 2010\Projects\Debug\game.exe”,已加载符号。 “game.exe”:已加载“C:\Windows\SysWOW64\ntdll.dll”,无法找到或打开 PDB 文件“game.exe”:已加载“C:\Windows\SysWOW64\kernel32.dll”,找不到或打开 PDB 文件 'game.exe': Loaded 'C:\Program Files\BitDefender\Bitdefender 2013\active virus control\Avc3_00172_012\avcuf32.dll'
  • @GladstoneAsder 这些都不是错误消息; cannot find PDB 消息只是告诉您未找到程序的调试信息,但正确运行不需要这些东西。您的程序是否按预期运行?
【解决方案2】:

1) 如果您想使用 UNICODE 进行编译,请更改选项。如果您从 IDE 编译,请设置以下属性 配置属性 -> 常规 -> 项目默认值 -> 字符集 -> 使用 Unicode 字符集。

如果从命令行编译使用选项 /DUNICODE /D_UNICODE

如果您不想使用 UNICODE 进行编译,只需执行以下步骤 2 和 3。在 Char Set 中,不要选择 UNICODE。

2) 之前

#include <windows.h>

添加

#include <tchar.h>

3) 改变

wc.lpszClassName = L"WindowClass1";

wc.lpszClassName = _T("WindowClass1");

如果你想用 UNICODE 编译,你可以只做 #1,但最好做所有 3。

如果您想在没有 UNICODE 的情况下进行编译,请执行 #2 和 #3 - 不要执行 #1。

【讨论】:

    【解决方案3】:

    我在我的单一源代码文件中使用它,因为我不想更改我当前的项目配置

    #ifndef UNICODE
    #define UNICODE
    #define UNICODE_WAS_UNDEFINED
    #endif
    
    #include <Windows.h>
    
    #ifdef UNICODE_WAS_UNDEFINED
    #undef UNICODE
    #endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-31
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 2015-08-05
      • 1970-01-01
      • 2020-01-23
      • 2013-08-11
      相关资源
      最近更新 更多