Author: Lou Adler
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:
1
2 function CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword: PWideChar;
3 dwLogonFlags: dword; lpApplicationName: PWideChar; lpCommandLine: PWideChar;
4 dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: PWideChar;
5 const lpStartupInfo: TStartupInfo; var lpProcessInformation: TProcessInformation):
6 BOOL; stdcall; external 'advapi32.dll';
7
8 function RunAsUser(const Filename, Domain, Username, Password: string): Boolean;
9 var
10 StartupInfo: TStartupInfo;
11 ProcessInfo: TProcessInformation;
12 wFilename, wDomain, wUsername, wPassword: PWideChar;
13 begin
14 FillChar(StartupInfo, SizeOf(StartupInfo), #0);
15 StartupInfo.cb := SizeOf(StartupInfo);
16 StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
17 StartupInfo.wShowWindow := SW_SHOWNORMAL;
18 GetMem(wFilename, Length(Filename) * SizeOf(WideChar) + SizeOf(WideChar));
19 GetMem(wDomain, Length(Domain) * SizeOf(WideChar) + SizeOf(WideChar));
20 GetMem(wUsername, length(Username) * SizeOf(WideChar) + SizeOf(WideChar));
21 GetMem(wPassword, length(Password) * SizeOf(WideChar) + SizeOf(WideChar));
22 StringToWideChar(Filename, wFilename, Length(Filename) * SizeOf(WideChar)
23 + SizeOf(WideChar));
24 StringToWideChar(Domain, wDomain, Length(Domain) * SizeOf(WideChar)
25 + SizeOf(WideChar));
26 StringToWideChar(Username, wUsername, Length(Username) * SizeOf(WideChar)
27 + SizeOf(WideChar));
28 StringToWideChar(Password, wPassword, Length(Password) * SizeOf(WideChar)
29 + SizeOf(WideChar));
30 Result := CreateProcessWithLogonW(wUsername, wDomain, wPassword, 0, wFilename,
31 nil, 0, nil, nil, StartupInfo, ProcessInfo);
32 if Result then
33 begin
34 WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
35 CloseHandle(ProcessInfo.hProcess);
36 CloseHandle(ProcessInfo.hThread);
37 end
38 else
39 RaiseLastOSError;
40 FreeMem(wFilename);
41 FreeMem(wDomain);
42 FreeMem(wUsername);
43 FreeMem(wPassword);
44 end;
|