Advertisement
Guest User

Untitled

a guest
May 1st, 2017
67
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.  
  60. begin
  61.  
  62.   ZeroMemory(@szPath, SizeOf(szPath));
  63.   ZeroMemory(@pi, SizeOf(pi));
  64.   ZeroMemory(@si, SizeOf(si));
  65.   si.cb:=SizeOf(TStartupInfo);
  66.   showmessage('1');
  67.   if LogonUser(PChar(User), nil, PChar(Password), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, hToken) then
  68.   begin
  69.      ZeroMemory (@fProfileInfo, sizeof (fProfileInfo));
  70.      fProfileInfo.dwSize := sizeof (fProfileInfo);
  71.      fProfileInfo.lpUserName := PChar (user);
  72.      fProfileInfo.dwFlags := PI_APPLYPOLICY;
  73.      fprofileLoaded := LoadUserProfile (hToken, fProfileInfo);
  74.      showmessage('1.1');
  75.      if CreateEnvironmentBlock(lpvEnv, hToken, True) then
  76.      begin
  77.         showmessage('2');
  78.         dwSize:=SizeOf(szPath) div SizeOf(WCHAR);
  79.         if (GetCurrentDirectoryW(dwSize, @szPath) > 0) then
  80.         begin
  81.            if (CreateProcessWithLogon(PWideChar(WideString(User)), nil, PWideChar(WideString(Password)),
  82.                LOGON_WITH_PROFILE, nil, PWideChar(WideString(Command)), CREATE_UNICODE_ENVIRONMENT,
  83.                lpvEnv, szPath, si, pi)) then
  84.            begin
  85.               showmessage('3');
  86.               result:=integer(ERROR_SUCCESS);
  87.               showmessage(inttostr(ERROR_SUCCESS));
  88.               WaitForSingleObject(pi.hProcess, INFINITE);
  89.               CloseHandle(pi.hProcess);
  90.               CloseHandle(pi.hThread);
  91.            end
  92.            else
  93.               result:=GetLastError;
  94.         end
  95.         else
  96.            result:=GetLastError;
  97.         DestroyEnvironmentBlock(lpvEnv);
  98.      end
  99.      else
  100.      begin
  101.         result:=GetLastError;
  102.         SysErrorMessage(GetLastError);
  103.         showmessage('GetLastError 1:'+inttostr(result));
  104.      end;
  105.      CloseHandle(hToken);
  106.   end
  107.   else
  108.   begin
  109.      result:=GetLastError;
  110.      SysErrorMessage(GetLastError);
  111.      showmessage('GetLastError 2:'+inttostr(result));
  112.   end;
  113. end;
  114.  
  115. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement