错误:无法在赋值中将“const wchar_t [13]"转换为“LPCSTR {aka const char*}" [英] error: cannot convert 'const wchar_t [13]' to 'LPCSTR {aka const char*}' in assignment

查看:32
本文介绍了错误:无法在赋值中将“const wchar_t [13]"转换为“LPCSTR {aka const char*}"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

// 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 教程.

=============== I use CodeBlock, this code is from a Direct X tutorial.

我收到以下错误:

||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 预处理器符号,所以 Windows API 函数接受字符串指针需要 char* 而不是 wchar_t *.改变

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"

"WindowClass1"

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

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.

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

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.


我建议的项目设置仅适用于 Visual Studio,不确定如何在 Code::Blocks 中执行此操作.


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

这篇关于错误:无法在赋值中将“const wchar_t [13]"转换为“LPCSTR {aka const char*}"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