MASM32编程实现运行时自动提示要求以管理员帐户来运行

简介: MASM32编程实现运行时自动提示要求以管理员帐户来运行

为提高系统安全性,微软从Windows Vista开始引入UAC(User Account Control,用户帐户控制),这一机制要求用户在执行可能会影响计算机运行的操作或执行更改影响其他用户的设置的操作之前必须提供权限或验证管理员密码。于是当我们以非管理员帐户运行此类程序时,这些程序会自动提示要求以管理员帐户来运行,这一功能是如何实现的呢?

 对于使用Visual Studio的开发者,可以通过添加 manifest来实现。那么使用MASM32的开发者又该怎么样做呢?

 我们MASM32的开发者可以在资源文件上着手。下面以Icztutes中的TUTE02的代码来做示例。

 首先我们在Icztutes的TUTE02文件夹中添加一个rsrc.rc文件,内容如下:

#include "\MASM32\include\Resource.h"
#ifndef  CREATEPROCESS_MANIFEST_RESOURCE_ID
#define  CREATEPROCESS_MANIFEST_RESOURCE_ID  1
#endif
#ifndef  RT_MANIFEST
#define  RT_MANIFEST                         24
#endif
#ifndef  VS_VERSION_INFO
#define  VS_VERSION_INFO                     1
#endif
#ifndef  VOS_NT_WINDOWS32
#define  VOS_NT_WINDOWS32                    0x00040004L
#endif
#ifndef  VFT_APP
#define  VFT_APP                             0x00000001L
#endif
1 24 "runAsAdminOnly.xml"
#define APP_VERSION_INFO    1     // Define at the top your .RC file  
// example version resource
APP_VERSION_INFO VERSIONINFO
    FILEVERSION 1,0         //1,1,0,0
    PRODUCTVERSION 1,0         //1,1,0,0
    FILEFLAGSMASK 0x17L
    FILEFLAGS 0x0L
    FILEOS 0x4L
    FILETYPE 0x1L
    FILESUBTYPE 0x0L
{
    BLOCK "StringFileInfo"
    {
        BLOCK "040904b0"
        {
            VALUE "CompanyName", "(c) PurpleEndurer"
            VALUE "FileDescription", "only run as an Admin"
            VALUE "FileVersion", "1.0"
            VALUE "InternalName", "runAsAdminOnly.exe"
            VALUE "LegalCopyright", "(c) PurpleEndurer"
            VALUE "OriginalFilename", "runAsAdminOnly.exe"
        }
    }
    BLOCK "VarFileInfo"
    {
        VALUE "Translation", 0x409, 1200
    }
}


在rsrc.rc中的第23行我们引入了一个实现这个功能的文件runAsAdminOnly.xml。接着我们就在Icztutes的TUTE02文件夹中创建这个文件,其内容如下:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<assemblyIdentity
    version="6.0.0.0"
    processorArchitecture="x86"
    name="mint.EXE"
    type="win32"
/>
<description>Win32 Program</description>
<trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel
          level="requireAdministrator" uiAccess="false"/>
        </requestedPrivileges>
       </security>
  </trustInfo>
</assembly>


接下来我们在MASM32的集成开发环境QEDITOR的中打开Icztutes的TUTE02中的源代码文件MSGBOX.ASM,其内容如下:

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib kernel32.lib
include \masm32\include\user32.inc
includelib user32.lib
.data
MsgCaption      db "Iczelion's tutorial no.2",0
MsgBoxText      db "Win32 Assembly is Great!",0
.code
start:
  invoke MessageBox, NULL,addr MsgBoxText, addr MsgCaption, MB_OK
  invoke ExitProcess,NULL
end start


然后另存为runAsAdminOnly.ASM,使用菜单:Project-> Build All,如果编译成功,以Guest帐户运行生成的runAsAdminOnly.exe,就会看到UAC提示框:

如果你编译出错,可以修改源代码如下:

复制  

.386
.model flat, stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib
.data
MsgCaption      db "Iczelion's tutorial no.2",0
MsgBoxText      db "Win32 Assembly is Great!",0
.code
start:
  invoke MessageBox, NULL,addr MsgBoxText, addr MsgCaption, MB_OK
  invoke ExitProcess,NULL
end start
相关文章
win7/10环境下VC++开发软件,怎么让exe有权限在c盘写文件
win7/10环境下VC++开发软件,怎么让exe有权限在c盘写文件
419 0
|
Shell Windows
VBS脚本代码(手工编写---在windows 7上调用系统对话框,来选择文件)
'=========================================================================='' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.
975 0
|
数据安全/隐私保护
通过VBS编写自动输入账号和密码、自动登录程序的脚本
通过VBS编写自动输入账号和密码、自动登录的脚本。 请查看附件: 附件中是以QQ为例。 这个脚本的好处是: 1、可以用于开机自动登录 2、可以用于运维人员自动登录一些程序 3、可以用于......
3355 0
|
C#
WPF程序 双击exe自动申请“以管理员方式运行”权限
原文:WPF程序 双击exe自动申请“以管理员方式运行”权限 实现方式: 在 xxx.exe 目录下包含其对应的清单文件(xxx.exe.manifest); 用记事本打开 manifest 文件,将文件中的项:更改为:
1361 0