function
RunProcess
(const ProcessName: String): Boolean;
Var
siStartupInfo:STARTUPINFO;
saProcess,saThread:SECURITY_ATTRIBUTES;
piProcInfo:PROCESS_INFORMATION;
Hd:Cardinal;
ProcessHd:THandle;
Hds:THandle;
Str:String;
begin
Result:=False;
if not ExistFileName(ProcessName) then
begin
Exit;
end;
Var
siStartupInfo:STARTUPINFO;
saProcess,saThread:SECURITY_ATTRIBUTES;
piProcInfo:PROCESS_INFORMATION;
Hd:Cardinal;
ProcessHd:THandle;
Hds:THandle;
Str:String;
begin
Result:=False;
if not ExistFileName(ProcessName) then
begin
Exit;
end;
ProcessHd:=GetProcessHandleAsName( 'Explorer ');
if ProcessHd = 0 then Exit;
if OpenProcessToken(ProcessHd,TOKEN_ALL_ACCESS,Hds) then
begin
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;
Result:= CreateProcessAsUser (Hd,nil,PChar(ProcessName),nil,nil,false, CREATE_DEFAULT_ERROR_MODE,nil,nil,siStartupInfo,piProcInfo);
end;
end;
end;
if ProcessHd = 0 then Exit;
if OpenProcessToken(ProcessHd,TOKEN_ALL_ACCESS,Hds) then
begin
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;
Result:= CreateProcessAsUser (Hd,nil,PChar(ProcessName),nil,nil,false, CREATE_DEFAULT_ERROR_MODE,nil,nil,siStartupInfo,piProcInfo);
end;
end;
end;
参数:ProcessName是你需要启动的应用程序的绝对路径。
一般希望启动的时候被启动的应用程序没有运行。这时你就需要首先轮询判断此应用程序是否正在运行,以下代码就是判断你指定的应用程序是否在运行中。
function
IsExeRun
(const ExeName: String): Boolean;
var
ok: Bool;
ProcessID: Integer;
ProcessFullPath: String;
pProcess: PProcessInfo;
ProcessListHandle: THandle;
ProcessStruct: TProcessEntry32;
begin
//检测用户端是否正在运行
ProcessListHandle := CreateToolHelp32Snapshot (TH32CS_SNAPPROCESS, 0);
ProcessStruct.dwSize := Sizeof(ProcessStruct);
ok := Process32First(ProcessListHandle, ProcessStruct);
while Integer(ok) <> 0 do
begin
ProcessID:=ProcessStruct.th32ProcessID;
if UpperCase(Trim(ProcessStruct.szExeFile)) = UpperCase(Trim(ExeName)) then
begin
Result:=True;
Exit;
end;
ok := Process32Next(ProcessListHandle, ProcessStruct);
end;
end;
var
ok: Bool;
ProcessID: Integer;
ProcessFullPath: String;
pProcess: PProcessInfo;
ProcessListHandle: THandle;
ProcessStruct: TProcessEntry32;
begin
//检测用户端是否正在运行
ProcessListHandle := CreateToolHelp32Snapshot (TH32CS_SNAPPROCESS, 0);
ProcessStruct.dwSize := Sizeof(ProcessStruct);
ok := Process32First(ProcessListHandle, ProcessStruct);
while Integer(ok) <> 0 do
begin
ProcessID:=ProcessStruct.th32ProcessID;
if UpperCase(Trim(ProcessStruct.szExeFile)) = UpperCase(Trim(ExeName)) then
begin
Result:=True;
Exit;
end;
ok := Process32Next(ProcessListHandle, ProcessStruct);
end;
end;
其中参数 const ExeName: String 是你需要判断的应用程序在任务管理器中的名称。
如果你希望了解更多信息,欢迎登录“
掰掰开发”论坛
本文转自狗窝博客51CTO博客,原文链接http://blog.51cto.com/fxh7622/529033如需转载请自行联系原作者
fxh7622