Win32 assembly language tutorial

xiaoxiao2021-03-06  14

Win32 assembly language tutorial

If you need assembly language writing, please send E-mail: billows-@hotmail.com

1. Introduction Win32 applications generally use C language programming, but in the case of deep programming, such as Win32 application execution mechanism analysis, virus clearance, encrypted decryption, etc., or high requirements for some speed requirements Program, you need to use assembly language (or even machine languages) to write Win32 applications directly. Win32 applications can use 386 assembly language and protection mode programming, but the implementation mechanism of Win32 applications has certain differences with other 32-bit applications, such as messages. Cycling, dynamic link, etc., Win32 assembly language also has its special programming methods. At present, there are very few information about Win32 assembly language. The assembly language books on the market generally only introduce the DOS real model assembly language and 386 protection model assembly language. Although Jinshan Company's "In-depth Windows Programming" book is introduced to use assembly language Write a WINDOWS application method, unfortunately, the book only introduces Win16 assembly language. In order to make everyone can have a certain understanding of the basic programming method of Win32 assembly language, the author has written this tutorial, which is designed to throw bricks. If this tutorial can lead you into the mysterious Win32 assembly language world, the author is willing to Using this tutorial, the reader is required to have the foundation of the C language to write Win32 applications (Win32SDK programming). Second, the basic software for Win32 assembly language is performed for Win32 assembly language programming. It should be prepared for the following basic software: 1. MASM 6.11 or later assembler MASM is the container of Microsoft, this is the most basic software, required MASM 6.11 or more The version can assemble the Win32 assembly language source program. However, the Win32 assembly language programming does not have to be full set of MASM 6.11, as long as a ml.exe file is OK, Windows 95 DDK has ML.EXE files with MASM 6.11D in Windows 98 DDK. EXE files can be used. Turbo Masm 5.0 is a compiler of Borland, or it can be used to assemble the Win32 assembly language source program, but the Some syntax of TASM is different from MASM, and the Win32 assembly language source program for MASM may need to be modified to use TASM compilation. . All Win32 assembly language source programs in this tutorial are based on MASM. 2, WIN32SDK for WIN32 assembly language programming requires the resource compiler (rc.exe) and connectors in Win32SDK, and the introduction library file in Win32SDK (kernel32.lib, user32.lib, GDI32.LIB, etc.). If there is no Win32SDK, Platform SDK can also be installed in Visual C , and the author uses Visual C 6.0. Borland C 4.0 or above Borland C can also be used, only the file names of the resource compiler and the connector are different, namely brc.exe (brc32.exe), and TLINK.exe (TLINK32.exe), the options are not the same, In addition, Borland C does not support the COFF format OBJ file, and the / COFF option cannot be used when assembled. 3, assembly language editor A normal text editor for editing the Win32 assembly language source program.

Edit, PWB, etc., the editor in the programming language such as Visual C can also, even Word, WPS 97, etc. can edit the word processing software for text files, but the author recommends using AsMedit, this is a dedicated assembly language editor. The effect is very good. Win32 assembly language generally uses a command line compilation connection. After some settings, it can also be connected under certain integrated environments (PWB, Visual C , AsMedit, etc.), and you can use NMAKE tools, but only use command line in this tutorial. Compilation connection or NMAKE tools are not used. Third, the ANSI Character Set API and Unicode Characters APIWIN32 API has two different types of different types: ANSI character set API and Unicode character set API, respectively correspond to ANSI characters and Unicode characters, Windows NT supports two kinds Type API, Windows 95/98 only supports an ANSI character set API. In Windows.h header files and other Win32 API definition header files, all APIs related to characters have two different definitions, and the ANSI character set API indicates that the API name is characterized by characters "a", Unicode character set API with API name plus the characters "W" indicates, using conditional compilation and macro definitions use the API defined automatically according to the current corresponding to the character set, e.g. GetModuleHandle defined function (including the header file WINBASE.H): WINBASEAPIHMODULEWINAPIGetModuleHandleA (LPCSTR lpModuleName); WINBASEAPIHMODULEWINAPIGetModuleHandleW (LPCWSTR LPModulenAme); # neydef unicode # define getModuleHandle GetModuleHandlew # Else # define getModuleHandle getModuleHandlea # Endif //! Unicode also has similar definitions. This tutorial takes into account the assembly of the assembly language, which will lead to less intuitive, all using the ANSI character set API, which can also ensure compatibility in the Windows 95/98 and Windows NT environments, so many API names and data structures in this tutorial The name is added with "A" characters, and the reader can easily switch to the Unicode character set API. Fourth, a simple Win32 assembly language program reader may feel a headache when he heard the four words of "assembly language"! The first impression of the assembly language is that a lot of difficulties are difficult to understand, and they are not structured, a large number of labels, unconditional jumping instructions (JMP) and condition jump instructions make it difficult to understand the program; process (Or function) call parameters are not intuitive, either directly use the register to pass parameters, do not meet the structural program design principles; either use the stack pass parameters, and cannot effectively verify the parameter type ... You want Win32 assembly language more troublesome! Fortunately, MASM 6.0 or more versors provide a lot of structured assembly language pseudo-instructions, which can easily implement assembly language structured programming. When you read this tutorial, you may feel that Win32 assembly language is not C is much more troublesome.

