可能是CREATEFILE的问题.高手请进. -----急........... ( 积分: 50 )

  • 主题发起人 主题发起人 yonge23
  • 开始时间 开始时间
Y

yonge23

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button2Click(Sender: TObject);<br>var i:integer;<br> &nbsp; &nbsp;sinfo:TSTARTUPINFO;<br> &nbsp; &nbsp;pinfo:TProcessInformation;<br> &nbsp; &nbsp;ct,et,kt,ut:Tfiletime;<br> &nbsp; &nbsp;hinputfile,houtputfile:THandle;<br> &nbsp; &nbsp;security:PSecurityAttributes;<br>begin<br> &nbsp;try<br> &nbsp;//CreatePipe(hReadPipe, hWritePipe, nil, 0);<br> &nbsp;security.nLength:=sizeof(security);<br> &nbsp;security.lpSecurityDescriptor:=NIL;<br> &nbsp;security.bInheritHandle:=TRUE;<br> &nbsp;hInputFile:=CreateFile(pchar('input.txt'),GENERIC_READ,FILE_SHARE_READ,security,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);<br> &nbsp;houtputFile:=CreateFile(pchar('output.txt'),GENERIC_WRITE,FILE_SHARE_WRITE,security,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);<br><br> &nbsp;FillChar(sInfo,sizeof(sInfo),#0);<br> &nbsp;sInfo.cb := SizeOf(sInfo); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// CREATE_NEW_CONSOLE or<br> &nbsp;sinfo.dwFlags:=STARTF_USESTDHANDLES;<br> &nbsp;sinfo.hStdInput:=hInputFile;<br> &nbsp;sinfo.hStdOutput:=houtputFile;<br><br> &nbsp;if not CreateProcess('xxx.exe',nil,nil,nil,false,NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo) then<br> &nbsp; &nbsp;MessageBox(Application.handle,'指定程序启动失败!','错误',MB_OK or MB_ICONSTOP)<br> &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;while (true) do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;//WaitForSingleObject(pInfo.hProcess,INFINITE);<br> &nbsp; &nbsp; &nbsp;getprocesstimes(pinfo.hProcess,ct,et,kt,ut);<br> &nbsp; &nbsp; &nbsp;if (ut.dwHighDateTime-ut.dwLowDateTime)&gt;1000000000 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( pinfo.hProcess );<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( pinfo.hThread );<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(houtputFile);<br> CloseHandle(hInputFile);<br> &nbsp; &nbsp; &nbsp; &nbsp;TerminateProcess(pinfo.hProcess,1);<br> &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage(IntToStr(GetLastError));<br> &nbsp; &nbsp; &nbsp; &nbsp;showmessage('运行超时');<br> &nbsp; &nbsp; &nbsp; &nbsp;edit6.Text:=inttostr(kt.dwHighDateTime-kt.dwLowDateTime);<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;except<br> &nbsp; &nbsp;showmessage('测试失败');<br> &nbsp;end;<br>end;<br><br>运行时下面错误:<br>---------------------------<br>Debugger Exception Notification<br>---------------------------<br>Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00419C97 in module 'Project1.exe'. Read of address 00000014'. Process stopped. Use Step or Run to continue.<br>---------------------------<br>OK &nbsp; Help &nbsp; <br>---------------------------<br><br>哪位大哥帮忙看看哪位了.可能问题是出现在CREATEFILE处.<br><br>我的程序代码是用来调用外部可执行程序.测试外部可执行程序.测试数据在INPUT.TXT中.测试结果输出到OUTPUT.TXT中.
 
