问题描述
我们需要在运行时更改 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;
微软建议的另一个解决方案是在进程外创建一个 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.
注意:没有CoCreateInstanceAsAdmin"API 调用.这只是一些漂浮的代码.这是我偶然发现的 Dephi 版本.当通常隐藏的代码在内部调用 CoGetObject:
Note: There is no "CoCreateInstanceAsAdmin" API call. It's just some code floating around. Here's the Dephi version i stumbled around for. It is apparently based on the trick of prefixing a class guid string with the "Elevation:Administrator!new:" prefix when normally hidden code internally calls 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?
这篇关于Delphi:需要时提示 UAC 提升的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!