掲示板システム
ホーム
アクセス解析
カテゴリ
ログアウト
ShellExecuteで実行したプロセスを終了させるには (ID:8620)
名前
ホームページ(ブログ、Twitterなど)のURL (省略可)
本文
こんばんわです。 力技ですが。 var Form1: TForm1; hMyAppWnd : HWND; implementation {$R *.dfm} function EnumWindowsProc(hwnd : THandle; lParam : longint):BOOL; stdcall; var dwProcessId : integer; begin Result := true; if (GetWindow(hwnd, GW_OWNER) = 0) and IsWindowVisible(hwnd) then begin GetWindowThreadProcessId(hwnd,@dwProcessId); if dwProcessId=lParam then begin hMyAppWnd := hwnd; Result := false; end; end; end; function exttoExepath(ext:string):string; var MyReg:TRegistry; RleName:string; ExePath:String; begin result:=''; MyReg:=TRegistry.Create; MyReg.RootKey:=HKEY_CLASSES_ROOT; try If not MyReg.OpenKey(ext,false) then ; RleName:=MyReg.ReadString(''); if not MyReg.OpenKey( '\' + RleName+ '\Shell\Open\Command',false) then ; ExePath:=MyReg.ReadString(''); result:=ExePath; finally MyReg.Free; end; end; procedure TForm1.ShellExecuteTx(path:string); var StInfo : TStartupInfo; PrInfo : TProcessInformation; appPath:string; SystemDir: array[0..MAX_PATH - 1] of Char; begin //こんなんじゃ実用に耐えませんので練りこんでください //----------------------------------------------------------- GetWindowsDirectory(SystemDir,Sizeof(SystemDir)); appPath:=ExtractFileExt(path); appPath:=exttoExepath(appPath); appPath:=StringReplace(appPath,'%SystemRoot%',SystemDir,[]); appPath:=StringReplace(appPath,' %1','',[]); appPath:=appPath+' '+path; //----------------------------------------------------------- GetStartupInfo(StInfo); CreateProcess(nil, PChar(appPath), nil, nil, False, CREATE_DEFAULT_ERROR_MODE, nil, PChar(GetCurrentDir), StInfo, PrInfo); WaitForInputIdle(PrInfo.hProcess, INFINITE ); EnumWindows(@EnumWindowsProc, PrInfo.dwProcessId); end; procedure TForm1.Button1Click(Sender: TObject); begin ShellExecuteTx(ExtractFilePath(Application.ExeName)+'test.txt'); end; procedure TForm1.Button2Click(Sender: TObject); begin SendMessage(hMyAppWnd, WM_CLOSE, 0, 0); end; 「ShellExecuteExでプロセス終了」の方はわかりません。
←解決時は質問者本人がここをチェックしてください。
戻る
掲示板システム
Copyright 2021 Takeshi Okamoto All Rights Reserved.