请教delphi调用外部程序的问题(100分)

  • 主题发起人 主题发起人 dream_flyer
  • 开始时间 开始时间
D

dream_flyer

Unregistered / Unconfirmed
GUEST, unregistred user!
本人最近制作一个ACM训练题程序,遇到的问题无法解决: <br>1.通过一个按钮(单击此按钮为调用turbo pascal编译器编译特定程序文件*.pas),并在编译执行后能够返回编译结果(通过、未通过) <br>2.如何在所编程序中获得调用的turbo pascal执行的结果
 
只知道shellexecute,别的就不会了
 
这个我也知道<br>。。。。
 
对,就用shellexecute,查查help就知道如何用了。
 
shellexecute(application.handle,'open','brcc32.exe ...*.pas ',....);
 
win api中shellexecute是用来调用外部程序,但是我现在遇到的问题是要调用turbo-<br>pascal并且在后台编译返回结果,而上述shellexecute仅仅是打开turbo-pascal而已,<br>试问如何让pascal在后台编译呢???
 
估计只能分析编译日志文件了,如果它不产生编译日志文件(估计不太可能,不过我没用过)<br>或找不到该文件的话,只好把编译器的执行结果重定向到一个文件再来分析文件了。
 
主要是使用API含数来作的,<br>我几个专用于执行外部文件的含数,<br>其中以shellexecute最好用。<br>如果是你说的让其在后台编译,<br>那你只能写那个程序的接口含数了。<br>或者都接去设用它的编译器,而不是打开<br>turbo-pascal<br><br>我记得turbo-pascal有专门的命令行编译程序,<br>它自己就是调用这个程序来工作的。<br>你找找了
 
我查看过turbo-pascal的文件夹,里面有一个tpc.exe(估计是编译程序)<br>使用ShellExecute(Handle,'open','D:/.../tpc.exe','.../*.pas,'',...)<br>果然是编译程序,而且弹出窗口显示程序编译通过
 
请问接口函数怎么用法?如何知道编译通过?<br>还有谢谢大家的回答:-)
 
大富翁里提到好多次了,一个是用SHellExecute、另一个是WinExeC
 
你这个训练题程序太高级了,<br>很少遇到还带编译器的训练程序。<br>大学时候见过一个练汇编的,不过<br>那个编译器(就是查错器啦其实不编译)是自己写的。<br>执行dos命令得到执行结果,关注此题。<br>
 