(If the reader does not understand the assembly language source program in this tutorial, you can look at the help of Masm 6.11) and the C language Win32 programming requires Windows.h header files and other Win32 API definition header file definition constants, data structure, and API Same, Win32 assembly language also needs to include files (INC files) to define constants, data structures, and APIs. However, the author found a long time and did not find a complete Windows.inc file or Win32.inc file (in the Win16 assembly language), the Windows.inc file used for Win16 assembly language, provided in Turbo Masm 5.0. Win32.inc files are not complete, only for the comes with the WAP32 example of the WAP32, and is less compatible with MASM 6.11 (I heard that there is a complete Win32.inc file in NASM, but unfortunately, I don't know if it is not known as Masm 6.11. compatible). The author has to define constants, data structures, and APIs (defined according to Windows.h header files and other Win32 API definitions), but this has brought a lot of benefits - better understanding the programming method of Win32 assembly language and principle. The author wrote a simple Win32 assembly language program that is simple to display: a message box is displayed on the screen. This procedure is called only two API functions: Functions and the MessageBox ExitProcess functions, procedures are as follows: File contains (MSGBOX.INC): UINT TYPEDEF DWORDLPSTR TYPEDEF PTR BYTELPCSTR TYPEDEF LPSTRPVOID TYPEDEF PTRHANDLE TYPEDEF PVOIDHWND TYPEDEF HANDLEMB_ICONINFORMATION = 00000040hMB_OK = 00000000hMessageBoxA PROTO stdcall,: HWND ,: LPCSTR,: LPCSTR,: UINTExitProcess PROTO stdcall,: UINT source (MSGBOX.ASM) :. 386p.MODEL flat, stdcallINCLUDE MSGBOX.INC.STACK 4096.DATAWindowTitle BYTE 'MsgBox', 0Message1 BYTE 'This is a simple MessageBox win32 application ', 0.CODE_start:. INVOKE MessageBoxA, 0, ADDR Message1, ADDR WindowTitle, MB_ICONINFORMATION or MB_OKINVOKE ExitProcess, 0PUBLIC _startEND assembler connected to this program are as follows: ml / c / coff / Cp msgbox.asmlink / subsystem: windows / Entry: _Start msgbox.obj kernel32.lib user32.lib assembly commands / c options in the assembly command represents only assembly, not automatically connected; / COFF option represents the OBJ file generated in the COFF format (if you use the Borland connector to use / Coff parameters) ; / CP option indicates that the identifier is case sensitive. In the connection command / subsystem: windows option indicates that the connector generates a normal Windows executable; / entry: _Start option indicates that the program entry point is the _start identifier. Connect to kernel32.lib and user32.lib when connecting.

Run the MSGBox.exe file generated after the assembly connection, a message box will appear on the screen, the title of the message box is "msgbox", the string in the message box is "this is a simple messagebox win32 application.". Win32 assembly language source program should start from .386P directive and .Model flat, StdCall directive, indicate assembler assembly 386 protection mode instruction, and use flat memory mode (Win32 memory mode) and stdcall function call mode (Win32 standard function call) the way). The proto directive definition function prototype (similar to the definition of the function prototype in the C language), can define a function name, call mode, and parameters, invoke directives to call the function defined by the PROTO directive, which can easily deliver parameters and check parameter types. Using the PROTO directive in the msgbox.inc file, use the Invoke directive in the msgbox.asm file to call the API function. It can be seen that the structured assembly language provider provided by the MASM 6.0 or later versatile is greatly simplified by Win32 assembly language programming. This program is not used by a assembly language instruction). This program calls the Messagebox function display message box, call the execution of the EXITPROCESS function termination program, the effect of the EXITPROCESS function is to terminate the current process.

V. V. Win32 assembly language program to display a window learned the first application writer writer writer of Win32SDK programming that the C language program that displays a window, and the author also wrote such a C language program, the program file named Simple. C, the following program: #include static char szWindowClass [] = "SIMPLE"; LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain (hINSTANCE hInstance, hINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {WNDCLASSEXA wcex; HWND hWnd; MSG msg; if {wcex.cbSize = sizeof (WNDCLASSEXA); wcex.style = CS_HREDRAW | CS_VREDRAW; (hPrevInstance!) wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.lpfnWndProc = WndProc ; wcex.hInstance = hInstance; wcex.hIcon = LoadIconA (hInstance, IDI_APPLICATION); wcex.hCursor = LoadCursorA (0, IDC_ARROW); wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW 1); wcex.lpszMenuName = NULL; wcex. lpszClassName = szWindowClass; wcex.hIconSm = LoadIconA (hInstance, IDI_APPLICATION); if return FALSE (RegisterClassExA (& wcex)!);} hWnd = CreateWindowExA (0, szWindowClass, "SIMPLE", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0 , 0, hinstance, null; if (! Hw) Return F ALSE; ShowWindow (hWnd, nShowCmd); UpdateWindow (hWnd); while (GetMessageA (& msg, 0,0,0)) {TranslateMessage (& msg); DispatchMessageA (& msg);} return msg.wParam;} LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hDC; PAINTSTRUCT ps; switch (message) {case WM_PAINT: hDC = BeginPaint (hWnd, & ps); EndPaint (hWnd, & ps); return 0; case WM_DESTROY: PostQuitMessage ( 0); RETURN 0; Default: Return DefWindowProca (hwnd, message, wparam, lparam);} return -1;} This program is basically the same as the general display of a window, just only using the ANSI character set.

Now I assembler language program function realization of this program with Win32, procedure is as follows: include files (SIMPLE.INC): UINT TYPEDEF DWORDLONG TYPEDEF DWORDLPSTR TYPEDEF PTR BYTELPCSTR TYPEDEF LPSTRPVOID TYPEDEF PTRLPVOID TYPEDEF PVOIDHANDLE TYPEDEF PVOIDHINSTANCE TYPEDEF HANDLEHWND TYPEDEF HANDLEHMENU TYPEDEF HANDLEHDC TYPEDEF HANDLEHGDIOBJ TYPEDEF HANDLEHICON TYPEDEF HANDLEHCURSOR TYPEDEF HANDLEHBRUSH TYPEDEF HANDLEtagWNDCLASSEXA STRUCTcbSize UINT? style UINT? lpfnWndProc DWORD? cbClsExtra DWORD? cbWndExtra DWORD? hInstance DWORD? hIcon DWORD? hCursor DWORD? hbrBackground DWORD? lpszMenuName DWORD? lpszClassName DWORD? hIconSm DWORD? tagWNDCLASSEXA ENDSWNDCLASSEXA TYPEDEF tagWNDCLASSEXAtagPOINT STRUCTx LONG? y LONG? tagPOINT ENDSPOINT TYPEDEF tagPOINTtagMSG STRUCTmessage UINT? wParam DWORD? lParam DWORD? time DWORD? pt POINT <> tagMSG ENDSMSG TYPEDEF tagMSGLPMSG TYPEDEF PTR MSGtagRECT STRUCTleft LONG? top LONG? right LONG? bottom LONG? tagRECT ENDSRECT TYPEDEF tagRECTtagPAINTS TRUCT STRUCThdc DWORD? FErase DWORD? RcPaint RECT <> fRestore tagPAINTSTRUCT ENDSPAINTSTRUCT TYPEDEF tagPAINTSTRUCTLPPAINTSTRUCT TYPEDEF PTR PAINTSTRUCTNULL = 0TRUE = 0ffffffffhFALSE = 0SW_SHOWDEFAULT = 10CS_HREDRAW = 0002hCS_VREDRAW = 0001hIDI_APPLICATION = 32512IDC_ARROW = 32512COLOR_WINDOW = 5WS_OVERLAPPEDWINDOW DWORD? FIncUpdate DWORD? RgbReserved BYTE 32 DUP (?) = 00cf0000hcw_usededefault = 80000000HWM_PAINT = 000fHWM_DESTROY =

