首页 > 解决方案 > c++ winapi 代码块有效,Visual Studio 社区无效

问题描述

在代码块中,它可以正常工作。但在 Visual Studio 中,它说:

类型的 C++ 参数与类型 LPCWSTR 的参数不兼容

这是我的代码:

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif
#include <windows.h>
    
    
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
    
    int s =  MessageBox(NULL,"Hi This is my First c++ GUI","Win32 API Say:",6);
    if(s == 10){
        MessageBox(NULL,"Hi This is my First c++ GUI","Win32 API Say:",6);
    }else if(s == 11){
        MessageBox(NULL,"Yes GO On","Win32 API Say:",MB_ICONINFORMATION);
    }else{
        MessageBox(NULL,"GoodBey !!","Win32 API Say:",MB_OK);
    }
    return 0;
}

标签: c++winapi

解决方案


您正在使用 的TCHAR版本MessageBox(),它实际上是一个预处理器宏,分别映射到MessageBoxW()MessageBoxA()取决于是否UNICODE定义。

在 CodeBlocks 的情况下,如果未定义它就可以工作UNICODE,因此MessageBox()映射到MessageBoxA(),它需要LPCSTR( const char *) 字符串作为输入。您正在传递char基于 - 的字符串文字,所以一切都很好。

在 Visual Studio 的情况下,如果UNICODE定义了 is 则失败,因此MessageBox()映射到MessageBoxW(),它需要LPCWSTR( const wchar_t *) 字符串作为输入。由于您传递的是char基于 - 的字符串文字,并且char*不能隐式转换为wchar_t*,因此会出现错误。

如果您要继续使用TCHAR功能1,则应将字符串文字包装在TEXT()预处理器宏中,以根据编译器设置使它们使用MessageBox()(或任何其他TCHAR基于 API)期望的相同字符编码。

1:你真的不应该依赖TCHAR现代代码。它的存在只是为了向后兼容旧代码,早于微软推动将 Win9x/ME 应用程序迁移到 NT4+/Win2K/XP 上的 Unicode。 TCHAR被发明来帮助此类代码从 ANSIchar字符串迁移到 Unicodewchar_t字符串。

此外,您也不应该依赖“幻数”。它使代码更难阅读和理解。请改用预定义的命名常量。您已经在第 3 次MB_ICONINFORMATIONMB_OK第 4MessageBox()次调用MessageBox().

试试这个:

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif
#include <windows.h>
    
    
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
    
    int s =  MessageBox(NULL, TEXT("Hi This is my First c++ GUI"), TEXT("Win32 API Say:"), MB_CANCELTRYCONTINUE);
    if(s == IDTRYAGAIN){
        MessageBox(NULL, TEXT("Hi This is my First c++ GUI"), TEXT("Win32 API Say:"), MB_CANCELTRYCONTINUE);
    }else if(s == IDCONTINUE){
        MessageBox(NULL, TEXT("Yes GO On"), TEXT("Win32 API Say:"), MB_ICONINFORMATION);
    }else{
        MessageBox(NULL, TEXT("GoodBey !!"), TEXT("Win32 API Say:"), MB_OK);
    }
    return 0;
}

也就是说,您根本不应该依赖TCHARAPI,而是根据需要直接调用MessageBoxA()MessageBoxW()(或其他窄/宽 API),例如:

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif
#include <windows.h>
    
    
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
    
    int s =  MessageBoxA(NULL, "Hi This is my First c++ GUI", "Win32 API Say:", MB_CANCELTRYCONTINUE);
    if(s == IDTRYAGAIN){
        MessageBoxA(NULL, "Hi This is my First c++ GUI", "Win32 API Say:", MB_CANCELTRYCONTINUE);
    }else if(s == IDCONTINUE){
        MessageBoxA(NULL, "Yes GO On", "Win32 API Say:", MB_ICONINFORMATION);
    }else{
        MessageBoxA(NULL, "GoodBey !!", "Win32 API Say:", MB_OK);
    }
    return 0;
}
#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif
#include <windows.h>
    
    
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE hPrev,LPSTR lpCmdLine, int iCmdShow){
    
    int s =  MessageBoxW(NULL, L"Hi This is my First c++ GUI", L"Win32 API Say:", MB_CANCELTRYCONTINUE);
    if(s == IDTRYAGAIN){
        MessageBoxW(NULL, L"Hi This is my First c++ GUI", L"Win32 API Say:", MB_CANCELTRYCONTINUE);
    }else if(s == IDCONTINUE){
        MessageBoxW(NULL, L"Yes GO On", L"Win32 API Say:", MB_ICONINFORMATION);
    }else{
        MessageBoxW(NULL, L"GoodBey !!", L"Win32 API Say:", MB_OK);
    }
    return 0;
}

推荐阅读