错误:不能将“const wchar_t[13]”转换为赋值中的“LPCSTR {aka const char*}”。

时间:2022-02-26 05:52:46
// 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); }

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

========我使用了CodeBlock,这个代码来自于一个直接的X教程。

I get the following errors:

我有以下错误:

||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 ===|

3 个解决方案

#1


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

您的项目没有定义UNICODE的预处理器符号,所以Windows API函数使用指向字符串的指针,而不是wchar_t *。改变

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.

对剩下的字符串进行相同的处理。或者,将它们改为_T(“WindowClass1”),它将根据定义的UNICODE符号扩展到正确的字符串文本类型。


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.

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

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

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

#2


4  

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.

1)如果你想用UNICODE编译,那就改变选项。如果您正在从IDE编译,则设置如下的propery配置属性-> General ->项目默认->字符集->使用Unicode字符集。

If compiling from command line use options /DUNICODE /D_UNICODE

如果从命令行编译,使用选项/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.

如果您不想使用UNICODE进行编译,请遵循下面的步骤2和步骤3。在Char集中,不要选择UNICODE。

2) Before

2)之前

#include <windows.h>

add

添加

#include <tchar.h>

3) Change

3)改变

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.

如果你想用UNICODE编译,你可以通过#1来获得,但是最好是全部3。

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

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

#3


1  

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

#1


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

您的项目没有定义UNICODE的预处理器符号,所以Windows API函数使用指向字符串的指针,而不是wchar_t *。改变

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.

对剩下的字符串进行相同的处理。或者,将它们改为_T(“WindowClass1”),它将根据定义的UNICODE符号扩展到正确的字符串文本类型。


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.

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

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

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

#2


4  

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.

1)如果你想用UNICODE编译,那就改变选项。如果您正在从IDE编译,则设置如下的propery配置属性-> General ->项目默认->字符集->使用Unicode字符集。

If compiling from command line use options /DUNICODE /D_UNICODE

如果从命令行编译,使用选项/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.

如果您不想使用UNICODE进行编译,请遵循下面的步骤2和步骤3。在Char集中,不要选择UNICODE。

2) Before

2)之前

#include <windows.h>

add

添加

#include <tchar.h>

3) Change

3)改变

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.

如果你想用UNICODE编译,你可以通过#1来获得,但是最好是全部3。

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

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

#3


1  

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