我的问题是,参数只是检索每个参数中的第一个字母,因此我不知道为什么。有人能详细说明一下吗?
#include <Windows.h>
#include <string>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR nCmdLine, int iCmdShow){
LPWSTR *szArglist;
int nArgs = 0;
szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
std::string a;
for(int i=0; i<nArgs; i++){
a += (LPCSTR)szArglist[i];
}
MessageBox(NULL, (LPCSTR)a.c_str(), (LPCSTR)a.c_str(), MB_OK);
LocalFree(szArglist);
return 0;
}我相信这是CommandLineToArgvW(GetCommandLineW(), &nArgs);的问题
发布于 2013-07-26 17:21:14
LPWSTR被键入为wchar_t *,szArglist是wchar_t *的数组。宽字符是2字节而不是1,因此字母可以表示为:
0x00380x0000
但是,如果您取这些字节并说‘嗨,假装我是char *,这看起来像一个C-字符串,只有一个字母(0x0038),因为第二个字符(0x0000)是null,在C风格的字符串中,它代表字符串的末尾。
您遇到的问题是,您试图将宽字符(wchar_t)插入到非宽字符串(char)中,这是一个更为复杂的操作。
解决方案:要么使用wstring/wchar_t everywhere (对应于LPWSTR/LPCWSTR),要么使用string/char everywhere (对应于LPSTR和LPCSTR )。请注意,“使用unicode”的项目设置应该与您的决定相匹配。别把这些混在一起!
发布于 2013-07-26 17:13:13
这不应该是
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR nCmdLine, int iCmdShow)
{
MessageBoxA(NULL, nCmdLine, nCmdLine, MB_OK);
return 0;
}或
int WINAPI
WinMain(HINSTANCE hInstance, HINSTANCE hpInstance, LPSTR nCmdLine, int iCmdShow)
{
MessageBoxW(NULL, GetCommandLineW(), GetCommandLineW(), MB_OK);
return 0;
}https://stackoverflow.com/questions/17886846
复制相似问题