CreateProcess和命令行参数 [英] CreateProcess and command line arguments

查看:636
本文介绍了CreateProcess和命令行参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景信息: Windows 7,Visual C ++ 2010 Express

Background Info: Windows 7, Visual C++ 2010 Express

问题:CreateProcess()不断返回无效的命令行参数

Problem: CreateProcess() keeps returning with 'Invalid command line argument'

说明:我正在编写一段使用Windows API的CreateProcess调用外部程序的代码。到目前为止,我已经可以使用一个外部程序了:

Explanation: I'm writing a piece of code that calls external programs using the Windows API's CreateProcess. I've gotten the call to work with one external program so far:

if( !CreateProcess( "C:\\Temp\\convert.exe",
    t_str,        // Arguments
    ...
}
//where t_str is " C:\\img1.jpeg C:\\img1.pgm" (ImageMagick if you're wondering). 

此即使我将所有数据推送到Windows字符串和指针中时,也能很好地工作,所以我将所有CreateProcess()的复制复制到另一个外部程序的调用:

This works perfectly even with the amount of data-mangling I did to push everything into the Windows strings and pointers. So I copied all of the mangling for CreateProcess() for another call to a different external program:

 if( !CreateProcess( "C:\\Temp\\sift.exe",
     t_str2,        // Arguments
     ...
 }
 //where t_str2 is ` < C:\\img1.pgm > C:\\img1.key`

基本上,有些相似,但是所有变量名都已更改(因为我有两个调用是串行运行的)。问题是;此命令无法运行,而是打印出无效的命令林e参数:< C:\img1.pgm。当然,该命令在命令提示符下可以正常工作,但在我的代码中效果不佳。

Basically, something very similar, but with all the variable names changed (since I have these two calls running serial). And this is where the problem is; this doesn't run, and, instead, prints out "Invalid command line argument: < C:\img1.pgm". Of course, this command works fine in the command prompt, but not in my code.

我将t_str2切换到其他位置

I switched t_str2 to something else a little less complicated(since I know how sift.exe works) and I get the same result. Same thing happens when I run just sift and not convert.

问题(因为我知道sift.exe的工作原理),并且得到了相同的结果。 :是什么原因可能导致此问题?我该怎么做才能进一步调试该问题?有关我使用的方法的替代方法的任何建议?可以得到任何帮助。我可以提供更多的代码,但是很简单,并且不会有太大问题。

Question: What could be causing this problem? What could I do to further debug this problem? Any suggestions on alternatives to the methods I'm using? Any help is appreciated. I can provide further code, but It's pretty straight forward, and not much could go wrong with it.

推荐答案

您不能对使用命令行重定向运算符直接产生CreateProcess()。您必须产生一个cmd.exe实例,并将运算符传递给它,例如:

You cannot use command-line redirection operators with CreateProcess() directly. You have to spawn an instance of cmd.exe and pass the operators to it instead, eg:

CreateProcess( "C:\\windows\\system32\\cmd.exe", t_str2, ...))

t_str2 / CC:\\Te mp\\sift.exe< C:\\img1.pgm> C:\\img1.key 。可以通过读取%COMSPEC%环境变量来确定cmd.exe的实际路径。

Where t_str2 is "/C C:\\Temp\\sift.exe < C:\\img1.pgm > C:\\img1.key". The actual path to cmd.exe can be determined by reading the %COMSPEC% environment variable.

这篇关于CreateProcess和命令行参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