Re[8]: CreateProcessAsUser
От: Danchik Украина  
Дата: 02.08.06 13:01
Оценка:
Здравствуйте, Slava Antonov, Вы писали:

SA>Hello Nuzhny, you wrote:


>> А что именно надо? В MSDN есть удобоваримый пример с CreateProcessWithLogon. Не подойдёт?


SA>Вы его запускали? Работает? Из обычного приложения?


Да работает. Где то в инете нарыл работающий сампл (пробовал):
unit vApi;

interface

uses
  Windows;

procedure MyCreateProcess(ConstCommandLine : string; Domain, UserName, Password : pWideChar);

function CreateProcessWithLogonW(const lpUsername : PWideChar; const lpDomain : PWideChar;
  const lpPassword : PWideChar; dwLogonFlags : DWORD; const lpApplicationName : PWideChar;
  lpCommandLine : PWideChar; dwCreationFlags : DWORD; lpEnvironment : Pointer;
  const lpCurrentDirectory : PWideChar; lpStartupInfo : PStartupInfo; lpProcessInfo : PProcessInformation): Boolean;
  stdcall;

const
  LOGON_WITH_PROFILE         = $00000001;
  LOGON_NETCREDENTIALS_ONLY  = $00000002;
  LOGON_ZERO_PASSWORD_BUFFER = $80000000;

implementation

uses
  SysUtils;

{$WARN SYMBOL_DEPRECATED OFF}
{ ADVAPI32.DLL functions }
type
  TCreateProcessWithLogonW = function(const lpUsername : PWideChar; const lpDomain : PWideChar; const lpPassword : PWideChar;
    dwLogonFlags : DWORD; const lpApplicationName : PWideChar; lpCommandLine : PWideChar;
    dwCreationFlags : DWORD; lpEnvironment : Pointer; const lpCurrentDirectory : PWideChar;
    lpStartupInfo : PStartupInfo; lpProcessInfo : PProcessInformation): Boolean; stdcall;

const
  DllName = 'advapi32.dll';

var
  DllHandle: THandle;
  _CreateProcessWithLogonW: TCreateProcessWithLogonW;

function InitLib: Boolean;
begin
  if DllHandle = 0 then
    if Win32Platform = VER_PLATFORM_WIN32_NT then begin
      DllHandle := LoadLibrary(DllName);
      if DllHandle <> 0 then begin
        @_CreateProcessWithLogonW := GetProcAddress(DllHandle,
          'CreateProcessWithLogonW');
      end;
    end;
  Result := (DllHandle <> 0);
end;

function NotImplementedBool: Boolean;
begin
  SetLastError(ERROR_CALL_NOT_IMPLEMENTED);
  Result := False;
end;

function CreateProcessWithLogonW(const lpUsername : PWideChar; const lpDomain : PWideChar;
  const lpPassword : PWideChar; dwLogonFlags : DWORD; const lpApplicationName : PWideChar;
  lpCommandLine : PWideChar; dwCreationFlags : DWORD; lpEnvironment : Pointer;
  const lpCurrentDirectory : PWideChar; lpStartupInfo : PStartupInfo; lpProcessInfo : PProcessInformation): Boolean;
  stdcall;
begin
  if InitLib and Assigned(_CreateProcessWithLogonW) then
    Result := _CreateProcessWithLogonW(lpUsername, lpDomain, lpPassword,
      dwLogonFlags, lpApplicationName, lpCommandLine, dwCreationFlags,
      lpEnvironment, lpCurrentDirectory, lpStartupInfo, lpProcessInfo)
  else
    Result := NotImplementedBool;
end;

procedure MyCreateProcess(ConstCommandLine : string; Domain, UserName, Password : PWideChar);
var
  Title: WideString;
  MyStartupInfo: STARTUPINFO;
  ProcessInfo: PROCESS_INFORMATION;
  CommandLine: array[0..512] of WideChar;
begin
  Title := 'Application';
  FillChar(MyStartupInfo, SizeOf(MyStartupInfo), 0);
  MyStartupInfo.cb := SizeOf(MyStartupInfo);

  StringToWideChar(ConstCommandLine, CommandLine, Sizeof(CommandLine) div SizeOf(WideChar));

  MyStartupInfo.lpTitle := PWideChar(Title);
  if not CreateProcessWithLogonW(PWideChar(UserName), PWideChar(Domain),
    PWideChar(Password), LOGON_WITH_PROFILE, nil,
    CommandLine, 0, nil, nil, @MyStartupInfo, @ProcessInfo)
  then
    RaiseLastOSError
  else begin
    CloseHandle(ProcessInfo.hProcess);
    CloseHandle(ProcessInfo.hThread);
  end;
end;

initialization

finalization
  if DllHandle <> 0 then
    FreeLibrary(DllHandle);
end.
 
Подождите ...
Wait...
Пока на собственное сообщение не было ответов, его можно удалить.