Advertisement
Guest User

Untitled

a guest
May 3rd, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 3.86 KB | None | 0 0
  1. unit uty2;
  2.  
  3. interface
  4. uses
  5.   Windows, Classes, SysUtils, Dialogs;
  6.  
  7. type
  8.  TProfileInfo = record
  9.     dwSize : DWORD;
  10.     dwFlags : DWORD;
  11.     lpUserName : LPTSTR;
  12.     lpProfilePath : LPTSTR;
  13.     lpDefaultPath : LPTSTR;
  14.     lpServerName : LPTSTR;
  15.     lpPolicyPath : LPTSTR;
  16.     hProfile : THandle;
  17.  end;
  18.  
  19. function RunAs(User, Password, Command: String): DWORD;
  20. implementation
  21.  
  22. const
  23.   PI_NOUI        = 1;     // Prevents displaying of messages
  24.   PI_APPLYPOLICY = 2;     // Apply NT4 style policy
  25.  
  26. function CreateProcessWithLogon(lpUsername: PWideChar;
  27.                                 lpDomain: PWideChar;
  28.                                 lpPassword: PWideChar;
  29.                                 dwLogonFlags: DWORD;
  30.                                 lpApplicationName: PWideChar;
  31.                                 lpCommandLine: PWideChar;
  32.                                 dwCreationFlags: DWORD;
  33.                                 lpEnvironment: Pointer;
  34.                                 lpCurrentDirectory: PWideChar;
  35.                                 var lpStartupInfo: TStartupInfo;
  36.                                 var lpProcessInfo: TProcessInformation): BOOL; stdcall;
  37.                                 external 'advapi32' name 'CreateProcessWithLogonW';
  38.  
  39. function CreateEnvironmentBlock(var lpEnvironment: Pointer;
  40.                                 hToken: THandle;
  41.                                 bInherit: BOOL): BOOL; stdcall; external 'userenv';
  42.  
  43. function DestroyEnvironmentBlock(pEnvironment: Pointer): BOOL; stdcall; external 'userenv';
  44.  
  45. const
  46.   LOGON_WITH_PROFILE   =  $00000001;
  47.  
  48. function LoadUserProfile (hToken : THandle; var profileInfo : TProfileInfo) : BOOL; stdcall;
  49.  
  50. function RunAs(User, Password, Command: String): DWORD;
  51. var  dwSize:        DWORD;
  52.      hToken:        THandle;
  53.      lpvEnv:        Pointer;
  54.      pi:            TProcessInformation;
  55.      si:            TStartupInfo;
  56.      szPath:        Array [0..MAX_PATH] of WideChar;
  57.      fProfileLoaded : boolean;
  58.      fProfileInfo : TProfileInfo;
  59. begin
  60.   ZeroMemory(@szPath, SizeOf(szPath));
  61.   ZeroMemory(@pi, SizeOf(pi));
  62.   ZeroMemory(@si, SizeOf(si));
  63.   si.cb:=SizeOf(TStartupInfo);
  64.   showmessage('1');
  65.   if LogonUser(PChar(User), nil, PChar(Password), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hToken) then
  66.   begin
  67.      ZeroMemory (@fProfileInfo, sizeof (fProfileInfo));
  68.      fProfileInfo.dwSize := sizeof (fProfileInfo);
  69.      fProfileInfo.lpUserName := PChar (user);
  70.      fProfileInfo.dwFlags := PI_APPLYPOLICY;
  71.      fprofileLoaded := LoadUserProfile (hToken, fProfileInfo);
  72.      showmessage('1.1');
  73.      if CreateEnvironmentBlock(lpvEnv, hToken, True) then
  74.      begin
  75.         showmessage('2');
  76.         dwSize:=SizeOf(szPath) div SizeOf(WCHAR);
  77.         if (GetCurrentDirectoryW(dwSize, @szPath) > 0) then
  78.         begin
  79.            if (CreateProcessWithLogon(PWideChar(WideString(User)), nil, PWideChar(WideString(Password)),
  80.                LOGON_WITH_PROFILE, nil, PWideChar(WideString(Command)), CREATE_UNICODE_ENVIRONMENT,
  81.                lpvEnv, szPath, si, pi)) then
  82.            begin
  83.               showmessage('3');
  84.               result:=integer(ERROR_SUCCESS);
  85.               showmessage(inttostr(ERROR_SUCCESS));
  86.               WaitForSingleObject(pi.hProcess, INFINITE);
  87.               CloseHandle(pi.hProcess);
  88.               CloseHandle(pi.hThread);
  89.            end
  90.            else
  91.               result:=GetLastError;
  92.         end
  93.         else
  94.            result:=GetLastError;
  95.         DestroyEnvironmentBlock(lpvEnv);
  96.      end
  97.      else
  98.      begin
  99.         result:=GetLastError;
  100.         SysErrorMessage(GetLastError);
  101.         showmessage('GetLastError 1:'+inttostr(result));
  102.      end;
  103.      CloseHandle(hToken);
  104.   end
  105.   else
  106.   begin
  107.      result:=GetLastError;
  108.      SysErrorMessage(GetLastError);
  109.      showmessage('GetLastError 2:'+inttostr(result));
  110.   end;
  111. end;
  112.  
  113. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement