欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

看黑客怎样汇编创建简单的窗口

程序员文章站 2022-04-07 08:02:00
理论: Windows 程序中,在写图形用户界面时需要调用大量的标准 Windows Gui 函数。其实这对用户和程序员来说都有好处,对于用户,面对的是同一套标准的窗口,对这些窗口的操作都... 08-10-08...
理论: windows 程序中,在写图形用户界面时需要调用大量的标准 windows gui 函数。其实这对用户和程序员来说都有好处,对于用户,面对的是同一套标准的窗口,对这些窗口的操作都是一样的,所以使用不同的应用程序时无须重新学习操作。对程序员来说,这些 gui 源代码都是经过了微软的严格测试,随时拿来就可以用的。当然至于具体地写程序对于程序员来说还是有难度的。为了创建基于窗口的应用程序,必须严格遵守规范。作到这一点并不难,只要用模块化或面向对象的编程方法即可。 下面我就列出在桌面显示一个窗口的几个步骤: 得到您应用程序的句柄(必需);
得到命令行参数(如果您想从命令行得到参数,可选);
注册窗口类(必需,除非您使用 windows 预定义的窗口类,如 messagebox 或 dialog box;
产生窗口(必需);
在桌面显示窗口(必需,除非您不想立即显示它);
刷新窗口客户区;
进入无限的获取窗口消息的循环;
如果有消息到达,由负责该窗口的窗口回调函数处理;
如果用户关闭窗口,进行退出处理。
相对于单用户的 dos 下的编程来说,windows 下的程序框架结构是相当复杂的。但是 windows 和 dos 在系统架构上是截然不同的。windows 是一个多任务的操作系统,故系统中同时有多个应用程序彼此协同运行。这就要求 windows 程序员必须严格遵守编程规范,并养成良好的编程风格。 内容: 下面是我们简单的窗口程序的源代码。在进入复杂的细节前,我将提纲挈领地指出几点要点: 您应当把程序中要用到的所有常量和结构体的声明放到一个头文件中,并且在源程序的开始处包含这个头文件。这么做将会节省您大量的时间,也免得一次又一次的敲键盘。目前,最完善的头文件是 hutch 写的,您可以到 hutch 或我的网站下载。您也可以定义您自己的常量和结构体,但最好把它们放到独立的头文件中
用 includelib 指令,包含您的程序要引用的库文件,譬如:若您的程序要调用 "messagebox",您就应当在源文件中加入如下一行: includelib user32.lib 这条语句告诉 masm 您的程序将要用到一些引入库。如果您不止引用一个库,只要简单地加入 includelib 语句,不要担心链接器如何处理这么多的库,只要在链接时用链接开关 /libpath 指明库所在的路径即可。
在其它地方运用头文件中定义函数原型,常数和结构体时,要严格保持和头文件中的定义一致,包括大小写。在查询函数定义时,这将节约您大量的时间;
在编译,链接时用makefile文件,免去重复敲键。
.386
.model flat,stdcall
option casemap:none
include \masm32\include\windows.inc
include \masm32\include\user32.inc
includelib \masm32\lib\user32.lib ; calls to functions in user32.lib and kernel32.lib
include \masm32\include\kernel32.inc
includelib \masm32\lib\kernel32.lib winmain proto :dword,:dword,:dword,:dword .data ; initialized data
classname db "simplewinclass",0 ; the name of our window class
appname db "our first window",0 ; the name of our window .data? ; uninitialized data
hinstance hinstance ? ; instance handle of our program
commandline lpstr ?
.code ; here begins our code
start:
invoke getmodulehandle, null ; get the instance handle of our program.
; under win32, hmodule==hinstance mov hinstance,eax
mov hinstance,eax
invoke getcommandline ; get the command line. you don't have to call this function if
; your program doesn't process the command line.
mov commandline,eax
invoke winmain, hinstance,null,commandline, sw_showdefault ; call the main function
invoke exitprocess, eax ; quit our program. the exit code is returned in eax from winmain. winmain proc hinst:hinstance,hprevinst:hinstance,cmdline:lpstr,cmdshow:dword
local wc:wndclassex ; create local variables on stack
local msg:msg
local hwnd:hwnd mov wc.cbsize,sizeof wndclassex ; fill values in members of wc
mov wc.style, cs_hredraw or cs_vredraw
mov wc.lpfnwndproc, offset wndproc
mov wc.cbclsextra,null
mov wc.cbwndextra,null
push hinstance
pop wc.hinstance
mov wc.hbrbackground,color_window 1
mov wc.lpszmenuname,null
mov wc.lpszclassname,offset classname
invoke loadicon,null,idi_application
mov wc.hicon,eax
mov wc.hiconsm,eax
invoke loadcursor,null,idc_arrow
mov wc.hcursor,eax
invoke registerclassex, addr wc ; register our window class
invoke createwindowex,null,\
addr classname,\
addr appname,\
ws_overlappedwindow,\
cw_usedefault,\
cw_usedefault,\
cw_usedefault,\
cw_usedefault,\
null,\
null,\
hinst,\
null
mov hwnd,eax
invoke showwindow, hwnd,cmdshow ; display our window on desktop
invoke updatewindow, hwnd ; refresh the client area .while true ; enter message loop
invoke getmessage, addr msg,null,0,0
.break .if (!eax)
invoke translatemessage, addr msg
invoke dispatchmessage, addr msg
.endw
mov eax,msg.wparam ; return exit code in eax
ret
winmain endp wndproc proc hwnd:hwnd, umsg:uint, wparam:wparam, lparam:lparam
.if umsg==wm_destroy ; if the user closes our window
invoke postquitmessage,null ; quit our application
.else
invoke defwindowproc,hwnd,umsg,wparam,lparam ; default message processing
ret
.endif
xor eax,eax
ret
wndproc endp end start 分析: 看到一个简单的 windows 程序有这么多行,您是不是有点想撤? 但是您必须要知道的是上面的大多数代码都是模板而已,模板的意思即是指这些代码对差不多所有标准 windows 程序来说都是相同的。在写 windows 程序时您可以把这些代码拷来拷去,当然把这些重复的代码写到一个库中也挺好。其实真正要写的代码集中在 winmain 中。这和一些 c 编译器一样,无须要关心其它杂务,集中精力于 winmain 函数。唯一不同的是 c 编译器要求您的源代码有必须有一个函数叫 winmain。否则 c 无法知道将哪个函数和有关的前后代码链接。相对c,汇编语言提供了较大的灵活性,它不强行要求一个叫 winmain 的函数。 下面我们开始分析,您可得做好思想准备,这可不是一件太轻松的活。 .386
.model flat,stdcall
option casemap:none winmain proto :dword,:dword,:dword,:dword include \masm32\include\windows.inc
include \masm32\include\user32.inc
include \masm32\include\kernel32.inc
includelib \masm32\lib\user32.lib
includelib \masm32\lib\kernel32.lib 您可以把前三行看成是"必须"的. .386告诉masn我们要用80386指令集。
. model flat,stdcall告诉masm 我们用的内存寻址模式,此处也可以加入stdcall告诉masm我们所用的参数传递约定。 接下来是函数 winmain 的原型申明,因为我们稍后要用到该函数,故必须先声明。我们必须包含 window.inc 文件,因为其中包含大量要用到的常量和结构的定义,该文件是一个文本文件,您可以用任何文本编辑器打开它, window.inc还没有包含所有的常量和结构定义,不过 hutch 和我一直在不断加入新的内容。