zoukankan      html  css  js  c++  java
  • Win7开发系列: Win7下程序调用installutil注册服务权限问题

    问题:


         win7系统下,自己写了个windows服务,在cmd下运行installutil注册不成功,以管理员身份起动cmd后运行installutil注册成功。

    然后将服务卸载掉,另写了个程序用windowsAPI里的process函数调用installutil.exe去注册这个服务不成功,给出的提示是“......不可访问的日志: Security.........”,其实也就是权限问题。

    答案:


    方法如下: 在项目中新建一个文件:app.manifest,然后在其中输入以下代码,即可将该程序的运行权限提高到管理员级别。
    app.manifest内容如下: 

    <?xml version="1.0" encoding="utf-8"?>
    <asmv1:assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv1="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      
    <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
      
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        
    <security>
          
    <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
            
    <!-- UAC 清单选项
                如果希望更改 Windows 用户帐户控制级别,请用以下节点之一替换 
                requestedExecutionLevel 节点。

            <requestedExecutionLevel  level="asInvoker" uiAccess="false" />
            <requestedExecutionLevel  level="requireAdministrator" uiAccess="false" />
            <requestedExecutionLevel  level="highestAvailable" uiAccess="false" />

                如果您希望利用文件和注册表虚拟化提供
                向后兼容性,请删除 requestedExecutionLevel 节点。
            
    -->
            
    <requestedExecutionLevel level="requireAdministrator" uiAccess="false" />
          
    </requestedPrivileges>
        
    </security>
      
    </trustInfo>
    </asmv1:assembly>
    内容里的说明够详细了吧,只要把 asInvoker替换成requireAdministrator,我们的程序就会默认要求管理员权限运行了

    下面再说下怎么给程序的按钮上也加上小盾牌图标吧

    这我们就需要调用Win32 API了

    要调用API么,要先引用命名空间

    using System.Runtime.InteropServices;

    [DllImport("user32.dll")]
            
    private static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

            
    ///////////////////////////////////////////////////////////////////////
            
    /// <summary>
            
    ///     Enables the elevated shield icon on the given button control
            
    /// </summary>
            
    /// <param name="ThisButton">
            
    ///     Button control to enable the elevated shield icon on.
            
    /// </param>
            
    ///////////////////////////////////////////////////////////////////////
            private void EnableElevateIcon_BCM_SETSHIELD(Button ThisButton)
            {
                
    // Input validation, validate that ThisControl is not null
                if (ThisButton == null)
                {
                    
    return;
                }

                
    // Define BCM_SETSHIELD locally, declared originally in Commctrl.h
                uint BCM_SETSHIELD = 0x0000160C;

                
    // Set button style to the system style
                ThisButton.FlatStyle = FlatStyle.System;

                
    // Send the BCM_SETSHIELD message to the button control
                SendMessage(new HandleRef(ThisButton, ThisButton.Handle), BCM_SETSHIELD, new IntPtr(0), new IntPtr(1));
            }

     运行效果:

    image 

     参考资料:


    UAC可以在编译时设置,提示用户输入管理员密码才能执行,参考http://msforums.ph/forums/t/52208.aspx

    如果嫌这个麻烦,可以跳过UAC检查,参考http://www.agileit.com/Blog/Lists/Posts/Post.aspx?ID=265 http://forum.sysinternals.com/topic13776.html 

    选择性禁止UAC,参考http://www.techrepublic.com/blog/window-on-windows/selectively-disable-uac-for-your-trusted-vista-applications/635 

    作者:罗敏贵
    邮箱:minguiluo@163.com
    QQ群:34178394 建群 主要是寻找志同道合的人士一起学习和讨论自己的所学所思
    出处:http://luomingui.cnblogs.com/
    说明:专注于微软平台项目架构、熟悉设计模式、架构设计、敏捷个人和项目管理。现主要从事WinForm、ASP.NET、等方面的项目开发、架构、管理工作。文章为作者平时里的思考和练习,可能有不当之处,请博客园的园友们多提宝贵意见。
    知识共享许可协议本作品采用知识共享署名-非商业性使用-相同方式共享 2.5 中国大陆许可协议进行许可。

  • 相关阅读:
    SQL SERVER 运维日记
    openstack
    Java GC 日志详解
    突破 BTrace 安全限制
    End-to-End Tracing of Ajax/Java Applications Using DTrace
    调试工具BTrace 的使用--例子
    btrace-dtrace-for-java-ish
    DTrace Probes in HotSpot VM
    DTrace memory leak 内存泄露
    Java-JVM-GC
  • 原文地址:https://www.cnblogs.com/luomingui/p/2096221.html
Copyright © 2011-2022 走看看