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

136 篇文章 0 订阅
19 篇文章 0 订阅

为提高系统安全性,微软从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

在尝试过程中参考了:http://masm32.com/board/index.php?topic=1235.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

紫郢剑侠

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值