GetCommandLine 分析
2015-06-27 14:06
399 查看
程序的 abc.exe
三个参数 1 2 3
1. 通过CreateProcess()调用
BOOL bRet = CreateProcess(
sCmd,
sParam,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
(1)如果sCmd = "D:\test\abc.exe", sParam = "1 2 3", 则在abc.exe程序中通过GetCommandLine()得到字串
"1 2 3"
(2)如果sCmd = "D:\test\abc.exe",sParam = "D:\test\abc.exe 1
2 3", 则在abc.exe程序中通过GetCommandLine()得到字串 "D:\test\abc.exe 1 2 3"
(3)如果sCmd = "D:\test\abc.exe", sParam = "\"D:\test\abc.exe\"
1 2 3", 则在abc.exe程序中通过GetCommandLine()得到字串 "\"D:\test\abc.exe\" 1 2 3"
总结:[u]在使用CreateProcess()调用程序时[/u]
(1).在子进程中通过GetCommandLine()得到的命令行结果跟调用时传入的sParam完全一致。
(2).[u]方式(2)和(3)效果其实是一样的,对进程全路径文件名是否单独加引号,Windows都兼容。只是程序在使用这个参数时,要做脱引号处理。[/u]
(3).标准做法是采用方式(3),这样既可以兼容执行程序路径中包含的空格,也可以和ShellExecute()调用保持一致。
2. 通过ShellExecute()调用
(1)ShellExecute(NULL, "open", "D:\test\abc.exe", "1 2 3", "", SW_HIDE);
结果则在abc.exe程序中通过GetCommandLine()得到的字串为"\"D:\test\abc.exe\" 1
2 3",
即命令行中包含有进程名,并且程名有单独的引号引起,程序在使用该参数时要做脱引号处理。
(2)ShellExecute(NULL, "open", "D:\test\abc.exe",
"D:\\test\\abc.exe 1 2 3", "", SW_HIDE);
结果则在abc.exe程序中通过GetCommandLine()得到的字串为 "\"D:\test\abc.exe\" D:\test\abc.exe
1 2 3", ----- 这种调用是错误的
(3)ShellExecute(NULL, "open", "D:\test\abc.exe", "\"D:\\test\\abc.exe\"
1 2 3", "", SW_HIDE);
结果则在abc.exe程序中通过GetCommandLine()得到的字串为 "\"D:\test\abc.exe\" \"D:\test\abc.exe\"
1 2 3", ----- 这种调用是错误的
总结:在使用ShellExecute()调用程序时
(1).命令就是命令,参数就是参数,参数中不需要再次包含执行程序名。
(2).在子进程中通过GetCommandLine()得到的命令行参数中,总会包含有进程全路径文件名,并且是被单独引号引起的,用到时需要做脱引号处理。
(3).在子进程中[u]通过GetCommandLine()得到的命令行参数结果跟ShellExecute()的第五个参数lpDirectory没任何关系,调用时永远默认置空字串即可。[/u]
(4).ShellExecuteEx()与ShellExecute()的情况一样。
3. 通过abc.exe的快捷方式启动,则在abc.exe程序中通过GetCommandLine()得到字串 ""D:\test\abc.exe"
1 2 3", 注意进程名有引号
总结:在CMD命令行里面调用时,不管是否对进程名加引号,在程序里面获取到的结果,进程名都会有单独的引号引起的。
4. 通过CMD命令行调用
(1)输入 D:\test\abc.exe 1 2 3, 则在abc.exe程序中通过GetCommandLine() 得到字串
""D:\test\abc.exe" 1 2 3", 注意进程名单独有引号
(2)输入 "D:\test\abc.exe" 1 2 3, 则在abc.exe程序中通过GetCommandLine() 得到字串
""D:\test\abc.exe" 1 2 3", 注意进程名单独有引号
可见:调用时无论对进程全路径文件名是否加引号, 结果是一样, 得到的参数中进程名都有单独的引号引起的。
特别注意:
对于路径中包含空格的情况, 则调用时必须加引号, 如 "C:\Program Files\abc.exe" 1 2 3
三个参数 1 2 3
1. 通过CreateProcess()调用
BOOL bRet = CreateProcess(
sCmd,
sParam,
NULL,
NULL,
FALSE,
0,
NULL,
NULL,
&si,
&pi);
(1)如果sCmd = "D:\test\abc.exe", sParam = "1 2 3", 则在abc.exe程序中通过GetCommandLine()得到字串
"1 2 3"
(2)如果sCmd = "D:\test\abc.exe",sParam = "D:\test\abc.exe 1
2 3", 则在abc.exe程序中通过GetCommandLine()得到字串 "D:\test\abc.exe 1 2 3"
(3)如果sCmd = "D:\test\abc.exe", sParam = "\"D:\test\abc.exe\"
1 2 3", 则在abc.exe程序中通过GetCommandLine()得到字串 "\"D:\test\abc.exe\" 1 2 3"
总结:[u]在使用CreateProcess()调用程序时[/u]
(1).在子进程中通过GetCommandLine()得到的命令行结果跟调用时传入的sParam完全一致。
(2).[u]方式(2)和(3)效果其实是一样的,对进程全路径文件名是否单独加引号,Windows都兼容。只是程序在使用这个参数时,要做脱引号处理。[/u]
(3).标准做法是采用方式(3),这样既可以兼容执行程序路径中包含的空格,也可以和ShellExecute()调用保持一致。
2. 通过ShellExecute()调用
(1)ShellExecute(NULL, "open", "D:\test\abc.exe", "1 2 3", "", SW_HIDE);
结果则在abc.exe程序中通过GetCommandLine()得到的字串为"\"D:\test\abc.exe\" 1
2 3",
即命令行中包含有进程名,并且程名有单独的引号引起,程序在使用该参数时要做脱引号处理。
(2)ShellExecute(NULL, "open", "D:\test\abc.exe",
"D:\\test\\abc.exe 1 2 3", "", SW_HIDE);
结果则在abc.exe程序中通过GetCommandLine()得到的字串为 "\"D:\test\abc.exe\" D:\test\abc.exe
1 2 3", ----- 这种调用是错误的
(3)ShellExecute(NULL, "open", "D:\test\abc.exe", "\"D:\\test\\abc.exe\"
1 2 3", "", SW_HIDE);
结果则在abc.exe程序中通过GetCommandLine()得到的字串为 "\"D:\test\abc.exe\" \"D:\test\abc.exe\"
1 2 3", ----- 这种调用是错误的
总结:在使用ShellExecute()调用程序时
(1).命令就是命令,参数就是参数,参数中不需要再次包含执行程序名。
(2).在子进程中通过GetCommandLine()得到的命令行参数中,总会包含有进程全路径文件名,并且是被单独引号引起的,用到时需要做脱引号处理。
(3).在子进程中[u]通过GetCommandLine()得到的命令行参数结果跟ShellExecute()的第五个参数lpDirectory没任何关系,调用时永远默认置空字串即可。[/u]
(4).ShellExecuteEx()与ShellExecute()的情况一样。
3. 通过abc.exe的快捷方式启动,则在abc.exe程序中通过GetCommandLine()得到字串 ""D:\test\abc.exe"
1 2 3", 注意进程名有引号
总结:在CMD命令行里面调用时,不管是否对进程名加引号,在程序里面获取到的结果,进程名都会有单独的引号引起的。
4. 通过CMD命令行调用
(1)输入 D:\test\abc.exe 1 2 3, 则在abc.exe程序中通过GetCommandLine() 得到字串
""D:\test\abc.exe" 1 2 3", 注意进程名单独有引号
(2)输入 "D:\test\abc.exe" 1 2 3, 则在abc.exe程序中通过GetCommandLine() 得到字串
""D:\test\abc.exe" 1 2 3", 注意进程名单独有引号
可见:调用时无论对进程全路径文件名是否加引号, 结果是一样, 得到的参数中进程名都有单独的引号引起的。
特别注意:
对于路径中包含空格的情况, 则调用时必须加引号, 如 "C:\Program Files\abc.exe" 1 2 3
相关文章推荐
- 密码发生器
- Linux基础之常识(一)
- java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceE解决方法
- java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceE解决方法
- open函数
- java.lang.NoClassDefFoundError: org/springframework/dao/support/PersistenceE解决方法
- SharePoint 2010 新列表模板列表
- Yii2 使用十八 使用redis
- git 简单用法
- 多面编程语言Scala
- 转帖:向开源项目贡献源码(以 Orchard 为例)
- html速度测试,超过500行的大表格处理(读,写,设置颜色)
- 6.27 嘻唰唰 oj 运算符重载 +—*/ y与上一篇区别
- 第十六周 项目二--有些数的阶乘就不算了
- 最小生成树的Kruskal算法实现
- maven安装及在MyEclipse中 设置
- js获取来源url(上个页面的地址)
- 迪米特法则
- 【C#】【邮件】C#发送邮件出现 "指定字符串与主题所要求的形式不符"
- Android 让EditText失去焦点避免自动弹出输入法