procedure TForm1.Button2Click(Sender: TObject);<br>var i:integer;<br> &nbsp; &nbsp;sinfo:TSTARTUPINFO;<br> &nbsp; &nbsp;pinfo:TProcessInformation;<br> &nbsp; &nbsp;ct,et,kt,ut:Tfiletime;<br> &nbsp; &nbsp;hinputfile,houtputfile:THandle;<br> &nbsp; &nbsp;security:PSecurityAttributes;<br>begin<br> &nbsp;try<br> &nbsp;//CreatePipe(hReadPipe, hWritePipe, nil, 0);<br> &nbsp;security.nLength:=sizeof(security);<br> &nbsp;security.lpSecurityDescriptor:=NIL;<br> &nbsp;security.bInheritHandle:=TRUE;<br> &nbsp;hInputFile:=CreateFile(pchar('input.txt'),GENERIC_READ,FILE_SHARE_READ,security,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);<br> &nbsp;houtputFile:=CreateFile(pchar('output.txt'),GENERIC_WRITE,FILE_SHARE_WRITE,security,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);<br><br> &nbsp;FillChar(sInfo,sizeof(sInfo),#0);<br> &nbsp;sInfo.cb := SizeOf(sInfo); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// CREATE_NEW_CONSOLE or<br> &nbsp;sinfo.dwFlags:=STARTF_USESTDHANDLES;<br> &nbsp;sinfo.hStdInput:=hInputFile;<br> &nbsp;sinfo.hStdOutput:=houtputFile;<br><br> &nbsp;if not CreateProcess('xxx.exe',nil,nil,nil,false,NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo) then<br> &nbsp; &nbsp;MessageBox(Application.handle,'指定程序启动失败!','错误',MB_OK or MB_ICONSTOP)<br> &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;while (true) do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;//WaitForSingleObject(pInfo.hProcess,INFINITE);<br> &nbsp; &nbsp; &nbsp;getprocesstimes(pinfo.hProcess,ct,et,kt,ut);<br> &nbsp; &nbsp; &nbsp;if (ut.dwHighDateTime-ut.dwLowDateTime)&gt;1000000000 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( pinfo.hProcess );<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( pinfo.hThread );<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(houtputFile);<br> CloseHandle(hInputFile);<br> &nbsp; &nbsp; &nbsp; &nbsp;TerminateProcess(pinfo.hProcess,1);<br> &nbsp; &nbsp; &nbsp; &nbsp;ShowMessage(IntToStr(GetLastError));<br> &nbsp; &nbsp; &nbsp; &nbsp;showmessage('运行超时');<br> &nbsp; &nbsp; &nbsp; &nbsp;edit6.Text:=inttostr(kt.dwHighDateTime-kt.dwLowDateTime);<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;except<br> &nbsp; &nbsp;showmessage('测试失败');<br> &nbsp;end;<br>end;<br><br>运行时下面错误:<br>---------------------------<br>Debugger Exception Notification<br>---------------------------<br>Project Project1.exe raised exception class EAccessViolation with message 'Access violation at address 00419C97 in module 'Project1.exe'. Read of address 00000014'. Process stopped. Use Step or Run to continue.<br>---------------------------<br>OK &nbsp; Help &nbsp; <br>---------------------------<br><br>哪位大哥帮忙看看哪位了.可能问题是出现在CREATEFILE处.<br><br>我的程序代码是用来调用外部可执行程序.测试外部可执行程序.测试数据在INPUT.TXT中.测试结果输出到OUTPUT.TXT中.
 
没人回答?!<br><br>至少也UP一下.
 
失误,不能妄言,我也试试...
 
搞不懂..<br><br>我有个&quot;001.C&quot;的文件:<br>#include &lt;stdio.h&gt;<br>#include &lt;math.h&gt;<br>const g=10;<br>main()<br>{<br>int a,b,c;<br>printf(&quot; &nbsp; A= &quot;);<br>scanf(&quot;%d&quot;,&amp;a);<br>printf(&quot; &nbsp; B= &quot;);<br>scanf(&quot;%d&quot;,&amp;b);<br>c=a+b;<br>printf(&quot; A+B = %d/n&quot;,c);<br>}<br><br>我现在想用DELPHI来测试该程序代码是否正确..<br><br>首先我先利用TURBOC的TCC.EXE对&quot;001.C&quot;进行编译.使之生成&quot;001.EXE&quot;可执行文件.我用如下方法:<br>ShellExecute(Handle, 'open', PChar('command.com'), PChar('/c C:/turboc2/tcc -e001 001.c &nbsp;&gt;p.txt'), nil, SW_SHOW);<br>这步可以成功解决了,也可以正确地生成可执行程序&quot;001.EXE&quot;.<br><br>接下来,我要调用&quot;001.EXE&quot;进行测试了.<br>我把测试数据:1 &nbsp;2 &nbsp;放在input.txt 文件中.想把测试结果的数据放在output.txt文件中.<br>我用如下方法:<br>ShellExecute(Handle, 'open', PChar('command.com'), PChar('/c 001 &nbsp;&lt;input.txt &gt;output.txt'), nil, SW_SHOW);<br>这样,测试数据就不用通过手动从键盘上输入.只要读取input.txt 文件中的测试数据就可以了.<br>但是用ShellExecute函数时,当001.EXE程序的代码如下时:<br>#include &lt;stdio.h&gt;<br>#include &lt;math.h&gt;<br>const g=10;<br>main()<br>{<br>int a,b,c;<br>printf(&quot; &nbsp; A= &quot;);<br>scanf(&quot;%d&quot;,&amp;a);<br>printf(&quot; &nbsp; B= &quot;);<br>scanf(&quot;%d&quot;,&amp;b);<br>while (g==10) ;<br>c=a+b;<br>printf(&quot; A+B = %d/n&quot;,c);<br>}<br>由于while (g==10) ;,发生逻辑错误,进入死循环.得用DELPHI程序在一段时间(如2秒)自动结果001.EXE程序.这时用ShellExecute函数就不大方便了.<br>所以我想用Creatprocess函数来代替<br>ShellExecute(Handle, 'open', PChar('command.com'), PChar('/c 001 &nbsp;&lt;input.txt &gt;output.txt'), nil, SW_SHOW);<br>实现同样的功能,并且能够结果001.EXE的死循环问题.<br><br>有哪位大哥有空能不能帮我实现这个功能,,,感谢万份.<br><br>不知道我能否把问题描述清楚...如果有不清楚的再说...
 
