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

Вниз

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

 
Геннадий ©   (2003-11-21 08:02) [0]

Каким образом, зная путь к программе, логин и пароль администратора, запустить эту программу с привилегиями администратора?


 
N169   (2003-11-21 10:35) [1]

Есть такая фича: CreateProcessWithLogonW(), см. MSDN


 
Игорь Шевченко ©   (2003-11-21 14:05) [2]

unit main;

interface

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

type
TfMain = class(TForm)
Button: TButton;
procedure ButtonClick(Sender: TObject);
end;

var
fMain: TfMain;

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;

end.


HSAdvApi.pas
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.


 
Геннадий ©   (2003-11-21 14:38) [3]

Спасибо за подсказку. Ого! Вот это код. Я-то думал, что есть простенькая WinAPI-функция типа RunAs(Path, User, Pass: String)...


 
Геннадий ©   (2003-11-27 08:11) [4]

А вот есть такой схожий вопрос. Каким образом назначить своей программе привилегии заданной учётной записи?

Можно себя запустить другой программой вышеуказанным способом, но не охота иметь такой загрузчик.


 
Бином Ньютоныч   (2003-11-27 17:42) [5]

LogonUser и ImpersonateLoggedOnUser


 
Геннадий ©   (2003-12-02 11:51) [6]

Когда пытаюсь запускать своей программой чужие приложения, которые для своей работы требуют привилегии администратора получаю "System error Code 5 - Отказано в доступе" Но эта ошибка появляется если я пытаюсь запустить приложение будучи под учётной записью Гостя или Пользователя. А вот под Администратором всё работает.
Может сама программа (моя), которая запускает чужое приложение с правами Администратора должна сама быть запущенной с правами админа? Или дело в другом?


> Бином Ньютоныч (27.11.03 17:42) [5]
> LogonUser и ImpersonateLoggedOnUser


Вызов LogonUser (с целью получения его токена) приводит к ошибке "Привилегии не даны" А как назначить их себе? (SE_TCB_NAME)



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

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

Наверх




Память: 0.48 MB
Время: 0.019 c
6-53674
XeON
2003-12-14 11:04
2004.02.17
Отправка файлов через UDP/TCP(Sockets)


1-53576
3APA3A
2004-02-08 21:28
2004.02.17
Цветной заголовок у GroupBox а...


1-53464
DAnton
2004-02-06 09:18
2004.02.17
Не могу убрать BalloonHint.


3-53362
jenya_d
2004-01-27 14:45
2004.02.17
регистрация действий пользователя в базе данных


1-53548
ZZZZZZZZZZZZZZZZ
2004-02-05 12:36
2004.02.17
Заставка