调用CreateProcess执行外部程序,总是返回FALSE,这是为什么?(100分)

  • 主题发起人 主题发起人 zhilon
  • 开始时间 开始时间
Z

zhilon

Unregistered / Unconfirmed
GUEST, unregistred user!
下面的这段代码,我单独新做一个程序,就执行下面几句,程序没有任何问题,但只要<br>把完全相似的代码放在我现在项目中就会出问题,CreateProcess执行总是返回FALSE,<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br>&nbsp; StartupInfo: TStartupInfo;<br>&nbsp; ProcessInfo: TProcessInformation;<br>&nbsp; scommad : string;<br>begin<br>&nbsp; scommad := 'bcp etc_center..deal_buf in D:/DATA.BCP -c -Uyangzy -SETC2003 -Pzu1234';<br><br>&nbsp; FillChar(StartupInfo, Sizeof(StartupInfo), #0);<br>&nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; StartupInfo.wShowWindow := 1; &nbsp;<br><br>&nbsp; if CreateProcess(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pChar(scommad),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, { creation flags }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StartupInfo,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessInfo) then<br>&nbsp; begin<br>&nbsp; &nbsp; WaitforSingleObject(ProcessInfo.hProcess, INFINITE); &nbsp;//等待,直到执行结束才执行下一个 <br>&nbsp; end;<br>&nbsp; showmessage('OK');<br>end;<br><br><br>完全相似的代码放在我现在项目中就会出问题,如下所示,CreateProcess执行总是返回FALSE,这是为什么? 我已看过<br>以前的贴子了,没有找到相关的东西,请高人指点呀!!!<br><br>&nbsp; &nbsp;.<br>&nbsp; &nbsp;.<br>&nbsp; &nbsp;.<br>&nbsp; &nbsp; &nbsp; //scommad 为sybase数据库的BCP命令,如:<br>&nbsp; &nbsp; &nbsp; //bcp etc_center..deal_buf in D:/data.txt -c -Uyangzy -SETC2003 -Pzu1234<br>&nbsp; &nbsp; &nbsp; if scommad &lt;&gt; '' then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; //CreateProcess初始化<br>&nbsp; &nbsp; &nbsp; &nbsp; FillChar(StartupInfo, Sizeof(StartupInfo), #0);<br>&nbsp; &nbsp; &nbsp; &nbsp; StartupInfo.cb := Sizeof(StartupInfo);<br>&nbsp; &nbsp; &nbsp; &nbsp; StartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; &nbsp; &nbsp; StartupInfo.wShowWindow := 1; &nbsp;//隐藏DOS窗口 &nbsp; 0:隐藏 &nbsp; 1:显示<br><br>&nbsp; &nbsp; &nbsp; &nbsp; if CreateProcess(<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; pChar(scommad),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; false,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, { creation flags }<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; StartupInfo,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessInfo) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WaitforSingleObject(ProcessInfo.hProcess, INFINITE); &nbsp;//等待,直到执行结束才执行下一个 <br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br><br>&nbsp; &nbsp;.<br>&nbsp; &nbsp;.<br>&nbsp; &nbsp;.<br><br><br>
 
具体原因不知道<br>调用CreateProcess后用GetLastError看看具体是什么错误
 
学习关注中......
 
调用GetLastError,返回值为2
 
shellexecute函数封装了createprocess,用shellexecute调用外部程序出错几率很小。
 
这个问题是不是真的很难,还是我给的分太少了?各位帮忙呀
 
2的意思是系统找不到指定的文件。 <br>
 
可是在指定的路径下明明有那个文件呀,各位请发言呀,
 
制定目录下文件是存在,但是找不到的不是 DATA.BCP,是bcp本身。<br>盘符:/路径/bcp etc_center..deal_buf in D:/DATA.BCP -c -Uyangzy -SETC2003 -Pzu1234';<br>试验一下。<br><br>如果不行,就在CreateProcess的初始目录参数中指定。<br><br>你的程序本身应该有控件改变了当前目录的。比如ShellListView之类的Shell控件等。
 
多人接受答案了。
 
后退
顶部