Главная страница
    Top.Mail.Ru    Яндекс.Метрика
Форум: "WinAPI";
Текущий архив: 2005.11.13;
Скачать: [xml.tar.bz2];

Вниз

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

 
Донской   (2005-09-14 14:48) [0]

День добрый!
Я бы не задавал этот вопрос, если бы на форме работал поиск.
(это к админам)
Вопрос вот в чем. Если щелкнуть правой кнопкой мышки по строке быстрого запуска, удерживая при этом клавишу Shift,
то в выпадающем меню появится строка Запустить от имени....
Вот это меня и интересует - как из моей программы запустить другую программу от имени другого пользователя (например, от имени локального админа, которого я же создам на своей машине)
Спасибо.


 
Игорь Шевченко ©   (2005-09-14 14:51) [1]

RunAs


 
Донской   (2005-09-14 15:03) [2]

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


 
Игорь Шевченко ©   (2005-09-14 15:09) [3]

Донской   (14.09.05 15:03) [2]

Будет.

Можно использовать функцию из advapi32.dll CreateProcessWithLogonW, но пароль все равно знать нужно.

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.


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.


 
Донской   (2005-09-14 15:49) [4]

Сейчас попробую,
спасибо!


 
Донской   (2005-09-14 16:04) [5]

Отлично работает,
Благодарю, Игорь!



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

Форум: "WinAPI";
Текущий архив: 2005.11.13;
Скачать: [xml.tar.bz2];

Наверх





Память: 0.47 MB
Время: 0.042 c
2-1129910821
Spinu Oleg
2005-10-21 20:07
2005.11.13
Указатели


3-1128069251
Slym
2005-09-30 12:34
2005.11.13
Как в Рантайме добабить LookupField


11-1111072940
uuzh
2005-03-17 18:22
2005.11.13
2 ECM Порт FBL под KOL


6-1122645473
CAMCOH
2005-07-29 17:57
2005.11.13
Как отправить html письмо с помощью Insy IdSmtp


4-1125218505
rolex
2005-08-28 12:41
2005.11.13
Функция показывает, что в папке Prefetch файлы в использовании!





Afrikaans Albanian Arabic Armenian Azerbaijani Basque Belarusian Bulgarian Catalan Chinese (Simplified) Chinese (Traditional) Croatian Czech Danish Dutch English Estonian Filipino Finnish French
Galician Georgian German Greek Haitian Creole Hebrew Hindi Hungarian Icelandic Indonesian Irish Italian Japanese Korean Latvian Lithuanian Macedonian Malay Maltese Norwegian
Persian Polish Portuguese Romanian Russian Serbian Slovak Slovenian Spanish Swahili Swedish Thai Turkish Ukrainian Urdu Vietnamese Welsh Yiddish Bengali Bosnian
Cebuano Esperanto Gujarati Hausa Hmong Igbo Javanese Kannada Khmer Lao Latin Maori Marathi Mongolian Nepali Punjabi Somali Tamil Telugu Yoruba
Zulu
Английский Французский Немецкий Итальянский Португальский Русский Испанский