在DELPHI中如何调用外部exe(100分)

  • 主题发起人 主题发起人 杨俊
  • 开始时间 开始时间
WinExec('Notepad.exe', sw_Normal);
 
本想说调用winapi的函数(WinExec),但有那么大虾在我之先,也罢,不在多言。
 
<br>&nbsp; &nbsp;uses WinProcs; &nbsp; <br><br>&nbsp; &nbsp;begin &nbsp; <br>&nbsp; &nbsp; WinExec('*.exe', WM_SHOW);<br><br>&nbsp; &nbsp;end. &nbsp;
 
WinExec足已!
 
procedure mycreateprocess;<br>var<br>&nbsp; &nbsp; &nbsp; &nbsp; t:TStartupInfo;<br>&nbsp; &nbsp; &nbsp; &nbsp; s:TProcessInformation;<br>&nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF Win32}<br>&nbsp; &nbsp; &nbsp; &nbsp; flag:boolean;<br>&nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF Win16}<br>&nbsp; &nbsp; &nbsp; &nbsp; flag1:integer;<br>&nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; t.cb:=Sizeof(STARTUPINFO);<br>&nbsp; &nbsp; &nbsp; &nbsp; //Specifies the size,in bytes,of the structure.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; t.dwFlags:=STARTF_USESHOWWINDOW;<br>&nbsp; &nbsp; &nbsp; &nbsp; //If this value is not specified,the wShowWindow member is ignored.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; t.wShowWindow:=SW_NORMAL;<br>&nbsp; &nbsp; &nbsp; &nbsp; //Activates and displays a window. If the window is minimized or maximized, Windows restores it to its original size and position. An application should specify this flag when displaying the window for the first time.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; t.cbReserved2:=0;<br>&nbsp; &nbsp; &nbsp; &nbsp; //Reserved;must be zero.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; t.lpReserved:=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; //Reserved.Set this member to nil before passing the structure to CreateProcess.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; t.lpReserved2:=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; //Reserved;must be nil.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; t.lpDesktop:=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; //Windows NT only: Points to a zero-terminated string that specifies either the name of the desktop only or the name of both the window station and desktop for this process.<br>&nbsp; &nbsp; &nbsp; &nbsp; //A backslash in the string pointed to by lpDesktop indicates that the string includes both desktop and window station names.<br>&nbsp; &nbsp; &nbsp; &nbsp; //Otherwise, the lpDesktop string is interpreted as a desktop name.<br>&nbsp; &nbsp; &nbsp; &nbsp; //If lpDesktop is NULL, the new process inherits the window station and desktop of its parent process.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; t.lpTitle:=nil;<br>&nbsp; &nbsp; &nbsp; &nbsp; //For console processes,<br>&nbsp; &nbsp; &nbsp; &nbsp; //this is the title displayed in the title bar if a new console window is created.<br>&nbsp; &nbsp; &nbsp; &nbsp; //If NULL, the name of the executable file is used as the window title instead.<br>&nbsp; &nbsp; &nbsp; &nbsp; //This parameter must be NULL for GUI or console processes that do not create a new console window.<br><br>&nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF Win32}<br>&nbsp; &nbsp; &nbsp; &nbsp; if(classify='0')then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag:=CreateProcess(Pchar(commandline),nil,nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,t,s);<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; flag:=CreateProcess(nil,Pchar(commandline),nil,nil,false,NORMAL_PRIORITY_CLASS,nil,nil,t,s);<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF Win16}<br>&nbsp; &nbsp; &nbsp; &nbsp; flag1:=WinExec(Pchar(commandline),SW_MAXIMIZE);<br>&nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>&nbsp; &nbsp; &nbsp; &nbsp; <br>&nbsp; &nbsp; &nbsp; &nbsp; {$IFDEF Win32}<br>&nbsp; &nbsp; &nbsp; &nbsp; if not(flag)then<br>&nbsp; &nbsp; &nbsp; &nbsp; begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('CreateProcess error!');<br>&nbsp; &nbsp; &nbsp; &nbsp; end<br>&nbsp; &nbsp; &nbsp; &nbsp; else begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; savelog('CreateProcess success!');<br>&nbsp; &nbsp; &nbsp; &nbsp; end;<br>&nbsp; &nbsp; &nbsp; &nbsp; {$ENDIF}<br>end;<br><br><br><br>createprocess中的第一个参数可用来执行application,第二个参数可用来执行命令行(如net stop w3svc)<br>不建义使用winexec,它是16位的,尽因为向后兼容才保留!!
 
看例子:<br>Delphi5/Demos/Doc/Filmanex<br><br>
 
看看以前的帖子呀你!<br>浪费100分!
 
使用CreateProcess或WinExec来做。<br>
 
老问题,请检索一下<br>
 
时间太久,强制结束。 &nbsp; wjiachun
 
什么都看不到
 
后退
顶部