procedure TForm1.btnAuthorClick(Sender: TObject);<br>var i:integer;<br> &nbsp; &nbsp;sinfo:TSTARTUPINFO;<br> &nbsp; &nbsp;pinfo:TProcessInformation;<br> &nbsp; &nbsp;ct,et,kt,ut:Tfiletime;<br> &nbsp; &nbsp;hinputfile,houtputfile:THandle;<br> &nbsp; &nbsp;security:TSecurityAttributes;//这里需要更改<br>begin<br> &nbsp;try<br> &nbsp;//CreatePipe(hReadPipe, hWritePipe, nil, 0);<br> &nbsp;security.nLength:=sizeof(security);<br> &nbsp;security.lpSecurityDescriptor:=NIL;<br> &nbsp;security.bInheritHandle:=TRUE;<br> &nbsp;hInputFile:=CreateFile(pchar('input.txt'),GENERIC_READ,FILE_SHARE_READ,@security,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0);//这一行<br> &nbsp;houtputFile:=CreateFile(pchar('output.txt'),GENERIC_WRITE,FILE_SHARE_WRITE,@security,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0);//这一行<br><br><br> &nbsp;FillChar(sInfo,sizeof(sInfo),#0);<br> &nbsp;sInfo.cb := SizeOf(sInfo); &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// CREATE_NEW_CONSOLE or<br> &nbsp;sinfo.dwFlags:=STARTF_USESTDHANDLES;<br> &nbsp;sinfo.hStdInput:=hInputFile;<br> &nbsp;sinfo.hStdOutput:=houtputFile;<br><br> &nbsp;if not CreateProcess('xxx.exe',nil,nil,nil,false,NORMAL_PRIORITY_CLASS, nil, nil, sInfo, pInfo) then<br> &nbsp; &nbsp;MessageBox(Application.handle,'指定程序启动失败!','错误',MB_OK or MB_ICONSTOP)<br> &nbsp;else<br> &nbsp; &nbsp;begin<br> &nbsp; &nbsp;while (true) do<br> &nbsp; &nbsp; &nbsp;begin<br> &nbsp; &nbsp; &nbsp;sleep(1000);<br> &nbsp; &nbsp; &nbsp;//WaitForSingleObject(pInfo.hProcess,INFINITE);<br> &nbsp; &nbsp; &nbsp;getprocesstimes(pinfo.hProcess,ct,et,kt,ut);<br> &nbsp; &nbsp; &nbsp;if (ut.dwHighDateTime-ut.dwLowDateTime)&gt;1000000000 then<br> &nbsp; &nbsp; &nbsp; &nbsp;begin//这里更改一下释放顺序<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle(houtputFile);<br> CloseHandle(hInputFile);<br> &nbsp; &nbsp; &nbsp; &nbsp;TerminateProcess(pinfo.hProcess,1);<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( pinfo.hProcess );<br> &nbsp; &nbsp; &nbsp; &nbsp;CloseHandle( pinfo.hThread );<br> &nbsp; &nbsp; &nbsp; &nbsp;edit1.Text:=inttostr(kt.dwHighDateTime-kt.dwLowDateTime);<br> &nbsp; &nbsp; &nbsp; &nbsp;exit;<br> &nbsp; &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp; &nbsp;end;<br> &nbsp; &nbsp;end;<br> &nbsp;except<br> &nbsp; &nbsp;showmessage('测试失败');<br> &nbsp;end;<br>end;
 
http://info.tlw.cn/7747.htm<br>另,楼主可以参看上面的实现方法
 
接受答案了.
 

Similar threads

后退
顶部