Главная страница
Top.Mail.Ru    Яндекс.Метрика
Текущий архив: 2007.03.25;
Скачать: CL | DM;

Вниз

Запуск программы от имени другого пользователя   Найти похожие ветки 

 
Dreamse   (2007-02-01 19:41) [0]

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

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


 
begin...end ©   (2007-02-01 20:36) [1]

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


 
fd979 ©   (2007-02-02 12:07) [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.


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

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;



Страницы: 1 вся ветка

Текущий архив: 2007.03.25;
Скачать: CL | DM;

Наверх




Память: 0.49 MB
Время: 0.031 c
15-1172665266
Чапаев
2007-02-28 15:21
2007.03.25
WFP


15-1172329940
CCil
2007-02-24 18:12
2007.03.25
Почему google стоит так не дешево?


15-1172903795
atruhin
2007-03-03 09:36
2007.03.25
Можно ли лицензировать пиратский Windows 2003 Server


15-1172737610
Ega23
2007-03-01 11:26
2007.03.25
Не ставятся breakpoint-ы в проекте....


11-1151077141
parovoZZ
2006-06-23 19:39
2007.03.25
Поработать до создания формы