奇怪的事(50分)

  • 主题发起人 主题发起人 kaka
  • 开始时间 开始时间
K

kaka

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);<br>VAR<br>&nbsp; &nbsp; ip,command &nbsp;: string ;<br>&nbsp; &nbsp; comline &nbsp; &nbsp; : pchar &nbsp;;<br>&nbsp; &nbsp; TEMP: TSTRINGLIST;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;ip := ' 120.23.34.124 ';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TEMP := TSTRINGLIST.Create;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ Shellexecute(handle, 'OPEN', PChar('ping &nbsp;120.23.34.124 '),nil, nil,sw_shownormal);}<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;command := 'COMMAND.COM /C nbtstat -A '+ip+' &gt;I:/ka.txt';<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;comline := pchar(command);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WinExec(comline, sw_hide);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TEMP.LoadFromFile('I:/ka.txt');<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF TEMP.Count &gt; 0 THEN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;IF POS('MAC',TEMP.Strings[TEMP.COUNT-1])&gt;0 THEN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SHOWMESSAGE(copy(TEMP.STRINGS[TEMP.COUNT-1],15,17));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;END;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;TEMP.FREE;<br><br>end;<br><br>end.<br>这是一个读网卡mac的program,运行后,关了以后,<br>改ka.txt成h.txt,program中有ka.txt成h.txt,run就说找不到h.txt,<br>但save退出delphi再run就ok了.<br><br>
 
不应该啊。
 
在WinExec之后要给出足够的时间等待程序结束
 
Change to createprocess and wait until the process stops
 
cj:<br>&nbsp; &nbsp;你能帮我写一个用createprocess改写这个program,<br>我愿再出100分.
 
我前几天写了一大段CreateProcess及其用法, 你看一下问题:<br>http://www.gislab.ecnu.edu.cn/delphibbs/DispQ.asp?LID=107419
 
hehe,参考huizhang的程序,胡乱改改<br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var &nbsp;bCreateProcess: boolean;<br>&nbsp; &nbsp; &nbsp;lpStartupInfo: TStartupInfo;<br>&nbsp; &nbsp; &nbsp;ip,command &nbsp;: string ;<br>&nbsp; &nbsp; &nbsp;comline &nbsp; &nbsp; : pchar &nbsp;;<br>&nbsp; &nbsp; &nbsp;TEMP: TSTRINGLIST;<br>begin<br>&nbsp; ip := ' 120.23.34.124 ';<br>&nbsp; TEMP := TSTRINGLIST.Create;<br>&nbsp; command := 'COMMAND.COM /C nbtstat -A '+ip+' &gt;I:/ka.txt';<br>&nbsp; comline := pchar(command);<br>&nbsp; Button1.Enabled := false; &nbsp;// 填入 StartupInfo<br>&nbsp; FillChar(lpStartupInfo, Sizeof(TStartupInfo), #0);<br>&nbsp; lpStartupInfo.cb := Sizeof(TStartupInfo);<br>&nbsp; lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; lpStartupInfo.wShowWindow := SW_NORMAL;<br>&nbsp; bCreateProcess := CreateProcessA(nil,ComLine,<br>&nbsp; &nbsp; nil, nil, True, NORMAL_PRIORITY_CLASS, nil,<br>&nbsp; &nbsp; nil, lpStartupInfo, lpProcessInformation);<br>&nbsp; if bCreateProcess then<br>&nbsp; begin<br>&nbsp; &nbsp; WaitForInputIdle(lpProcessInformation.hProcess, 60000);<br>&nbsp; &nbsp; memo1.Lines.Add(format('hProcess: %d', [lpProcessInformation.hProcess]));<br>&nbsp; &nbsp; memo1.lines.add(format('hThread: %d', [lpProcessInformation.hThread]));<br>&nbsp; &nbsp; memo1.lines.add(format('dwProcessID: %u', [lpProcessInformation.dwProcessID]));<br>&nbsp; &nbsp; memo1.lines.add(format(' dwThreadID: %u', [lpProcessInformation.dwThreadId]));<br>&nbsp; end;<br>&nbsp; TEMP.LoadFromFile('I:/ka.txt');<br>&nbsp; IF TEMP.Count &gt; 0 THEN<br>&nbsp; BEGIN<br>&nbsp; &nbsp; &nbsp;IF POS('MAC',TEMP.Strings[TEMP.COUNT-1])&gt;0 THEN<br>&nbsp; &nbsp; &nbsp; &nbsp; SHOWMESSAGE(copy(TEMP.STRINGS[TEMP.COUNT-1],15,17));<br>&nbsp; END;<br>&nbsp; TEMP.FREE;<br>end;<br>
 