0002hGetModuleHandleA PROTO stdcall,: LPCSTRGetCommandLineA PROTO stdcallExitProcess PROTO stdcall,: UINTLoadIconA PROTO stdcall,: HINSTANCE,: LPCSTRLoadCursorA PROTO stdcall,: HINSTANCE,: LPCSTRRegisterClassExA PROTO stdcall,: PTR WNDCLASSEXACreateWindowExA PROTO stdcall,: DWORD,: LPCSTR,: LPCSTR,: DWORD, : DWORD,: DWORD,: DWORD,: DWORD,: HWND,: HMENU,: HINSTANCE,: LPVOIDShowWindow PROTO stdcall,: HWND,: DWORDUpdateWindow PROTO stdcall,: HWNDGetMessageA PROTO stdcall,: LPMSG,: HWND,: UINT,: UINTTranslateMessage PROTO stdcall,: PTR MSGDispatchMessageA PROTO stdcall,: PTR MSGBeginPaint PROTO stdcall,: HWND,: LPPAINTSTRUCTEndPaint PROTO stdcall,: HWND,: PTR PAINTSTRUCTPostQuitMessage PROTO stdcall,: DWORDDefWindowProcA PROTO stdcall,: HWND,: UINT,: DWORD,: DWORD source (SIMPLE.ASM) :. 386p.MODEL flat, stdcallINCLUDE SIMPLE.INCWinMain PROTO stdcall,: HINSTANCE,: HINSTANCE,: LPSTR,: DWORD.STACK 4096.DATAWindowClass BYTE 'SIMPLE', 0WindowTitle BYTE 'SIMPLE', 0hInst1 HINSTANCE 0lpCmdLine1 LPSTR 0.code_start: i NVOKE GetModuleHandleA, NULLmov hInst1, eaxINVOKE GetCommandLineAmov lpCmdLine1, eaxINVOKE WinMain, hInst1, NULL, lpCmdLine1, SW_SHOWDEFAULTINVOKE ExitProcess, eax WinMain PROC hInst: HINSTANCE, hPrevInst: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: DWORDLOCAL wcex: WNDCLASSEXALOCAL hWnd: HWNDLOCAL msg: MSG. IF! hPrevInst mov wcex.cbSize, SIZEOF WNDCLASSEXA mov wcex.style, CS_HREDRAW or CS_VREDRAW mov wcex.cbClsExtra, 0 mov wcex.cbWndExtra, 0 mov wcex.lpfnWndProc, OFFSET WndProc mov eax, hInst mov wcex.hInstance, eax INVOKE LoadIconA, Hinst, IDi_Application Mov Wcex.hicon, Eax Invoke Loadcursora, 0, IDC_ARROW MOV WCEX.HCURSOR, EAX MOV WCEX.HBRBACKGROUND, Color_Window

1 mov wcex.lpszMenuName, NULL mov wcex.lpszClassName, OFFSET WindowClass INVOKE LoadIconA, hInst, IDI_APPLICATION mov wcex.hIconSm, eax INVOKE RegisterClassExA, ADDR wcex .IF! Eax mov eax, FALSE ret .ENDIF.ENDIFINVOKE CreateWindowExA, 0, ADDR WindowClass , ADDR WindowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0,0, hInst, NULLmov hWnd, eax.IF! eax mov eax, FALSE ret.ENDIFINVOKE ShowWindow, hWnd, nShowCmdINVOKE UpdateWindow, hWnd.WHILE TRUE INVOKE GetMessageA, ADDR ! msg, 0,0,0 .BREAK .IF eax INVOKE TranslateMessage, ADDR msg INVOKE DispatchMessageA, ADDR msg.ENDWmov eax, msg.wParamretWinMain ENDPWndProc PROC hWnd: HWND, message: UINT, wParam: DWORD, lParam: DWORDLOCAL hDC: HDCLOCAL ps: PAINTSTRUCT.IF message == WM_PAINT INVOKE BeginPaint, hWnd, ADDR ps mov hDC, eax INVOKE EndPaint, hWnd, ADDR ps mov eax, 0 ret.ELSEIF message == WM_DESTROY INVOKE PostQuitMessage, 0 mov eax, 0 ret.ELSE INVOKE DefWindowProca, Hwnd, Message, WParam, LParam Ret.Endifmov Eax, 0FFFFFFFFHRETWNDP ROC ENDPPUBLIC _STARTEND Compilation The command of the program is as follows: ml / c / coff / cp simple.asmlink / subsystem: windows / entry: _Start Simple.obj kernel32.lib user32.lib gdi32.lib Run SIMPLE.exe after running assembly Document, a standard window will appear on the screen, the title of the window is "Simple". Readers who have learned Win32SDK know that the entry point of the Win32 application is a winmain function. In fact, the WinMain function is called by the initialization and end code of the C language, and the true entry point of the Win32 application is nothing difference. Both the application starting point specified in the file header.

Win32 assembly language does not have a C language initialization and end code, must write initialization and end code calling WinMain functions (procedure), the prototype of Winmain function is: int WinStance Hinstance, Hinstance Hprevinstance, lpstr lpcmdline, int nshowcmd; WinMain There are 4 parameters, namely: Hinstance-- The handle of the current instance can be obtained by calling the getModuleHandle function. HPREVINSTANCE - the handle of the previous instance of the application, there will be no application in the current address space in Win32. The instance is running, which is typically set to NULL (providing this parameter is only easy to port Win16 application source program). LPCMDLINE - command line parameters can be obtained by calling the getcommandline function. The NSHOWCMD - the display status of the main window can be set to sw_showdefault. This program initialization and end code as follows: INVOKE GetModuleHandleA, NULLmov hInst1, eaxINVOKE GetCommandLineAmov lpCmdLine1, eaxINVOKE WinMain, hInst1, NULL, lpCmdLine1, SW_SHOWDEFAULTINVOKE ExitProcess, eax Win32 standard function call, the function returns the value returned by the EAX register. The initialization and end code of this program is simple, just call the getModuleHandle function to obtain the handle of the application's current instance, call the getcommandline function to obtain the command line parameter, then call the WinMain function (process), the winmain function returns the execution of the EXITPROCESS function termination program . Comparing this program with Simple.c programs, you can see that the program structure is very similar, and this program uses a large number of structural assembly language providers provided by MASM 6.0 or higher, not much trouble than C language.

Sixth, resources in Win32 assembly language programs are very important in Win32 applications. The author wrote a C language program that uses resources. The program implements the menu and a "About" dialog, the program is as follows: Source program (GENERIC.C): # include #include "resource.h" static HINSTANCE hInst; static char szWindowClass [] = "GENERIC"; LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam); BOOL CALLBACK AboutDlgProc (HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam); int WINAPI WinMain (hINSTANCE hInstance, hINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) {WNDCLASSEXA wcex; HWND hWnd; MSG msg; hInst = hInstance; if {(hPrevInstance!) wcex.cbSize = sizeof (WNDCLASSEXA); wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.cbClsExtra = 0; wcex.cbWndExtra = 0; wcex.lpfnWndProc = WndProc; wcex.hInstance = hInstance; wcex.hIcon = LoadIconA (hInstance, IDI_APPLICATION ); wcex.hCursor = LoadCursorA (0, IDC_ARROW); wcex.hbrBackground = (HBRUSH) (COLOR_WINDOW 1); wcex.lpszMenuName = MAKEINTRESOURCE (IDR_MAINMENU); wcex.lpszClassName = szWindowClass; wcex.hIconSm = LoadIconA (hInstance, IDI_APPLICATION ); If (! RegisterClassexa (& WCEX)) Return False;} hwnd = Creat eWindowExA (0, szWindowClass, "Generic", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0,0, hInstance, NULL); if (! hWnd) return FALSE; ShowWindow (hWnd, nShowCmd); UpdateWindow (hWnd); while (GetMessageA (& msg, 0,0,0)) {TranslateMessage (& msg); DispatchMessageA (& msg);} return msg.wParam;} LRESULT CALLBACK WndProc (HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) {HDC hDC; PaintStruct PS; Switch (Message) {Case WM_COMMAND: Switch (WPARAM)) {CASE IDM_EXIT: SendMessagea (HWND, WM_CLOSE, 0, 0); Return 0;

case IDM_ABOUT: DialogBoxParamA (hInst, MAKEINTRESOURCE (IDD_ABOUT), hWnd, (DLGPROC) AboutDlgProc, 0); return 0; default: return DefWindowProcA (hWnd, message, wParam, lParam);} case WM_PAINT: hDC = BeginPaint (hWnd, & ps ); EndPaint (hWnd, & ps); return 0; case WM_DESTROY: PostQuitMessage (0); return 0; default: return DefWindowProcA (hWnd, message, wParam, lParam);} return -1;} BOOL CALLBACK AboutDlgProc (HWND hDlg, Uint Message, WPARAM WPARAM, LPARAM LPARAM) {Switch (Message) {Case WM_INITDIALOG: RETURN TRUE; Case WM_Command: IF ((WPARAM) == iDok) || (WPARAM) == IDCANCEL) {endDialog HDLG, LOWORD (WPARAM); RETURN true; default: returnaf false;}}} resource source file (generic.rc, built using Visual C 6.0): // Microsoft Developer Studio generated resource script.//#include "resource.h" #define APSTUDIO_READONLY_SYMBOLS ///// Generated from the TEXTINCLUDE 2 resource.//#include "afxres.h" / # undef APSTUDIO_READONLY_SYMBOLS /// Chinese (PRC) resources # if! defined (AFX_RESOURCE_DLL) || Defined (AFX_TARG_CHS) #i fdef _WIN32LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED # pragma code_page (936) #endif // _ WIN32 ///// Menu // IDR_MAINMENU MENU DISCARDABLE BEGIN POPUP "& File" BEGIN MENUITEM "& Exit", IDM_EXIT END POPUP "& Help" BEGIN MENUITEM "& About .. . ", Idm_about ended # ifdef apstudio_invoked ///// TEXTINCLUDE / / 1 TEXTINCLUDE DISCARDABLE BEGIN" inclus "in" "/ 0" "/ r / n" "/ r / n" End3 textinclude discardable begin "/ r / n" "/ 0"

END # endif // APSTUDIO_INVOKED ///// Dialog // IDD_ABOUT DIALOG DISCARDABLE 0, 0, 92, 65STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENUCAPTION "About" FONT 9, "Times New Roman" BEGIN LTEXT "Generic V1.0" , IDC_STATIC1,20,10,50,10 DEFPUSHBUTTON "OK", IDOK, 20,40,50,15END ///// DESIGNINFO // # ifdef APSTUDIO_INVOKEDGUIDELINES DESIGNINFO DISCARDABLE BEGIN IDD_ABOUT, DIALOG BEGIN LEFTMARGIN, 7 rIGHTMARGIN, 85 TOPMARGIN, 7 BOTTOMMARGIN, 58 ENDEND # endif // APSTUDIO_INVOKED # endif // Chinese (PRC) resources / # ifndef APSTUDIO_INVOKED ///// Generated from the TEXTINCLUDE 3 resource.///#endif // not APSTUDIO_INVOKED resource header file (rESOURCE. H, Visual C 6.0 automatically created): // {{NO_DEPENDENCIES}} // Microsoft Developer Studio generated include file.// Used by GENERIC.rc // # define IDR_MAINMENU 101 # define IDD_ABOUT 102 # define IDC_STATIC1 1000 # define IDM_EXIT 40001 #define idm_abou T 40002 // Next default values ​​for new objects // #ifdef APSTUDIO_INVOKED # ifndef GENERIC program APSTUDIO_READONLY_SYMBOLS # define _APS_NEXT_RESOURCE_VALUE 103 # define _APS_NEXT_COMMAND_VALUE 40003 # define _APS_NEXT_CONTROL_VALUE 1001 # define _APS_NEXT_SYMED_VALUE 101 # endif # endif This procedure and the general profile substantially Win32 programming The same, just use only the ANSI character set.

