Delphi 如何以管理员权限运行程序
2010-05-06 17:12
786 查看
IT 时常会碰到一个困扰,就是当用户权限太大时,容易出现各种问题,比如乱装软件,病毒等问题。但权限太小,也很麻烦,时常要IT给用户装一些允许的软件。一下程序允许用户以管理员的身份安装IT允许用户自己安装的软件而不用给用户管理员密码。
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
_STARTUPINFOW = record
cb: DWORD;
lpReserved: LPWSTR;
lpDesktop: LPWSTR;
lpTitle: LPWSTR;
dwX: DWORD;
dwY: DWORD;
dwXSize: DWORD;
dwYSize: DWORD;
dwXCountChars: DWORD;
dwYCountChars: DWORD;
dwFillAttribute: DWORD;
dwFlags: DWORD;
wShowWindow: Word;
cbReserved2: Word;
lpReserved2: PByte;
hStdInput: THandle;
hStdOutput: THandle;
hStdError: THandle;
end;
STARTUPINFOW = _STARTUPINFOW;
var
Form1: TForm1;
implementation
{$R *.dfm}
function CreateProcessWithLogonW(lpUserName, lpDomain, lpPassword: LPCWSTR;
dwLogonFlags: DWORD; lpApplicationName: LPCWSTR; lpCommandLine: LPWSTR;
dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: LPCWSTR;
const lpStartupInfo: STARTUPINFOW; var lpProcessInformation: PROCESS_INFORMATION): BOOL; stdcall;
external advapi32 Name 'CreateProcessWithLogonW'
procedure TForm1.FormCreate(Sender: TObject);
var
STARTUPINFO: StartupInfoW;
ProcessInfo: TProcessInformation;
AUser, ADomain, APass, AExe: WideString;
const
LOGON_WITH_PROFILE = $00000001;
LOGON_NETCREDENTIALS_ONLY = $00000002;
begin
FillChar(STARTUPINFO, SizeOf(StartupInfoW), 0);
STARTUPINFO.cb := SizeOf(StartupInfoW);
STARTUPINFO.dwFlags := STARTF_USESHOWWINDOW;
STARTUPINFO.wShowWindow := SW_SHOW;
AUser := 'administrator';
ADomain := 'msic.com.cn';
APass := 'xxxxxx';
AExe := '\\it\tools\install_flash_player_ax.exe';
if not CreateProcessWithLogonW(PWideChar(AUser), PWideChar(ADomain),
PWideChar(APass),
LOGON_WITH_PROFILE, nil, PWideChar(AExe),
NORMAL_PRIORITY_CLASS, nil, nil, STARTUPINFO, ProcessInfo) then
RaiseLastOSError;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
close;
end;
end.
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Label1: TLabel;
procedure FormCreate(Sender: TObject);
procedure FormActivate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
type
_STARTUPINFOW = record
cb: DWORD;
lpReserved: LPWSTR;
lpDesktop: LPWSTR;
lpTitle: LPWSTR;
dwX: DWORD;
dwY: DWORD;
dwXSize: DWORD;
dwYSize: DWORD;
dwXCountChars: DWORD;
dwYCountChars: DWORD;
dwFillAttribute: DWORD;
dwFlags: DWORD;
wShowWindow: Word;
cbReserved2: Word;
lpReserved2: PByte;
hStdInput: THandle;
hStdOutput: THandle;
hStdError: THandle;
end;
STARTUPINFOW = _STARTUPINFOW;
var
Form1: TForm1;
implementation
{$R *.dfm}
function CreateProcessWithLogonW(lpUserName, lpDomain, lpPassword: LPCWSTR;
dwLogonFlags: DWORD; lpApplicationName: LPCWSTR; lpCommandLine: LPWSTR;
dwCreationFlags: DWORD; lpEnvironment: Pointer; lpCurrentDirectory: LPCWSTR;
const lpStartupInfo: STARTUPINFOW; var lpProcessInformation: PROCESS_INFORMATION): BOOL; stdcall;
external advapi32 Name 'CreateProcessWithLogonW'
procedure TForm1.FormCreate(Sender: TObject);
var
STARTUPINFO: StartupInfoW;
ProcessInfo: TProcessInformation;
AUser, ADomain, APass, AExe: WideString;
const
LOGON_WITH_PROFILE = $00000001;
LOGON_NETCREDENTIALS_ONLY = $00000002;
begin
FillChar(STARTUPINFO, SizeOf(StartupInfoW), 0);
STARTUPINFO.cb := SizeOf(StartupInfoW);
STARTUPINFO.dwFlags := STARTF_USESHOWWINDOW;
STARTUPINFO.wShowWindow := SW_SHOW;
AUser := 'administrator';
ADomain := 'msic.com.cn';
APass := 'xxxxxx';
AExe := '\\it\tools\install_flash_player_ax.exe';
if not CreateProcessWithLogonW(PWideChar(AUser), PWideChar(ADomain),
PWideChar(APass),
LOGON_WITH_PROFILE, nil, PWideChar(AExe),
NORMAL_PRIORITY_CLASS, nil, nil, STARTUPINFO, ProcessInfo) then
RaiseLastOSError;
end;
procedure TForm1.FormActivate(Sender: TObject);
begin
close;
end;
end.
相关文章推荐
- Delphi下如何使程序在Win7/Vista上用管理员权限运行[转]
- Delphi下如何使程序在Win7/Vista上用管理员权限运行
- Delphi下如何使程序在Win7/Vista上用管理员权限运行[转]
- Delphi下如何使程序在Win7/Vista上用管理员权限运行(转)
- Delphi下如何使程序在Vista上用管理员权限运行
- Delphi下如何使程序在Win7/Vista上用管理员权限运行[转]
- Delphi下如何使程序在Win7/Vista上用管理员权限运行
- Delphi下如何使程序在Win7/Vista上用管理员权限运行
- Delphi下如何使程序在Win7/Vista上用管理员权限运行[转]
- Delphi下如何使程序在Win7/Vista上用管理员权限运行
- Delphi编译的程序如何获取管理员权限
- Vista、Win7上.net程序如何自动使用管理员权限运行
- 如何在程序里模拟在cmd里用管理员权限运行一条指令
- C# windows权限问题 程序如何以管理员身份运行程序
- VS2013编译生成的程序如何设置以管理员权限运行
- 如何在VC中通过添加资源文件,使程序在Vista上用管理员权限运行
- 如何让QT程序以管理员权限运行(UAC)
- [快手(AAuto)学习笔记]如何让程序在运行时请求管理员权限(UAC)
- Win7如何提升为管理员权限,如何开机启动需要管理员权限的程序,解决win7开机不能自动运行的问题等
- 如何让Delphi程序启动自动“以管理员身份运行"