改了程序;<br>但还是老样<br>procedure TForm1.Button1Click(Sender: TObject);<br><br>var<br>&nbsp; &nbsp; &nbsp;bCreateProcess : Boolean;<br>&nbsp; &nbsp; &nbsp;lpProcessInformation : TProcessInformation;<br>&nbsp; &nbsp; &nbsp;lpStartupInfo: TStartupInfo;<br>&nbsp; &nbsp; &nbsp;ip,command &nbsp;: string ;<br>&nbsp; &nbsp; &nbsp;comline &nbsp; &nbsp; : pchar &nbsp;;<br>&nbsp; &nbsp; &nbsp;TEMP: TSTRINGLIST;<br>begin<br>&nbsp; &nbsp; &nbsp; ip := ' 159.26.134.49 ';<br>&nbsp; &nbsp; &nbsp; TEMP := TSTRINGLIST.Create;<br>&nbsp; &nbsp; &nbsp; command := 'COMMAND.COM /C nbtstat -A '+ip+' &gt;I:/ag.txt';<br>&nbsp; &nbsp; &nbsp; comline := pchar(command);<br>&nbsp; &nbsp; &nbsp; Button1.Enabled := false; &nbsp;// 填入 StartupInfo<br>&nbsp; &nbsp; &nbsp; FillChar(lpStartupInfo, Sizeof(TStartupInfo), #0);<br>&nbsp; &nbsp; &nbsp; lpStartupInfo.cb := Sizeof(TStartupInfo);<br>&nbsp; &nbsp; &nbsp; lpStartupInfo.dwFlags := STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; &nbsp; lpStartupInfo.wShowWindow := SW_NORMAL;<br>&nbsp; &nbsp; &nbsp; &nbsp;bCreateProcess := CreateProcessA(nil,ComLine,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, nil, True, NORMAL_PRIORITY_CLASS, nil,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;nil, lpStartupInfo, lpProcessInformation);<br>&nbsp; &nbsp; &nbsp; &nbsp;if bCreateProcess then<br>&nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WaitForInputIdle(lpProcessInformation.hProcess, 60000);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.Lines.Add(format('hProcess: %d', [lpProcessInformation.hProcess]));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.add(format('hThread: %d', [lpProcessInformation.hThread]));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.add(format('dwProcessID: %u', [lpProcessInformation.dwProcessID]));<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;memo1.lines.add(format(' dwThreadID: %u', [lpProcessInformation.dwThreadId]));<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp;TEMP.LoadFromFile('I:/ag.txt');<br>&nbsp; &nbsp; &nbsp; &nbsp; IF TEMP.Count &gt; 0 THEN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;BEGIN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IF POS('MAC',TEMP.Strings[TEMP.COUNT-1])&gt;0 THEN<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SHOWMESSAGE(copy(TEMP.STRINGS[TEMP.COUNT-1],15,17));<br>&nbsp; &nbsp; &nbsp; &nbsp; END;<br>&nbsp; &nbsp; &nbsp; &nbsp;TEMP.FREE;<br>end;<br><br>procedure TForm1.Button2Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp; Application.Terminate;<br>end;<br>compile-run-pass:<br>我把出现ag.txt 改为 df.txt ,save-compile-run:<br>说找不到 df.txt ,Program reset-save-compile-run<br>就ok了.or退出delphi再open project,compile-run也ok.<br>大侠可以把ip改为自己local net 随便那一台machine ip(except host). <br>try <br>
 
