首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 网络安全 > 黑客技术 > Win32Asm快速教程(10)
【标  题】:Win32Asm快速教程(10)
【关键字】:in,教程,Win,Win32,10,32,Wi,Win32Asm,10
【来  源】:网络

Win32Asm快速教程(10)

12.3-第三步
我们将要创建著名的“Hello World”程序。要显示“hello World”字符串,我们要用消息对话框。消息对话框由MessageBox函数创建。你可以在《win32 程序员参考》(看第二章)中查找这个函数。这是书上说的:

MessageBox函数创建,显示并操作消息对话框。消息对话框包含应用程序定义的消息和标题,加上任何预定义的图标与按钮的组合。

int MessageBox(
HWND hWnd, // handle of owner window
LPCTSTR lpText, // address of text in message box
LPCTSTR lpCaption, // address of title of message box
UINT uType // style of message box
);

Parameters

hWnd
Identifies the owner window of the message box to be created. If this parameter is NULL, the message box has no owner window.

lpText
Points to a null-terminated string containing the message to be displayed.

lpCaption
Points to a null-terminated string used for the dialog box title. If this parameter is NULL, the default title Error is used.

uType
Specifies a set of bit flags that determine the contents and behavior of the dialog box. This parameter can be a combination of flags from the following groups of flags.
[--SNIP--]

在这段文字后有所有常数和标志的列表(他们定义在windows.inc中)。因为它太长了,我没有在这里列出来。通过查看参考,你就知道MessageBox函数要4个参数:父窗口(owner),指向消息串的指针,指向标题串的指针和消息框的类型。

HWnd可以是Null。因为我们的程序没有窗口。
LpText必须是指向我们文本的指针。这仅仅意为参数是文本所在内存地址的offset。
LpCaption 是标题串的offset。
UType 是参考中解释的像MB_OK,MB_OKCANCEL,MB_ICONERROR等值的组合。

让我们先定义两个用于MessageBox的字符串:

在first.asm中加入:

.data

MsgText db "Hello world!",0
MsgTitle db "This is a messagebox",0

.data 指示data部分的开始。用db,字节直接被插入,而且字符串又只是字节的集合,data部分会在包含上面的字符串,附加上结尾的0。MsgText装有第一个字符串的offset。MsgTitle有第二个字符串的offset。现在我们可以使用函数:

invoke MessageBox, NULL, offset MsgText, offset MsgTitle, Null

但因为用的是invoke,你可以使用(更安全)ADDR代替offset:

invoke MessageBox, Null, ADDR MsgText, ADDR MsgTitle, Null

我们还没有看最后一个参数,但这不会有什么问题。因为MB_OK(有一个ok按钮的消息对话框的样式)等于0(NULL)。但你也可以使用其他的任何样式。Utype(第4个参数)的定义是:

指定一系列决定对话框内容与行为的位标志。这个参数可以是下面标志组中标志的组合。

现在以我们要一个有OK按钮与“information”图标的简单消息对话框为例。MB_OK是OK按钮的样式,MB_ICONINFORMATION是information图标的样式。样式是用“or”操作符联合的。这不是or伪代码。Masm会在汇编前处理or操作。不用or,你可以用+号(加号)代替,但有时对层叠样式有问题(一个样式包含其他一些样式)。但在本例中你也可以用+号。

.code

start:

invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK + MB_ICONINFORMATION

end start

把以上的代码加入到你的first.asm文件中。

我们还加入了一个start标签。如果你现在汇编你的程序并运行它,它将显示一个消息对话框但很有可能在你点OK之后就崩溃了。这是因为程序没有结束,而处理器开始执行MessageBox代码后的任何东西。Windows中程序是用ExitProcess函数结束的:

VOID ExitProcess(
UINT uExitCode //对于所有线程的退出代码 );

我们可以把0用作退出码。

把你的代码改成这样:

.code

start:

invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK + MB_ICONINFORMATION
invoke ExitProcess, NULL

end start

12.4-第4步
因此我们最终的程序是:

.486
.model flat, stdcall

option casemap:none

includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib

include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
include \masm32\include\windows.inc

.data
MsgText db "Hello world!",0
MsgTitle db "This is a messagebox",0

.code

start:
invoke MessageBox, NULL, ADDR MsgText, ADDR MsgTitle, MB_OK or MB_ICONINFORMATION
invoke ExitProcess, NULL

end start

Win32Asm快速教程(11):【上一篇】
Win32Asm快速教程(9):【下一篇】
【相关文章】
  • Win32Asm快速教程(11)
  • Win32Asm快速教程(12)
  • Win32Asm快速教程(13)
  • Win32Asm快速教程(14)
  • win9x/Winnt/Win2k/Winxp病毒技术探讨 上
  • win9x/Winnt/Win2k/Winxp病毒技术探讨 下
  • WinNT & Win2K下实现进程的完全隐藏
  • 用命令行写IPSec(win)
  • Windows优化大师注册机源代码
  • windows进程中的内存结构(1)
  • 【随机文章】
  • 走向JSP高手之路
  • 不用软件轻松克隆硬盘
  • Oracle中自增字段的两种方法的比较(Trigger和Sequence)
  • 只能在保护模式下执行的指令
  • IWNCOMM 企业级室内型接入点(AP)
  • 揭露伪环保健康显示器
  • bug报告模板
  • 优化TryCatch性能
  • Agile Web Development with Rails 翻译(十一)
  • HzorInline v 1.3 beta build 20050901
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.