Delphi 服務程式[7] 呼叫外部程式的方法
阿新 • • 發佈:2021-01-21
Delphi 服務程式[7] 呼叫外部程式的方法:
Function GetProcessHandleAsName(Name:String):THandle; //獲取程序控制代碼 Var Hd,Hs:THandle; dExit:Cardinal; Tmp,Tmp1:String; Lp:TProcessEntry32; begin Result:=0; Lp.dwSize:=sizeof(TProcessEntry32); Hd:=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0); if Process32First(Hd,Lp) then Repeat Tmp:=UpperCase(Trim(Name)); Tmp1:=Trim(UpperCase(Lp.szExeFile)); if AnsiPos(Tmp,Tmp1)>0 then begin Result:=OpenProcess(PROCESS_ALL_ACCESS,true,Lp.th32ProcessID); break; end Until Process32Next(Hd,Lp)=False; end; procedure CreateProc(const ProcessName:String); //建立/開啟外部程式 Var siStartupInfo:STARTUPINFO; saProcess,saThread:SECURITY_ATTRIBUTES; piProcInfo:PROCESS_INFORMATION; Hd:Cardinal; ProcessHd:THandle; Hds:THandle; Str:String; begin ProcessHd:=GetProcessHandleAsName('Explorer'); if ProcessHd=0 then Exit; if OpenProcessToken(ProcessHd,TOKEN_ALL_ACCESS,Hds) then if DuplicateTokenEx(Hds,TOKEN_ALL_ACCESS,nil,SecurityIdentification,TokenPrimary,Hd) then begin ZeroMemory(@siStartupInfo,sizeof(siStartupInfo)); siStartupInfo.cb:=sizeof(siStartupInfo); saProcess.nLength:=sizeof(saProcess); saProcess.lpSecurityDescriptor:=nil; saProcess.bInheritHandle:=false; saThread.nLength:=sizeof(saThread); saThread.lpSecurityDescriptor:=nil; saThread.bInheritHandle:=false; CreateProcessAsUser(Hd,nil,PChar(ProcessName),nil,nil,false, CREATE_DEFAULT_ERROR_MODE,nil,nil,siStartupInfo,piProcInfo); end; end;
建立時間:2021.01.21 更新時間: