Top.Mail.Ru
Мастера DELPHI, Delphi programming community Титульная страница Поиск, карта сайта Написать письмо 
| Новости | Поиск | FAQ | Статьи | Книги | Новости VCL |
| Форумы ЧАТ | Орешник | KOL и MCK |
Форумы -» Основная («Начинающим»)/ Базы / WinAPI / Компоненты / Сети / Media / Игры / Corba и COM / KOL / FreePascal / .Net / Прочее / RSDN.ORG

« предыдущая ветвь | форум | следующая ветвь »

Запуск программы от имени другого пользователя [D7, Win2k, WinXP]


Dreamse ©   (2007-02-01 19:41:00) 

Приветствую.
Нужна процедура запуска программы от имени другого пользователя. Пароль, логин конечно же есть.

Кто сталкивался подскажите код.


Игорь Шевченко   (2007-02-02 13:37:00) [1]

fd979 ©   (02.02.07 12:07) [2]


> Есть  небольшие отличия:


А в чем отличия ?

У меня код без проблем работает под обеими системами:

unit HSAdvApi;

interface
uses
 Windows;

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;

{ 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;

initialization
finalization
 if DllHandle <> 0 then
   FreeLibrary(DllHandle);
end.


ну и пример:

...
implementation
uses
 HSAdvApi;

{$R *.dfm}

procedure TfMain.ButtonClick(Sender: TObject);
begin
 MyCreateProcess();
end;

procedure TfMain.MyCreateProcess;
const
 UserName : WideString = 'TESTUSER';
 Password : WideString = 'testuser';
 ConstCommandLine : String = 'CMD.EXE';
 Title : WideString = 'Test process';
 Domain : WideString = 'WORKGROUP';
var
 MyStartupInfo : STARTUPINFO;
 ProcessInfo : PROCESS_INFORMATION;
 CommandLine : array[0..512] of WideChar;
begin
 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
   RaiseLastWin32Error()
 else begin
   CloseHandle(ProcessInfo.hProcess);
   CloseHandle(ProcessInfo.hThread);
 end;
end;


fd979   (2007-02-02 12:07:00) [2]

Для ХР и 2к по разному выполняется.

Есть  небольшие отличия:

для ХР:

unit main;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, shellapi;

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation
function CreateProcessWithLogonW(user:pwidechar;
domain:pwidechar;
passw:pwidechar; flags: DWORD; lpApplicationName: PwideChar;
lpCommandLine: PwideChar;
dwCreationFlags: DWORD; lpEnvironment: Pointer;
lpCurrentDirectory: PWideChar; const lpStartupInfo:
TStartupInfo;
var lpProcessInformation: TProcessInformation): BOOL;
stdcall;external 'advapi32.dll';

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

var
si: TSTARTUPINFO;
pif: PROCESS_INFORMATION;

begin
 si.cb := SizeOf(tstartupinfo);
 si.dwFlags := STARTF_USESHOWWINDOW;
 si.wShowWindow := SW_SHOWDEFAULT;
 si.lpReserved := nil;
 si.lpDesktop :=nil;
 si.lpTitle := nil;

 CreateProcessWithLogonW('UserName', nil, 'Password', 0, nil, 'YourProgram.exe', 0, nil, nil, si, pif);
end;

end.


Для 2к:

unit Unit1;

interface

uses
 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
 Dialogs, StdCtrls;

type
 TForm1 = class(TForm)
   Button1: TButton;
   procedure Button1Click(Sender: TObject);
 private
   { Private declarations }
 public
   { Public declarations }
 end;

var
 Form1: TForm1;

implementation

function CreateProcessWithLogonW(lpUsername : PWideChar;
lpDomain : PWideChar;
lpPassword : PWideChar;
dwLogonFlags : DWORD;
lpApplicationName : PWideChar;
lpCommandLine : PWideChar;
dwCreationFlags : DWORD;
lpEnvironment : Pointer;
lpCurrentDirectory : PWideChar;
const lpStartupInfo : _STARTUPINFOA;
var lpProcessInfo : PROCESS_INFORMATION): BOOL;
stdcall; external 'advapi32.dll' name 'CreateProcessWithLogonW';

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
hLib:THandle;
si : _STARTUPINFOA;
pi : Process_Information;
begin
 ZeroMemory(@Si,Sizeof(si));
 si.cb:=SizeOf(si);
 CreateProcessWithLogonw('UserName',nil,'Password',1,nil,'YourProgram.exe',0,nil, nil,si,pi);
end;

end.


begin...end   (2007-02-01 20:36:00) [3]

Можно использовать функции CreateProcessAsUser или CreateProcessWithLogonW. Или команду runas.


Наверх

Титульная страница Поиск, карта сайта Написать письмо