Thema: Delphi RunAs für Windows 2000

Einzelnen Beitrag anzeigen

Benutzerbild von kuba
kuba

Registriert seit: 26. Mai 2006
Ort: Arnsberg
588 Beiträge
 
Delphi 11 Alexandria
 
#1

RunAs für Windows 2000

  Alt 15. Jul 2008, 22:30
Hy,

funktioniert RunAs(User,Password,Command) unter Windows 2000 ?

Habe folgenden Code gefunden der unter WinXP funktioniert - jedoch nicht unter Win2K :

Delphi-Quellcode:
function CreateProcessWithLogon(lpUsername: PWideChar;
                                lpDomain: PWideChar;
                                lpPassword: PWideChar;
                                dwLogonFlags: DWORD;
                                lpApplicationName: PWideChar;
                                lpCommandLine: PWideChar;
                                dwCreationFlags: DWORD;
                                lpEnvironment: Pointer;
                                lpCurrentDirectory: PWideChar;
                                var lpStartupInfo: TStartupInfo;
                                var lpProcessInfo: TProcessInformation): BOOL; stdcall;
                                external 'advapi32name 'CreateProcessWithLogonW';

function CreateEnvironmentBlock(var lpEnvironment: Pointer;
                                hToken: THandle;
                                bInherit: BOOL): BOOL; stdcall; external 'userenv';

function DestroyEnvironmentBlock(pEnvironment: Pointer): BOOL; stdcall;
external 'userenv';

const
  LOGON_WITH_PROFILE = $00000001;

{------------
Emulate the RunAs function
--------------}


function RunAs(User, Password, Command: String): Integer;
var dwSize: DWORD;
     hToken: THandle;
     lpvEnv: Pointer;
     pi: TProcessInformation;
     si: TStartupInfo;
     szPath: Array [0..MAX_PATH] of WideChar;
begin

  ZeroMemory(@szPath, SizeOf(szPath));
  ZeroMemory(@pi, SizeOf(pi));
  ZeroMemory(@si, SizeOf(si));
  si.cb:=SizeOf(TStartupInfo);

  if LogonUser(PChar(User), nil, PChar(Password), LOGON32_LOGON_INTERACTIVE,
LOGON32_PROVIDER_DEFAULT, hToken) then
  begin
     if CreateEnvironmentBlock(lpvEnv, hToken, True) then
     begin
        dwSize:=SizeOf(szPath) div SizeOf(WCHAR);
        if (GetCurrentDirectoryW(dwSize, @szPath) > 0) then
        begin
           if (CreateProcessWithLogon(PWideChar(WideString(User)), nil,
               PWideChar(WideString(Password)),
               LOGON_WITH_PROFILE, nil, PWideChar(WideString(Command)),
               CREATE_UNICODE_ENVIRONMENT,
               lpvEnv, szPath, si, pi)) then
           begin
              result:=ERROR_SUCCESS;
              CloseHandle(pi.hProcess);
              CloseHandle(pi.hThread);
           end
           else
              result:=GetLastError;
        end
        else
           result:=GetLastError;
        DestroyEnvironmentBlock(lpvEnv);
     end
     else
        result:=GetLastError;
     CloseHandle(hToken);
  end
  else
     result:=GetLastError;
end;
und:

Delphi-Quellcode:
function CreateProcessAsLogon(const User, PW, Application, CmdLine: WideString):
  Boolean;
var
  si : TStartupInfoW;
  pif : TProcessInformation;
begin
  ZeroMemory(@si, sizeof(TStartupInfo));
  si.cb := SizeOf(TStartupInfoW);
  si.dwFlags := STARTF_USESHOWWINDOW;
  si.wShowWindow := 1;
  Result := CreateProcessWithLogonW(PWideChar(User), nil, PWideChar(PW),
    LOGON_WITH_PROFILE, nil, PWideChar(Application +' '+CmdLine),
    CREATE_DEFAULT_ERROR_MODE, nil, nil, si, pif);
end;
Ich benötige RunAs unter Windows 2000

KUBA
Stefan Kubatzki
E=mc2
  Mit Zitat antworten Zitat