原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。http://fxh7622.blog.51cto.com/63841/529033
在很多时候,我们需要使用服务启动指定的应用程序来做到隐蔽启动程序的目的。
但是当我们直接使用Winexec来运行的时候,你会发现系统提示出错。以下的代码就是如何在Delphi编写的服务中启动指定的应用程序。
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 是你需要判断的应用程序在任务管理器中的名称。
如果你希望了解更多信息,欢迎登录“掰掰开发”论坛
http://fxh7622.blog.51cto.com/63841/529033