我试图制作一个纯粹的WinApi程序,该程序只接受参数并将第一个参数放入MessageBoxW。但是,存在一个问题,因为传递的参数被放入LPWSTR数组,而MessageBoxW接受LPCWSTR。这给了我一个错误,我尝试了所有操作,使用reinterpret_cast,const_cast,标准C转换,但是没有任何效果。
这里是代码:
#include <Windows.h>
int main()
{
LPWSTR *argv;
int argc;
argv = CommandLineToArgvW(GetCommandLineW(), &argc);
LPWSTR lol = argv[1];
MessageBoxW(NULL, lol, "Test", 0);
ExitProcess(0);
}
结果是:
main.cpp(11): error C2664: 'int MessageBoxW(HWND,LPCWSTR,LPCWSTR,UINT)': cannot convert argument 3 from 'const char [5]' to 'LPCWSTR'
main.cpp(11): note: Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
使用命令行在Visual Studio 2015中编译的程序:
cl /Os /GS- /Oi- primetest.cpp /link /fixed /nodefaultlib /safeseh:no /filealign:512 /entry:main /subsystem:console /MERGE:.rdata=.text kernel32.lib shell32.lib user32.lib
这些参数允许一个2kb的小输出文件,但这与类型转换无关。
此外,如果在MessageBoxW中放置了字符串文字而不是“ lol”变量,则程序将编译,并且不会出现任何错误或警告,并且可以正常工作。
#include <Windows.h>
int main()
{
LPCWSTR *argv;
int argc;
argv = CommandLineToArgvW(GetCommandLineW(),
&argc);
LPCWSTR lol = argv[1];
MessageBoxW(NULL, lol, L"Test", NULL);
ExitProcess(0);
return 0;
}