Now I assembler language program function realization of this program with Win32, procedure is as follows: include files (GENERIC.INC): UINT TYPEDEF DWORDLONG TYPEDEF DWORDLPSTR TYPEDEF PTR BYTELPCSTR TYPEDEF LPSTRPVOID TYPEDEF PTRLPVOID TYPEDEF PVOIDHANDLE TYPEDEF PVOIDHINSTANCE TYPEDEF HANDLEHWND TYPEDEF HANDLEHMENU TYPEDEF HANDLEHDC TYPEDEF HANDLEHGDIOBJ TYPEDEF HANDLEHICON TYPEDEF HANDLEHCURSOR TYPEDEF HANDLEHBRUSH TYPEDEF HANDLEtagWNDCLASSEXA STRUCTcbSize UINT? style UINT? lpfnWndProc DWORD? cbClsExtra DWORD? cbWndExtra DWORD? hInstance DWORD? hIcon DWORD? hCursor DWORD? hbrBackground DWORD? lpszMenuName DWORD? lpszClassName DWORD? hIconSm DWORD? tagWNDCLASSEXA ENDSWNDCLASSEXA TYPEDEF tagWNDCLASSEXAtagPOINT STRUCTx LONG? y LONG? tagPOINT ENDSPOINT TYPEDEF tagPOINTtagMSG STRUCTmessage UINT? wParam DWORD? lParam DWORD? time DWORD? pt POINT <> tagMSG ENDSMSG TYPEDEF tagMSGLPMSG TYPEDEF PTR MSGtagRECT STRUCTleft LONG? top LONG? right LONG? bottom LONG? tagRECT ENDSRECT TYPEDEF tagRECTtagPAINT STRUCT STRUCThdc DWORD? FErase DWORD? RcPaint RECT <> fRestore tagPAINTSTRUCT ENDSPAINTSTRUCT TYPEDEF tagPAINTSTRUCTLPPAINTSTRUCT TYPEDEF PTR PAINTSTRUCTNULL = 0TRUE = 0ffffffffhFALSE = 0SW_SHOWDEFAULT = 10CS_HREDRAW = 0002hCS_VREDRAW = 0001hIDI_APPLICATION = 32512IDC_ARROW = 32512COLOR_WINDOW = 5WS_OVERLAPPEDWINDOW DWORD? FIncUpdate DWORD? RgbReserved BYTE 32 DUP (?) = 00cf0000HCW_USEDEFAULT = 80000000HWM_COMMAND = 0111HWM_Close = 0010HWM_PAINT = 000fHWM_DESTROY = 0002HWM_INITDIALOG = 0110Hidok = 1 IDCANCANCEL =

2GetModuleHandleA PROTO stdcall,: LPCSTRGetCommandLineA PROTO stdcallExitProcess PROTO stdcall,: UINTLoadIconA PROTO stdcall,: HINSTANCE,: LPCSTRLoadCursorA PROTO stdcall,: HINSTANCE,: LPCSTRRegisterClassExA PROTO stdcall,: PTR WNDCLASSEXACreateWindowExA PROTO stdcall,: DWORD,: LPCSTR,: LPCSTR,: DWORD, : DWORD,: DWORD,: DWORD,: DWORD,: HWND,: HMENU,: HINSTANCE,: LPVOIDShowWindow PROTO stdcall,: HWND,: DWORDUpdateWindow PROTO stdcall,: HWNDGetMessageA PROTO stdcall,: LPMSG,: HWND,: UINT,: UINTTranslateMessage PROTO stdcall,: PTR MSGDispatchMessageA PROTO stdcall,: PTR MSGSendMessageA PROTO stdcall,: HWND,: UINT,: DWORD,: DWORDDialogBoxParamA PROTO stdcall,: HINSTANCE,: LPCSTR,: HWND,: DWORD,: DWORDBeginPaint PROTO stdcall,: HWND ,: LPPAINTSTRUCTEndPaint PROTO stdcall,: HWND,: PTR PAINTSTRUCTPostQuitMessage PROTO stdcall,: DWORDDefWindowProcA PROTO stdcall,: HWND,: UINT,: DWORD,: DWORDEndDialog PROTO stdcall,: HWND,: DWORD resources include file (RESOURCE.INC): IDR_MAINMENU = 101IDD_ABOUT = 102IDC_STATIC1 = 1000IDM_E XIT = 40001IDM_ABOUT = 40002 source (GENERIC.ASM) :. 386p.MODEL flat, stdcallINCLUDE GENERIC.INCINCLUDE RESOURCE.INCWinMain PROTO stdcall,: HINSTANCE,: HINSTANCE,: LPSTR,: DWORD.STACK 4096.DATAWindowClass BYTE 'GENERIC', 0Windowtitle Byte 'Generic'

