DelphiでEXEファイルを呼び出す、最も簡単で分かりやすい方法.

3351 ワード

私たちがよく使う関数は2つあります.WinExecとShellExecuteです.
1)WinExec関数(WinProcsユニットに属する)・宣言形式UNIT WinExec(LPCSTR lpCmdLine,UINT uCmdShow)を使用する.[例]
var  SDir:string;    

SetLength(SDir,144);    

GetWindowsDirectory(PChar(SDir),144);    

SetLength(SDir,StrLen(PChar(SDir)));    

SDir:=SDir+'
otepad.exe'+' '+savedialog1.FileName; WinExec(PChar(SDir), SW_SHOWMAXIMIZED);

注意:SDirが有効なパスでない場合、エラーは表示されません.[例]winexec('command.com/c copy*.*c:',SW_Normal);[例]winexec('start abc.txt'); 
//     ,       OK   

procedure  TForm1.Button1Click(Sender:  TObject);  

var  

   S:  String;  

begin  

   if  OpenDialog1.Execute  then  

   begin  

       s  :=  OpenDialog1.FileName;  

       WinExec(  PChar(s),  SW_NORMAL);  

   end;  

end;  

--------------------------------------------------------------- 
2)ShellExecute関数(ShellAPIユニットに属する)を使用するそのいくつかのパラメータ:・hwnd:フォームのハンドル;・lpOperation:プログラムを開いて実行する操作、共に“open”、“explore”、“print”の3種類の方式を保留して、この参数は省略することができて、この時開いたファイルによって(lpFile)のタイプは、例えば、lpFileがテキストファイルである場合、そのファイルに関連付けられたプログラムで開く・lpFile:ファイル名;lpParamerters:ファイルを開くために必要なパラメータ;lpDirectory:ファイル名が存在するパス、もちろん一般的にWindowsに登録されているパスプログラム(WinWordなど)はこのパラメータを提供する必要はありません.・nShowCmd:ファイルを開いた後にプログラムフォームがどのように表示されるか.(1)実行可能ファイルを実行する[例]は「メモ帳」を例にとります
procedure  TForm1.OpenBtnClick(Sender:TObject);      

begin  
ShellExecute(handle,'open','notepad.exe',nil,nil,SW_ShowNormal);    

end;   


(2)また,ShellExeCute()はリンクネットワークを行うことも可能である.[例]
procedure  TForm1.HttpClick(Sender:  TObject);      

begin      

   ShellExecute(handle,'open','http://liangming.163.net',  nil,nil,SW_ShowNormal);      

end;  

(3)Windowsに登録されている外部ファイルを開くWindowsのレジストリに登録されているファイルがあれば、まずusesセクションに追加する:uses Shellapi;次に呼び出す場所で使用するプロセス
procedure  URLink(URL:PChar);    

begin    

   ShellExecute(0,  nil,  URL,  nil,  nil,  SW_NORMAL);    

end;  
を定義する:URLink('Readme.txt');ホームページにリンクするには:URLink('http://vortex.yeah.net');メールを送るには、メールアドレスの前にmailto URLink('mailto:[email protected]');外部実行プログラムを開く場合は、直接呼び出すこともできます.  --------------------------------------------------------------- 
function  RunProgram(ProgramName:string;Wait:Boolean=False):Cardinal;  

var  

   StartInfo:STARTUPINFO;  

   ProcessInfo:PROCESS_INFORMATION;  

begin  

//      ,    0,          

   Result:=0;  

   if  ProgramName=''  then  exit;  

   GetStartupInfo(StartInfo);  

   StartInfo.dwFlags:=StartInfo.dwFlags  or  STARTF_FORCEONFEEDBACK;  

   if  not  CreateProcess(nil,PChar(ProgramName),nil,nil,false,0,  

                   nil,nil,StartInfo,ProcessInfo)  then    exit;  

   Result:=ProcessInfo.hProcess;  

   //        

   //           

   if  not  wait  then  exit;  

   while  IsProgram_Runing(Result)  do  Application.ProcessMessages;  

end;  

function  IsProgram_Runing(hProcess:Cardinal):Boolean;  

var  

   ExitCode:Cardinal;  

begin  

   //            

   GetExitCodeProcess(hProcess,ExitCode);  

   if  ExitCode=STILL_ACTIVE  then  

       Result:=True  

   else  

       Result:=False;  

end;