【发布时间】:2013-07-27 01:08:42
【问题描述】:
我的问题是参数只检索每个参数中的第一个字母,为此我不知道为什么.. 有人可以详细说明吗?
#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);
【问题讨论】:
-
这正是 C 强制转换是邪恶的原因。如果您遇到编译器错误,请不要本能地进行强制转换。使用
std::wstring,for循环可以替换为将a初始化为GetCommandLineW并去掉空格。 -
为了详细说明为什么 C 转换是邪恶的,宽字符可能由位序列
0100000000000000表示,其中 ANSI 字符表示的相同位序列是两个字符01000000 00000000。它现在是一个 1 个字符的以 null 结尾的字符串。 -
但是 wstring 会使我的类型成为一个无法检索所有数组元素的常量。缺少空格不是大问题。
-
我现在理解位排序,但是如何将参数放入数组中?
-
std::wstring与std::string没有什么不同。它们都是使用不同字符类型的 typedef。
标签: c++ parameters arguments argv