, 0hInst1 HINSTANCE 0lpCmdLine1 LPSTR 0.CODE_start: INVOKE GetModuleHandleA, NULLmov hInst1, eaxINVOKE GetCommandLineAmov lpCmdLine1, eaxINVOKE WinMain, hInst1, NULL, lpCmdLine1, SW_SHOWDEFAULTINVOKE ExitProcess, eax WinMain PROC hInst: HINSTANCE, hPrevInst: HINSTANCE, lpCmdLine: LPSTR, nShowCmd: DWORDLOCAL wcex : WNDCLASSEXALOCAL hWnd: HWNDLOCAL msg:! MSG.IF hPrevInst mov wcex.cbSize, SIZEOF WNDCLASSEXA mov wcex.style, CS_HREDRAW or CS_VREDRAW mov wcex.cbClsExtra, 0 mov wcex.cbWndExtra, 0 mov wcex.lpfnWndProc, OFFSET WndProc mov eax, hInst mov wcex.hInstance, eax INVOKE LoadIconA, hInst, IDI_APPLICATION mov wcex.hIcon, eax INVOKE LoadCursorA, 0, IDC_ARROW mov wcex.hCursor, eax mov wcex.hbrBackground, COLOR_WINDOW 1 mov wcex.lpszMenuName, IDR_MAINMENU and 0000ffffh mov wcex.lpszClassName , OFFSET WindowClass INVOKE LoadIconA, hInst, IDI_APPLICATION mov wcex.hIconSm, eax INVOKE RegisterClassExA, ADDR wcex .IF! eax mov eax, FALSE ret .ENDIF.ENDIFINVOKE CreateWindowExA, 0, ADDR WindowClass, ADDR WindowTitle, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0,0, hInst, NULLmov hWnd, eax.IF! Eax mov eax, FALSE ret.ENDIFINVOKE ShowWindow, hWnd, nShowCmdINVOKE UpdateWindow, hWnd.WHILE TRUE INVOKE GetMessageA, ADDR msg, 0, ! 0,0 .BREAK .IF eax INVOKE TranslateMessage, ADDR msg INVOKE DispatchMessageA, ADDR msg.ENDWmov eax, msg.wParamretWinMain ENDPWndProc PROC hWnd: HWND, message: UINT, wParam: DWORD, lParam: DWORDLOCAL hDC: HDCLOCAL ps: PAINTSTRUCT. If Message == WM_COMMAND MOV EAX, WPARAM .IF AX == IDM_EXIT Invoke SendMessagea, Hwnd, WM_Close, 0 ,0 MOV EAX, 0 Ret .elseif AX ==

IDM_ABOUT mov ebx, OFFSET AboutDlgProc INVOKE DialogBoxParamA, hInst1, IDD_ABOUT and 0000ffffh, hWnd, ebx, 0 mov eax, 0 ret .ELSE INVOKE DefWindowProcA, hWnd, message, wParam, lParam ret .ENDIF.ELSEIF message == WM_PAINT INVOKE BeginPaint, hWnd , ADDR ps mov hDC, eax INVOKE EndPaint, hWnd, ADDR ps mov eax, 0 ret.ELSEIF message == WM_DESTROY INVOKE PostQuitMessage, 0 mov eax, 0 ret.ELSE INVOKE DefWindowProcA, hWnd, message, wParam, lParam ret.ENDIFmov eax , 0ffffffffhretWndProc ENDPAboutDlgProc PROC hDlg: HWND, message: UINT, wParam: DWORD, lParam: DWORD.IF message == WM_INITDIALOG mov eax, TRUE ret.ELSEIF message == WM_COMMAND mov eax, wParam .IF (ax == IDOK) || (AX == idCancel) Invoke EndDialog, HDLG, AX MOV EAX, TRUE RET.Endif Mov Eax, False Ret.Else MOV EAX, FALSE RET.ELSE MOV EAX, FALSE RET.ENDIFMO EAX, FALSERETABOUTDLGPROC ENDPUBLIC _STARTEND assembly connection This program is as follows: ml / c / Coff / cp generic.asmrc generic.rclink / subsystem: windows / entry: _Start generic.obj generic.res kernel32.lib user32.lib gdi32.Exe files After running assembly, the generic.exe file generated after the assembly, will display on the screen A window with a menu, the title of the window is "generic", the menu has two main menu items, "file" and "help", select the "EXIT" menu item under the "File" menu item to exit the program , Select the "About" menu item under the "Help" menu item to display the "About" dialog. This program is very similar to the Simple.c program. The method of using resources in Win32 assembly language programs is also different from C language programs, which can generate resource source files and resource header files with resource editing tools, and then use the resource compiler. Compile the resource source file, connect the generated resource file (RES file) to the target file generated by the assembler and the introduction library file (resource header files need to be ported to assembly language), establish a resource containing files). The essence of the MakeintResource macro in the C language program is to clear the high level (not used) of the resource identifier value, and then forcibly convert to a character pointer, and the resource identifier value and 0000FFFFH will be used as an operation in the Win32 assembly language program.

转载请注明原文地址:https://www.9cbs.com/read-50383.html

New Post(0)