德尔福:需要时提醒UAC抬高 [英] Delphi: Prompt for UAC elevation when needed
问题描述
我们需要在运行时将一些设置更改为HKEY_LOCAL_MACHINE。
We need to change some settings to the HKEY_LOCAL_MACHINE at runtime.
是否可以在运行时需要提示uac提升,还是必须启动第二个提升的过程是做肮脏的工作?
Is it possible to prompt for uac elevation if needed at runtime, or do I have to launch a second elevated process to do 'the dirty work'?
推荐答案
我将重新启动自己作为提升,传递命令行参数,你想做的事情您可以直接跳到适当的表单,或者只是保存HKLM的东西。
i would relaunch yourself as elevated, passing command line parameters indicating what elevated thing you want to do. You can then jump right to the appropriate form, or just save your HKLM stuff.
function RunAsAdmin(hWnd: HWND; filename: string; Parameters: string): Boolean;
{
See Step 3: Redesign for UAC Compatibility (UAC)
http://msdn.microsoft.com/en-us/library/bb756922.aspx
This code is released into the public domain. No attribution required.
}
var
sei: TShellExecuteInfo;
begin
ZeroMemory(@sei, SizeOf(sei));
sei.cbSize := SizeOf(TShellExecuteInfo);
sei.Wnd := hwnd;
sei.fMask := SEE_MASK_FLAG_DDEWAIT or SEE_MASK_FLAG_NO_UI;
sei.lpVerb := PChar('runas');
sei.lpFile := PChar(Filename); // PAnsiChar;
if parameters <> '' then
sei.lpParameters := PChar(parameters); // PAnsiChar;
sei.nShow := SW_SHOWNORMAL; //Integer;
Result := ShellExecuteEx(@sei);
end;
另一个Microsoft建议的解决方案是创建一个COM对象(使用特殊创建的CoCreateInstanceAsAdmin函数) )。我不喜欢这个想法,因为你必须写一个COM对象。
The other Microsoft suggested solution is to create an COM object out of process (using the specially created CoCreateInstanceAsAdmin function). i don't like this idea because you have to write and register a COM object.
注意: / strong>没有CoCreateInstanceAsAdminAPI调用。只是一些代码浮动。这是我发现的Dephi版本。这显然是基于在正常隐藏代码内部调用 CoGetObject :
function CoGetObject(pszName: PWideChar; pBindOptions: PBindOpts3;
const iid: TIID; ppv: PPointer): HResult; stdcall; external 'ole32.dll';
procedure CoCreateInstanceAsAdmin(const Handle: HWND;
const ClassID, IID: TGuid; PInterface: PPointer);
var
BindOpts: TBindOpts3;
MonikerName: WideString;
Res: HRESULT;
begin
//This code is released into the public domain. No attribution required.
ZeroMemory(@BindOpts, Sizeof(TBindOpts3));
BindOpts.cbStruct := Sizeof(TBindOpts3);
BindOpts.hwnd := Handle;
BindOpts.dwClassContext := CLSCTX_LOCAL_SERVER;
MonikerName := 'Elevation:Administrator!new:' + GUIDToString(ClassID);
Res := CoGetObject(PWideChar(MonikerName), @BindOpts, IID, PInterface);
if Failed(Res) then
raise Exception.Create(SysErrorMessage(Res));
end;
另一个问题:如何在Windows XP中处理作为标准用户运行的用户?
One other question: How do you handle someone running as standard user in Windows XP?
这篇关于德尔福:需要时提醒UAC抬高的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!