T
tsedlinux
Unregistered / Unconfirmed
GUEST, unregistred user!
XPSP2 普通用户下,转换身份为高权限用户,来执行一个程序问题(部分代码) 在普通用户下 需要运行一安装程序,但普通用户没有执行安装的权限 所以用程序以更高权限的用户如administrator来执行程序 按MSDN的说明 需要用LOGONUSER 和 CREATEPROCESSASUSER两个函数配合使用,以下是我做的代码 但问题是不能执行设想中的NOTEPAD 在WIN2000下通过CreateProcessWithLogonW函数即可实现,但在XPSP2下是不能使用这个函数的Windows XP SP2 and Windows Server 2003: You cannot call CreateProcessWithLogonW from a process that is running under the LocalSystem account, because the function uses the logon SID in the caller token, and the token for the LocalSystem account does not contain this SID. As an alternative, use the CreateProcessAsUser and LogonUser functions.哪位兄弟能帮我一下 unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs,shellapi, StdCtrls;type TForm1 = class(TForm) Button4: TButton; procedure Button1Click(Sender: TObject); procedure Button4Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}{function ValidateUserLogonAPI(const UserName: string;const Domain: string;const PassWord: string): boolean;var Retvar: boolean; LHandle: THandle;begin Retvar := LogonUser(PChar(UserName), PChar(Domain), PChar(PassWord), LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT, LHandle); if Retvar then CloseHandle(LHandle); Result := Retvar;end;}procedure TForm1.Button1Click(Sender: TObject);beginend; //CreateProcessAsUser运行程序procedure TForm1.Button4Click(Sender: TObject);var si:STARTUPINFO; piROCESS_INFORMATION; hToken: THandle;beginif LogonUser('administrator','.','123456789',LOGON32_LOGON_NETWORK,LOGON32_PROVIDER_DEFAULT,hToken) then begin ZeroMemory( @si, sizeof( STARTUPINFO ) ); si.cb := sizeof( STARTUPINFO ); Si.lpDesktop := PChar('Winsta0/Default'); si.wShowWindow:=SW_SHOWNORMAL; CreateProcessAsUser(hToken, pchar('c:/notepad.exe') , //程序名 pchar('c:/a.txt'), //参数 nil, nil, FALSE, NORMAL_PRIORITY_CLASS or CREATE_NEW_CONSOLE, nil, nil, si, pi ); end; end;{ ZeroMemory(&si, sizeof(STARTUPINFO)); si.cb = sizeof(STARTUPINFO); si.lpDesktop = "winsta0//default"; // // start the process // if (!CreateProcessAsUser( hToken, NULL, "cmd.exe", NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS | CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi )) return RTN_ERROR;}end