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

问题描述

  //包括基本的窗口头文件
#include< windows.h>
#include< windowsx.h>

// WindowProc函数原型LRESULT CALLBACK WindowProc(HWND hWnd,
UINT message,
WPARAM wParam,
LPARAM lParam);

//任何Windows程序的入口点int WINAPI WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow){
//窗口的句柄,由函数
HWND hWnd填充;
//这个struct保存窗口类的信息
WNDCLASSEX wc;

//清除窗口类使用
ZeroMemory(& wc,sizeof(WNDCLASSEX));

//用所需的信息填充结构
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 = LWindowClass1;

//注册窗口类
RegisterClassEx(& wc);

//创建窗口并使用结果作为句柄
hWnd = CreateWindowEx(NULL,
LWindowClass1,//窗口类名称
L我们的第一个窗口程序,//窗口的标题
WS_OVERLAPPEDWINDOW,//窗口样式
300,// x位置窗口
300,// y位置窗口
500,//窗口的宽度
400,//窗口的高度
NULL,//我们没有父窗口,NULL
NULL,//我们没有使用菜单,NULL
hInstance,//应用程序句柄
NULL); //用于多个窗口,NULL

//在屏幕上显示窗口
ShowWindow(hWnd,nCmdShow);

//输入主循环:

//此结构保存Windows事件消息
MSG msg;

//等待队列中的下一条消息,将结果存储在'msg'中
while(GetMessage(& msg,NULL,0,0))
{
//将击键消息转换成正确的格式
TranslateMessage(& msg);

//发送消息到WindowProc函数
DispatchMessage(& msg);
}

//将WM_QUIT消息的这部分返回到Windows
return msg.wParam; }

//这是程序的主要消息处理程序LRESULT CALLBACK WindowProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam){
//排序,找到什么代码运行为给出的消息
switch(message)
{
//当窗口关闭时读取此消息
case WM_DESTROY:
{
/ /完全关闭应用程序
PostQuitMessage(0);
return 0;
} break;
}

//处理switch语句没有的任何消息
return DefWindowProc(hWnd,message,wParam,lParam); }

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



我遇到以下错误:

  ||在函数'int WinMain(HINSTANCE,HINSTANCE,LPSTR,int)': 
错误:不能在赋值语句中将'const wchar_t [13]'转换为'LPCSTR {aka const char *}'
| 49 | warning:从NULL [-Wconversion-null]转换为非指针类型DWORD {aka long unsigned int}|
| 49 |错误:不能将'const wchar_t *'转换为'LPCSTR {aka const char *}'为参数'2'到'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 * / code>。更改

  LWindowClass1

 WindowClass1

对剩余的字符串执行相同操作。或者,将它们更改为 _T(WindowClass1),这将扩展为基于 UNICODE


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



编辑:

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


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

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 ===|
解决方案

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.

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

08-22 22:02