已经有人写了,最近看到一个构件,贴出来:<br>unit process;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;<br><br>type<br><br><br>&nbsp; TShowWindow = ( swHide , swMaximize , swMinimize , swRestore , swShow ,<br>&nbsp; &nbsp; swShowDefault , swShowMaximized , swShowMinimized ,<br>&nbsp; &nbsp; swShowMinNoActive , swShowNA , swShowNoActivate , swShowNormal );<br><br>&nbsp; TProcessEvent = procedure ( Sender : TObject; ExitCode : DWord ) of object;<br><br>&nbsp; TProcess = class(TComponent)<br>&nbsp; private<br>&nbsp; &nbsp; FCommand : string;<br>&nbsp; &nbsp; FShowWindow : TShowWindow;<br>&nbsp; &nbsp; FDirectory : string;<br>&nbsp; &nbsp; FWait : Boolean;<br>&nbsp; &nbsp; FOnFinished : TProcessEvent;<br>&nbsp; protected<br>&nbsp; public<br>&nbsp; &nbsp; constructor Create( AOwner : TComponent );<br>&nbsp; &nbsp; procedure Execute;<br>&nbsp; published<br>&nbsp; &nbsp; property Command : string read FCommand write FCommand;<br>&nbsp; &nbsp; property Directory : string read FDirectory write FDirectory;<br>&nbsp; &nbsp; property ShowWindow : TShowWindow read FShowWindow write FShowWindow;<br>&nbsp; &nbsp; property Wait : Boolean read FWait write FWait;<br>&nbsp; &nbsp; property OnFinished : TProcessEvent read FOnFinished write FOnFinished;<br>&nbsp; end;<br><br>procedure Register;<br><br>implementation<br><br>const ShowWindowValues : array [ 0..11 ] of integer =<br>&nbsp; ( sw_Hide , sw_Maximize , sw_Minimize , sw_Restore , sw_Show ,<br>&nbsp; &nbsp; sw_ShowDefault , sw_ShowMaximized , sw_ShowMinimized ,<br>&nbsp; &nbsp; sw_ShowMinNoActive , sw_ShowNA , sw_ShowNoActivate , sw_ShowNormal );<br><br>type<br><br>&nbsp; TProcessThread = class( TThread )<br>&nbsp; private<br>&nbsp; &nbsp; FProcess : TProcess;<br>&nbsp; protected<br>&nbsp; &nbsp; procedure Execute; override;<br>&nbsp; public<br>&nbsp; &nbsp; constructor CreateThread( Process : TProcess );<br>&nbsp; end;<br><br><br>procedure Register;<br>begin<br>&nbsp; RegisterComponents('Samples', [TProcess]);<br>end;<br><br>constructor TProcess.Create( AOwner : TComponent );<br>begin<br>&nbsp; inherited Create( AOwner );<br>&nbsp; FShowWindow := swShowNormal;<br>&nbsp; FWait := False; <br>end;<br><br>procedure TProcess.Execute;<br>begin<br>&nbsp; TProcessThread.CreateThread( Self );<br>end;<br><br>//------------------------------------------------------------------------------<br>// &nbsp;TProcessThread<br><br>constructor TProcessThread.CreateThread( Process : TProcess );<br>begin<br>&nbsp; inherited Create( True );<br>&nbsp; FProcess := Process;<br>&nbsp; FreeOnTerminate := True;<br>&nbsp; Resume;<br>end;<br><br>procedure TProcessThread.Execute;<br>var<br>&nbsp; StartupInfo : TStartupInfo;<br>&nbsp; ProcessInfo : TProcessInformation;<br>&nbsp; ExitCode : DWord;<br>&nbsp; Directory : PChar;<br>begin<br>&nbsp; FillChar( StartupInfo , SizeOf( StartupInfo ) , 0 );<br>&nbsp; with StartupInfo do<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; cb := SizeOf( StartupInfo );<br>&nbsp; &nbsp; &nbsp; dwFlags := startf_UseShowWindow;<br>&nbsp; &nbsp; &nbsp; wShowWindow := ShowWindowValues[ Ord( FProcess.ShowWindow ) ];<br>&nbsp; &nbsp; end;<br>&nbsp; ExitCode := 0;<br>&nbsp; FProcess.Directory := Trim( FProcess.Directory );<br>&nbsp; if Length( FProcess.Directory ) = 0 then<br>&nbsp; &nbsp; Directory := nil<br>&nbsp; else<br>&nbsp; &nbsp; Directory := PChar( FProcess.Directory );<br><br>&nbsp; if CreateProcess( nil , PChar( FProcess.Command ) , nil , nil , False ,<br>&nbsp; &nbsp; NORMAL_PRIORITY_CLASS , nil , Directory ,<br>&nbsp; &nbsp; StartupInfo , ProcessInfo ) then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; if FProcess.Wait then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WaitForSingleObject( ProcessInfo.hProcess , Infinite );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; GetExitCodeProcess( ProcessInfo.hProcess , ExitCode );<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if Assigned( FProcess.FOnFinished ) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FProcess.FOnFinished( FProcess , ExitCode );<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; CloseHandle( ProcessInfo.hProcess );<br>&nbsp; &nbsp; &nbsp; CloseHandle( ProcessInfo.hThread );<br>&nbsp; &nbsp; end;<br>end;<br><br>end.<br>
 
我是过程序了,可以的呀,不过我的txt里面是host not found
 
你可以把ip改成自己的ip,再把command改成<br>command := 'COMMAND.COM /C winipcfg -A '+ip+' &gt;I:/ag.txt'<br>关键要像我的步骤来,就会出现怪事。
 
还有大虾对这个问题有意见吗?<br><br>晓茶在网吧上网,看到此类问题干着急. &nbsp;:-))
 
接受答案了.
 

Similar threads

后退
顶部