5,392
社区成员
发帖
与我相关
我的任务
分享
{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;
Function CreateProc(ProcessName:String):boolean;
Var
siStartupInfo:STARTUPINFO;
saProcess,saThread:SECURITY_ATTRIBUTES;
piProcInfo:PROCESS_INFORMATION;
Hd:NativeInt;
ProcessHd:THandle;
Hds:THandle;
Str:String;
begin
Result := false;
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;
if CreateProcessAsUser(Hd,nil,PChar(ProcessName),nil,nil,false,
CREATE_DEFAULT_ERROR_MODE,nil,nil,siStartupInfo,piProcInfo) then
Result := true;
end;
end;}
function CreateProcessWithLogonW(
lpUsername, // user's name
lpDomain, // user's domain
lpPassword:PWideChar; // user's password
dwLogonFlags:dword; // logon option
lpApplicationName: PWideChar;
lpCommandLine: PWideChar;
dwCreationFlags: DWORD;
lpEnvironment: Pointer;
lpCurrentDirectory: PWideChar;
const lpStartupInfo: tSTARTUPINFO;
var lpProcessInformation: TProcessInformation
): BOOL; stdcall;external 'advapi32.dll';
Function RunAsUser(const filename,username,password:string):boolean;
var
StartupInfo: tStartupInfo;
ProcessInfo: TProcessInformation;
wfilename,wusername,wpassword:pwidechar;
begin
FillChar (StartupInfo, SizeOf(StartupInfo), #0);
StartupInfo.cb := SizeOf(StartupInfo);
StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
StartupInfo.wShowWindow := SW_SHOWNORMAL;
GetMem( wfilename, Length( filename ) * SizeOf( WideChar ) + SizeOf(
WideChar ) ) ;
GetMem( wusername, length(username) * SizeOf( WideChar ) + SizeOf(
WideChar ) ) ;
GetMem( wpassword, length(password) * SizeOf( WideChar ) + SizeOf(
WideChar ) ) ;
StringToWideChar( filename, wfilename, Length( filename ) * SizeOf(
WideChar ) + SizeOf( WideChar ) ) ;
StringToWideChar( username, wusername, Length( username ) * SizeOf(
WideChar ) + SizeOf( WideChar ) ) ;
StringToWideChar( password, wpassword, Length( password ) * SizeOf(
WideChar ) + SizeOf( WideChar ) ) ;
result:=CreateProcessWithLogonW(wusername,nil,wpassword,0, wfilename, nil,
0, nil, nil,StartupInfo , ProcessInfo);
freemem(wfilename);
freemem(wusername);
freemem(wpassword);
end;
RunAsUser(strPath,'user','pass');//这里填写操作系统的用户名与密码
这个方法不行,不能启动程序。