【问题标题】:2 IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"2 IntelliSense:“const char *”类型的参数与“LPCWSTR”类型的参数不兼容
【发布时间】:2015-06-12 23:54:35
【问题描述】:

Stackflow 成员 ---- 我是 C++ WINAPI 的初学者 --- 当我在我的第一个 WIN32 GUI 中编码时,出现了一些错误 [OUTPUT] --- 有没有推荐的 C++ 高级书籍 ---我的代码:

#include <windows.h>
const char g_szClassName[] = ("myWindowClass");
 LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { 
   switch(msg)    {    
   case WM_CLOSE:     
   DestroyWindow(hwnd);    
   break;     
   case WM_DESTROY:  
   PostQuitMessage(0);  
   break;     
   default:    
   return DefWindowProc(hwnd, msg, wParam, lParam);  
   }  
   return 0; 
   }
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,  
  LPSTR lpCmdLine, int nCmdShow) {  
  WNDCLASSEX wc;   
  HWND hwnd;  
  MSG Msg;
    wc.cbSize        = sizeof(WNDCLASSEX);
    wc.style         = 0;  
    wc.lpfnWndProc   = WndProc;
    wc.cbClsExtra    = 0;  
    wc.cbWndExtra    = 0;  
    wc.hInstance     = hInstance;  
    wc.hIcon         = LoadIcon(NULL, IDI_APPLICATION);   
    wc.hCursor       = LoadCursor(NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);  
    wc.lpszMenuName  = NULL;  
    wc.lpszClassName = (g_szClassName);  
    wc.hIconSm       = LoadIcon(NULL, IDI_APPLICATION);
    if(!RegisterClassEx(&wc))    {  
    MessageBox(NULL,
        L"Window Registration Failed!",
    L"Error!",     
    MB_ICONEXCLAMATION | MB_OK);  
    return 0;  
    }
    // Creating the Window   
    hwnd = CreateWindowEx(    
    WS_EX_CLIENTEDGE,   
    (g_szClassName),     
    L"The title of my window",      
    WS_OVERLAPPEDWINDOW,      
    CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,        NULL, NULL, hInstance, NULL);
    if(hwnd == NULL)    {  
    MessageBox(NULL, 
        L"Window Creation Failed!",
        L"Error!",    
    MB_ICONEXCLAMATION | MB_OK);     
    return 0;  
    }
    ShowWindow(hwnd, nCmdShow);  
    UpdateWindow(hwnd);
    // Step 3: The Message Loop    
    while(GetMessage(&Msg, NULL, 0, 0) > 0)    {     
    TranslateMessage(&Msg);       
    DispatchMessage(&Msg); 
    }  
    return Msg.wParam; 
    }

输出:

    3   IntelliSense: argument of type "const char *" is incompatible with parameter of type "LPCWSTR"  c:\Users\Youssef\Documents\Visual Studio 2012\Projects\Win32Project2\Win32Project2\Win32Project2.cpp    46  2   Win32Project2
    2   IntelliSense: a value of type "const char *" cannot be assigned to an entity of type "LPCWSTR"  c:\Users\Youssef\Documents\Visual Studio 2012\Projects\Win32Project2\Win32Project2\Win32Project2.cpp    34  19  Win32Project2
Error   1   error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?   c:\users\youssef\documents\visual studio 2012\projects\win32project2\win32project2\win32project2.cpp    65  1   Win32Project2

【问题讨论】:

  • 看到这个:stackoverflow.com/questions/27049481/… 基本上,您正在构建一个在 Windows 函数中采用宽字符串的应用程序,但您创建的字符串不是宽字符串。有关详细信息,请参阅链接。
  • 您已经知道如何在字符串文字前加上 L 前缀以将它们转换为 Unicode 字符串,只需始终如一地这样做。
  • 另一个错误提示 “您是否忘记将 '#include "stdafx.h"' 添加到您的源代码中?” ,所以将其包含在第一行,或转到项目属性 -> C/C++ -> 预编译头文件并更改文件设置

标签: c++ winapi


【解决方案1】:

您遇到的编译问题与字符串类型和 Windows 程序有关。

请注意,Windows 程序有两种字符集“构建类型”。第一个是MBCS,第二个是Unicode。默认情况下(假设您使用的是 Visual Studio),构建类型为 Unicode,这意味着各种 Windows API 函数将处理宽字符串,而不是基于 charchar 的字符串。

解决问题的最简单方法是使用TCHAR 处理字符数组,使用_T() 宏处理要从Windows API 函数发送和接收的字符串文字。这将确保您的应用可以针对 Unicode 和 MBCS 进行编译(尽管 MBCS 程序现在被认为已过时)。

这一行:

 wc.lpszClassName = (g_szClassName);  

给出了一个问题,因为您正在编译一个 Unicode 应用程序,而 WNDCLASSEX 的这个成员采用 LPCTSTR(指向 TCHAR 的指针),而不是 const char *

https://msdn.microsoft.com/en-us/library/windows/desktop/ms633577%28v=vs.85%29.aspx

所以对于这个问题,

const char g_szClassName[] = ("myWindowClass");

变化应该是:

#include <tchar.h> // in case it isn't included 
//...
TCHAR g_szClassName[] = _T("myWindowClass");

【讨论】:

  • 请注意,_T() 宏属于 C 运行时库,用于 _TCHAR 类型。另一方面,Win32 API 有自己的TCHAR 类型和TEXT() 宏。从技术上讲,_TCHARTCHAR_T()TEXT() 都映射到相同的数据类型,因此可以互换,但从逻辑上讲,它们属于不同的 API,不应混用。为您进行的每个库调用(在本例中为 Win32 API)使用适当的类型/宏,因此 _T()_TCHARTEXT()TCHAR - TCHAR g_szClassName[] = TEXT("myWindowClass");
  • 别忘了这段代码中的MessageBox()CreateWindowEx() 调用也是基于TCHAR,所以对这些文字也使用TEXT() 宏,例如:@987654353 @,否则使用WCHAR 函数,例如:MessageBoxW(NULL, L"Window Registration Failed!", L"Error!", MB_ICONEXCLAMATION | MB_OK);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-07-04
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2016-05-19
  • 1970-01-01
  • 2020-01-20
相关资源
最近更新 更多