from delphi tips newsletter, a example to capture output of dos applications.<br><br>just add a button and a memo to your form<br><br>try it! it 's pefect to fulfill your goal.<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br><br>&nbsp;procedure RunDosInMemo(DosApp:String;AMemo:TMemo);<br>&nbsp;const<br>&nbsp; &nbsp; ReadBuffer = 2400;<br>&nbsp;var<br>&nbsp; Security &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: TSecurityAttributes;<br>&nbsp; ReadPipe,WritePipe &nbsp;: THandle;<br>&nbsp; start &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : TStartUpInfo;<br>&nbsp; ProcessInfo &nbsp; &nbsp; &nbsp; &nbsp; : TProcessInformation;<br>&nbsp; Buffer &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: Pchar;<br>&nbsp; BytesRead &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; : DWord;<br>&nbsp; Apprunning &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;: DWord;<br>&nbsp;begin<br>&nbsp; With Security do begin<br>&nbsp; &nbsp;nlength &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= SizeOf(TSecurityAttributes);<br>&nbsp; &nbsp;binherithandle &nbsp; &nbsp; &nbsp; := true;<br>&nbsp; &nbsp;lpsecuritydescriptor := nil;<br>&nbsp; end;<br>&nbsp; if Createpipe (ReadPipe, WritePipe,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;@Security, 0) then begin<br>&nbsp; &nbsp;Buffer &nbsp;:= AllocMem(ReadBuffer + 1);<br>&nbsp; &nbsp;FillChar(Start,Sizeof(Start),#0);<br>&nbsp; &nbsp;start.cb &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= SizeOf(start);<br>&nbsp; &nbsp;start.hStdOutput &nbsp;:= WritePipe;<br>&nbsp; &nbsp;start.hStdInput &nbsp; := ReadPipe;<br>&nbsp; &nbsp;start.dwFlags &nbsp; &nbsp; := STARTF_USESTDHANDLES +<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp;start.wShowWindow := SW_HIDE;<br><br>&nbsp; &nbsp;if CreateProcess(nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; PChar(DosApp),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Security,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; @Security,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; true,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NORMAL_PRIORITY_CLASS,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ProcessInfo)<br>&nbsp; &nbsp;then<br>&nbsp; &nbsp;begin<br>&nbsp; &nbsp; repeat<br>&nbsp; &nbsp; &nbsp;Apprunning := WaitForSingleObject<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (ProcessInfo.hProcess,100);<br>&nbsp; &nbsp; &nbsp;Application.ProcessMessages;<br>&nbsp; &nbsp; until (Apprunning &lt;&gt; WAIT_TIMEOUT);<br>&nbsp; &nbsp; &nbsp;Repeat<br>&nbsp; &nbsp; &nbsp; &nbsp;BytesRead := 0;<br>&nbsp; &nbsp; &nbsp; &nbsp;ReadFile(ReadPipe,Buffer[0],<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ReadBuffer,BytesRead,nil);<br>&nbsp; &nbsp; &nbsp; &nbsp;Buffer[BytesRead]:= #0;<br>&nbsp; &nbsp; &nbsp; &nbsp;OemToAnsi(Buffer,Buffer);<br>&nbsp; &nbsp; &nbsp; &nbsp;AMemo.Text := AMemo.text + String(Buffer);<br>&nbsp; &nbsp; &nbsp;until (BytesRead &lt; ReadBuffer);<br>&nbsp; end;<br>&nbsp; FreeMem(Buffer);<br>&nbsp; CloseHandle(ProcessInfo.hProcess);<br>&nbsp; CloseHandle(ProcessInfo.hThread);<br>&nbsp; CloseHandle(ReadPipe);<br>&nbsp; CloseHandle(WritePipe);<br>&nbsp; end;<br>&nbsp;end;<br><br>&nbsp;begin {button 1 code}<br>&nbsp; &nbsp;RunDosInMemo('ping 10.43.0.1',Memo1);<br>&nbsp;end;
 
hehe,高级倒不怎么高级<br>其实是老师布置的作业,没办法阿:p<br>现在其他都可以解决,现在就剩下一个问题<br>就是不知道怎么获取和返回编译器编译后的信息
 
上面那位兄台的程序我基本上看不太懂(水平比较糟糕:)),但的确可行,我在<br>delphi5/winme环境下编译通过并且可以运行可以得到结果的确ms-dos环境下输入指令:<br>ping xxx.xxx.xxx.xxx的回显,同样我也将命令参数'ping 10.43.0.1'换成'ipconfig'<br>也通过并正确显示,但是当命令参数换成编译命令时:'d:/.../tpc.exe d:/.../x.pas'<br>确无法回显结果,我看不懂上面程序,所以也不知道为什么?<br>请教高手指点。。。
 
你试试看dcc32 d:/.../x.pas 正常。
 
To bubble:<br>&nbsp; 命令参数改为'dcc32 d:/.../*.pas'可以正确回显,我查找了dcc32.exe,这个可执行<br>文件路径为:'D:/Borland/Delphi5/Bin',应该是delphi中object pascal的编译程序,我想<br>这个编译程序应该是进注册表里,必须要调用注册表注册程序才可以在dos命令行标准输出?<br>(我想可能,应该是这样的)
 
因为Dcc32.exe是32位的程序,而tpc.exe是16位程序<br>winnt在运行16位程序时是作为Ntvdm进程运行的
 
后退
顶部