2010年11月5日星期五

关于CreateProcessAsUser的问题(转)

摘自:http://topic.csdn.net/t/20030912/17/2253782.html

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;

没有评论: