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