C/C++ CreateProcess command line param problem

本文探讨了使用CreateProcess函数启动外部程序时遇到的问题,并通过几个实例详细解释了如何正确设置参数以避免错误行为。文章还深入分析了当应用程序名称为空时,CreateProcess函数的行为特点。
 

C/C++ CreateProcess command line param problem

Back to some tech stuff, a simple task yesterday made me much headaches. I needed to run pdftk.exe from our program. I figured out the params and all worked as a dream. Let's implement it:
CreateProcess("pdftk.exe", "a.pdf b.pdf cat output c.pdf dont_ask", ...)

And it did not work. I failed to get any error, any output. Then I tried with cmd:

CreateProcess("cmd.exe", "/C pdftk.exe a.pdf b.pdf cat output c.pdf dont_ask", ...)

And it worked, but why? And I don't want the cmd.exe to be involved. I saw an example where program name was null:

CreateProcess(NULL, "pdftk.exe a.pdf b.pdf cat output c.pdf dont_ask", ...)

And it worked, ok, we got rid of cmd.exe.
WTF? The program name is null and the params, wait, command line!

The flexibility of the CreateProcess() function (and a possible point of confusion) arises when you pass a valid string pointer to both the ApplicationName and CommandLine parameters. This allows you to specify the application to be executed as well as the complete command line that is passed to the application. One might assume that the command line passed to the created application is a composite of the ApplicationName and CommandLine parameters, but this is not the case. As a result, a process created by CreateProcess can receive a value other than its .exe name as its "argv[0]" parameter. The following is an example of a call to CreateProcess that produces this "abnormal" behavior:
   CreateProcess( "c:\\MyApp.exe", "Param1 Param2 Param3", ...)
 
MyApp's arguments will be as follow:
  argv[0] == "Param1"
  argv[1] == "Param2"
  argv[2] == "Param3"
Read more about it here. Hope it will save you some time :)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值