c/c++简单获取命令行参数的方法
版权声明:
本文为博主原创文章,转载请声明原文链接...谢谢。o_0。
更新时间:
2019-03-02 17:11:16
温馨提示:
学无止境,技术类文章有它的时效性,请留意文章更新时间,如发现内容有误请留言指出,防止别人"踩坑",我会及时更新文章
一面是一个升级程序的命令行接收参数
// 程序入口
int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
LPTSTR *szArgList;
int argCount;
szArgList = CommandLineToArgvW(GetCommandLine(), &argCount);
//解析参数
tstring url = L"";
tstring softVersion = L"";
tstring softPid = L"";
bool jingmo = false;
tstring cmdStr = L"";
for (int i = 0;i < argCount;i++) {
if (_tcscmp(szArgList[i], L"-url") == 0) {
url = szArgList[i + 1];
//printf("url: %s", url);
}
else if (_tcscmp(szArgList[i], L"-version") == 0) {
softVersion = szArgList[i + 1];
//MessageBox(NULL, softVersion.c_str(), L"提示", 0);
//printf("softVersion: %s", softVersion);
}
else if (_tcscmp(szArgList[i], L"-pid") == 0) {
softPid = szArgList[i + 1];
//printf("softPid: %s", softPid);
}
else if (_tcscmp(szArgList[i], L"-jingmo") == 0) {
if (_tcscmp(szArgList[i + 1], L"0") == 0) {
jingmo = false;
}
else {
jingmo = true;
}
//printf("jingmo: true");
}
else if (_tcscmp(szArgList[i], L"-cmd") == 0) {
cmdStr = szArgList[i + 1];
//printf("cmdStr: %s", cmdStr);
}
}
}