针对 Windows 7 中的注册表操作限制的解决方法? [英] Workaround against registry manipulation limitation in windows 7?

查看:23
本文介绍了针对 Windows 7 中的注册表操作限制的解决方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,因为在 xp 中大多数人都以管理员身份登录 - 程序员很容易使程序与注册表一起工作.我想做的是:

软件已启动,并作为启动进程添加到注册表中 - 但是这应该仅在应用程序关闭时完成 - 而不是之前.

但是,当用户受限时,这在 xp 中不起作用,而在 vista,7,2008 中也是如此.

有什么解决方法?我正在考虑让程序创建计划任务或附加到具有更高权限的进程?有什么工作方式吗?我将其标记为 .net,因为我的软件与 .net 相关 - 实际上在 c++ 中也会发生同样的事情 - 但我暗中希望 net 提供更简单的方法来解决它.提前10倍!

解决方案

嗯,这不是Windows 7的限制;它实际上是设计使然..
但只是总结一下链接失效的情况,以下是步骤:

  1. 确定用户是否已经拥有适当的权限.最简单的方法是调用 IsUserAnAdminAPI函数.

  2. 使用盾牌"图标通知用户需要提升.在WinForms中,需要将按钮的FlatStyle属性设置为System",并使用P/Invoke来显示盾牌.示例代码:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]公共静态外部 IntPtr SendMessage(IntPtr hWnd, int Msg,IntPtr wParam, IntPtr lParam);公共常量 int BCM_SETSHIELD = 0x0000160C;public static void SetButtonShield(Button btn, bool showShield){//验证我们是否在 Vista 或更高版本上运行if ((Environment.OSVersion.Platform == PlatformID.Win32NT) &&(Environment.OSVersion.Version.Major >= 6)){SendMessage(btn.Handle, BCM_SETSHIELD, IntPtr.Zero,秀盾?新的 IntPtr(1) : IntPtr.Zero);}}

  3. 以管理员权限重新启动该进程.这涉及显示提升对话框以允许用户提升程序.示例代码:

    ProcessStartInfo psi = new ProcessStartInfo{参数 = "-justelevated",错误对话框 = 真,//Handle 是表单的句柄ErrorDialogParentHandle = 句柄,文件名 = Application.ExecutablePath,动词 = "runas"};尝试{过程.开始(psi);关闭();}捕捉(例外前){//进程无法启动.发生这种情况的原因有 3 个://1.用户取消了UAC框//2. 受限用户尝试提升为密码为空的管理员//3. 受限用户尝试提升为访客账户MessageBox.Show(ex.Message);}

  4. [可选] 代码签名您的应用程序以替换看起来充满敌意的黄色UAC 高度框带有更令人愉悦的灰色或蓝色.

Ok so since in xp most people are logged as admins - it's easy for a programmer to make a program work with the registry. What I am trying to do is this:

The software is started and it's added as a startup process in the registry- however this should be done only when the application is closing - not before.

However this doesn't work in xp when the user is limited and same thing in vista,7,2008.

What are the workarounds? I was thinking to make the program create a scheduled task or being attached to a process with higher privilleges? Any working way? I am taggin this as .net since my software is .net related - actually same thing happens and in c++ - but i secretly hope that net offers easier methods to work it out. 10x in advance!

解决方案

Um, this isn't a limitation of Windows 7; it's actually by-design. See my answer here for details.

What you need is called process elevation. It's the standard way of dealing with this, a mechanism built into UAC to allow users to authenticate themselves as Administrators and temporarily gain all of the privileges and responsibilities that come with this title. Windows itself uses this all over the place:

There's a fantastic how-to article available here: Shield icons, UAC, and process elevation in .NET.
But just to summarize in case of link rot, here are the steps:

  1. Determine if the user has the appropriate permissions already. The simplest way is calling the IsUserAnAdmin API function.

  2. Notify the user that elevation is required using a "shield" icon. In WinForms, you need to set the button's FlatStyle property to "System", and use P/Invoke to display the shield. Sample code:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hWnd, int Msg,
                                            IntPtr wParam, IntPtr lParam);
    
    public const int BCM_SETSHIELD = 0x0000160C;
    
    public static void SetButtonShield(Button btn, bool showShield)
    {
        // Verify that we're running on Vista or later
        if ((Environment.OSVersion.Platform == PlatformID.Win32NT) &&
            (Environment.OSVersion.Version.Major >= 6))
        {
            SendMessage(btn.Handle, BCM_SETSHIELD, IntPtr.Zero,
                        showShield ? new IntPtr(1) : IntPtr.Zero);
        }
    }
    

  3. Re-launch the process with administrator privileges. This involves showing the elevation dialog to allow the user to elevate the program. Sample code:

    ProcessStartInfo psi = new ProcessStartInfo
                               {
                                   Arguments = "-justelevated",
                                   ErrorDialog = true,
    
                                   // Handle is the handle for your form
                                   ErrorDialogParentHandle = Handle,
                                   FileName = Application.ExecutablePath,
                                   Verb = "runas"
                               };
    try
    {
        Process.Start(psi);
        Close();
    }
    catch (Exception ex)
    {
        // the process couldn't be started. This happens for 1 of 3 reasons:
    
        // 1. The user cancelled the UAC box
        // 2. The limited user tried to elevate to an Admin that has a blank password
        // 3. The limited user tries to elevate as a Guest account
        MessageBox.Show(ex.Message);
    }
    

  4. [Optional] Code sign your application to replace the hostile-looking yellow UAC elevation box with a more pleasing gray or blue one.

这篇关于针对 Windows 7 中的注册表操作限制的解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