delphi 系统服务运行桌面用户指定程序


本文整理自网络,侵删。

 
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;
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;

相关阅读 >>

Delphi使用idhttp 获取 httpsurl内容

Delphi android路径 tpath 文件路径,文件管理

Delphi 建立快捷方式

Delphi toscilloscope 仿windows任务管理器cpu使用记录组件

Delphi xe5如何像手机qq一样最小化后显示图标

Delphi 非常简单的字符串加密解密函数,支持中文

Delphi firedac 下的 sqlite [12] - 备忘录

Delphi 判断com端口是否存在

delph做的百叶窗小程序

Delphi exe图标替换

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...