2006. július 6., csütörtök

Give a process or thread administrator attributes

Problem/Question/Abstract:

I am trying to write a launcher program that can launch applications with administrator rights, kind of like the option in Windows 2000 to have an application "Run As" administrator. I think the way to do this is to use createprocess() function with something set in lpProcessAttributes or lpThreadAttributes that gives the process or thread administrator attributes. Unfortunatily, I'm not sure what to do beyond this. If anyone can give me answers as to what I can do next, I would be grateful.

Answer:

function CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword: PWideChar;
dwLogonFlags: dword; 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, Domain, Username, Password: string): Boolean;
var
StartupInfo: TStartupInfo;
ProcessInfo: TProcessInformation;
wFilename, wDomain, 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(wDomain, Length(Domain) * 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(Domain, wDomain, Length(Domain) * 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, wDomain, wPassword, 0, wFilename,
nil, 0, nil, nil, StartupInfo, ProcessInfo);
if Result then
begin
WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
CloseHandle(ProcessInfo.hProcess);
CloseHandle(ProcessInfo.hThread);
end
else
RaiseLastOSError;
FreeMem(wFilename);
FreeMem(wDomain);
FreeMem(wUsername);
FreeMem(wPassword);
end;


Nincsenek megjegyzések:

Megjegyzés küldése