win32汇编——实现类似于mfc dll动态库

实现类似于mfc dll动态库

简介

注入程序后会显示窗口
以下写出模板代码

代码

有 2 种实现方法:

  1. 对话框

    mfc_dll.asm

    		.386
    		.model flat, stdcall
    		option casemap :none
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; Include 文件定义
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    include		windows.inc
    include		user32.inc
    includelib	user32.lib
    include		kernel32.inc
    includelib	kernel32.lib
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; Equ 等值定义
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ICO_MAIN	equ	1000	;图标
    DLG_MAIN	equ	1
    ID_TIMER1	equ	100
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; 常量
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    		.const
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; 数据
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    		.data?
    hInstance		dd	?
    
    		.data
    
    
    		.code
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; 代码
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    _ProcDlgMain	proc	uses ebx edi esi hWnd,wMsg,wParam,lParam
    
    		mov	eax,wMsg
    		.if	eax == WM_TIMER			; 计时器
    			mov	eax,wParam
    			.if	eax == ID_TIMER1
    				; ***********************************
    				; 计时器用不到可以删掉
    				; ***********************************
    
    			.endif
    		.elseif	eax == WM_CLOSE
    			invoke	EndDialog,hWnd,NULL
    
    		.elseif	eax == WM_INITDIALOG
    			; *******************************************
    			; 加载图标    这里图标无法显示,懂的可以说一下
    			; *******************************************
    			invoke	LoadIcon,hInstance,ICO_MAIN
    			invoke	SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
    			
    			; *******************************************
    			; 计时器初始化
    			; *******************************************
    			invoke SetTimer,hWnd,ID_TIMER1,250,NULL
    
    		.elseif	eax == WM_COMMAND
    			mov	eax,wParam
    			.if	ax == IDOK		; 确定
    				; ***********************************
    				; 这里输入你的代码
    				; ***********************************
    				
    			.elseif	ax == IDCANCEL		; 取消
    				; ***********************************
    				; 这里输入你的代码
    				; ***********************************
    				
    			.endif
    		.else
    			mov	eax,FALSE
    			ret
    		.endif
    		mov	eax,TRUE
    		ret
    
    _ProcDlgMain	endp
    
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; dll 的入口函数
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    DllEntry	proc	_hInstance,_dwReason,_dwReserved
    	
    	.If _dwReason == DLL_PROCESS_ATTACH
    		mov	eax,_hInstance
    		mov	hInstance,eax
    		; 创建对话框
    		invoke	DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL	
    		invoke	LoadIcon,hInstance,ICO_MAIN
    		invoke	SendMessage,hInstance,WM_SETICON,ICON_BIG,eax
    
    	.elseif	_dwReason == DLL_PROCESS_DETACH
    		invoke ExitProcess,hInstance
    		
    	.EndIf
    	mov Eax, TRUE 
    	ret
    DllEntry EndP
    
    End DllEntry
    
    

    mfc_dll.rc

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    #include		<resource.h>
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    
    #define	DLG_MAIN		1
    
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ICO_MAIN	ICON		"Main.ico"
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    DLG_MAIN DIALOG 50, 50,280, 180
    STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
    CAPTION "对话框模板"
    FONT 9, "宋体"
    {
          
          
    
     DEFPUSHBUTTON "确定(&X)", IDOK, 20, 120, 100, 30
     DEFPUSHBUTTON "取消", IDCANCEL, 160, 120, 100, 30
    }
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    

    mfc_dll.def

    EXPORTS
    

    Makefile

    DLL = mfc_dll
    RES = $(DLL).res
    
    ML_FLAG = /c /coff
    LINK_FLAG = /subsystem:windows /Dll
    
    ####################################################
    # 创建共享数据段的DLL时使用的连接选项
    # LINK_FLAG = /subsystem:windows /Dll /section:.bss,S
    ####################################################
    
    $(DLL).dll: $(DLL).obj $(DLL).def $(DLL).res
    	Link  $(LINK_FLAG) /Def:$(DLL).def $(DLL).obj $(DLL).res
    
    .asm.obj:
    	ml $(ML_FLAG) $<
    .rc.res:
    	rc $<
    
    clean:
    	del *.obj
    	del *.exp
    	del *.lib
    	del *.res
    
    

    效果
    在这里插入图片描述

  2. 窗口
    mfc_dll.asm

    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    		.386
    		.model flat, stdcall
    		option casemap :none
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; Include 文件定义
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    include		windows.inc
    include		user32.inc
    includelib	user32.lib
    include		kernel32.inc
    includelib	kernel32.lib
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; Equ 等值定义
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ICO_MAIN	equ	1000h		;图标
    ID_TIMER1	equ	100
    
    
    
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    		.data?
    hInstance	dd	?
    hWinMain	dd	?
    
    IDC_OK		dd	?	; 确定
    IDC_CANCEL	dd	?	; 取消
    IDC_LABEL1	dd	?	; 标签
    IDC_TEXT1	dd	?	; 编辑框
    
    
    		.const
    szClassName	db	'RemoteClass',0
    szCaptionMain	db	'窗体模板',0		; 窗体名
    szStatic	db	'static',0
    szButton	db	'Button',0
    szEdit		db	'Edit',0
    szLabel1	db	'标签',0
    szButton1	db	'确定',0
    szButton2	db	'取消',0
    szText1		db	'编辑框',0
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    		.code
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    ; 窗口过程
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    _ProcWinMain	proc	uses ebx edi esi,hWnd,uMsg,wParam,lParam
    		local	i
    		
    		mov	eax,uMsg
    		.if	eax ==	WM_CLOSE
    			invoke	DestroyWindow,hWnd
    			invoke	PostQuitMessage,NULL
    ;********************************************************************
    		.elseif	eax == WM_TIMER
    			mov	eax,wParam
    			.if	eax == ID_TIMER1
    				;****************************************************
    				; 输入你的代码
    				;****************************************************
    				
    
    				
    			.endif
    		.elseif	eax ==	WM_COMMAND
    			mov	eax,wParam
    			.if	eax == IDC_OK
    				;****************************************************
    				; 输入你的代码
    				;****************************************************
    			.elseif	eax == IDC_CANCEL
    				;****************************************************
    				; 输入你的代码
    				;****************************************************
    			
    			.endif
    			
    		.else
    			invoke	DefWindowProc,hWnd,uMsg,wParam,lParam
    			ret
    		.endif
    ;********************************************************************
    		xor	eax,eax
    		ret
    
    _ProcWinMain	endp
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    _WinMain	proc	uses ebx esi edi _lParam
    		local	@stWndClass:WNDCLASSEX
    		local	@stMsg:MSG
    
    		invoke	RtlZeroMemory,addr @stWndClass,sizeof @stWndClass
    ;********************************************************************
    ; 注册窗口类
    ;********************************************************************
    		invoke	LoadCursor,0,IDC_ARROW
    		mov	@stWndClass.hCursor,eax
    		push	hInstance
    		pop	@stWndClass.hInstance
    		mov	@stWndClass.cbSize,sizeof WNDCLASSEX
    		mov	@stWndClass.style,CS_HREDRAW or CS_VREDRAW
    		mov	@stWndClass.lpfnWndProc,offset _ProcWinMain
    		mov	@stWndClass.hbrBackground,COLOR_WINDOW + 1
    		mov	@stWndClass.lpszClassName,offset szClassName
    		invoke	RegisterClassEx,addr @stWndClass
    ;********************************************************************
    ; 建立并显示窗口
    ;********************************************************************
    		invoke	CreateWindowEx,WS_EX_CLIENTEDGE,offset szClassName,offset szCaptionMain,\
    			WS_OVERLAPPEDWINDOW,\
    			50, 50,350, 250,\
    			NULL,NULL,hInstance,NULL
    		mov	hWinMain,eax
    		;****************************************************
    		;	创建标签
    		;****************************************************
    		invoke	CreateWindowEx,NULL,offset szStatic,offset szLabel1,\		;标签
    			SS_CENTERIMAGE or SS_CENTER or WS_CHILD or WS_VISIBLE,\
    			20,20,100,30,\
    			hWinMain,1,hInstance,NULL
    		
    		
    		;****************************************************
    		;	创建按钮
    		;****************************************************
    		invoke	CreateWindowEx,NULL,offset szButton,offset szButton1,\		;确定
    			WS_CHILD or WS_VISIBLE,\					
    			20,120,100,30,\
    			hWinMain,2,hInstance,NULL
    		invoke	GetDlgCtrlID,eax
    		mov	IDC_OK,eax
    
    		invoke	CreateWindowEx,NULL,offset szButton,offset szButton1,\		;取消
    			WS_CHILD or WS_VISIBLE,\					
    			160,120,100,30,\
    			hWinMain,3,hInstance,NULL
    		invoke	GetDlgCtrlID,eax
    		mov	IDC_CANCEL,eax
    		;****************************************************
    		;	创建编辑框
    		;****************************************************
    		invoke	CreateWindowEx,NULL,offset szEdit,offset szText1,\		;编辑框
    			ES_LEFT or WS_BORDER or WS_TABSTOP or WS_CHILD or WS_VISIBLE,\
    			160,20,100,25,\
    			hWinMain,4,hInstance,NULL
    		invoke	GetDlgCtrlID,eax
    		mov	IDC_TEXT1,eax
    	
    
    		;****************************************************
    		;	创建时钟
    		;****************************************************	
    		invoke SetTimer,hWinMain,ID_TIMER1,250,NULL
    		;****************************************************
    		;	加载图标
    		;****************************************************
    		invoke	LoadIcon,hInstance,ICO_MAIN
    		invoke	SendMessage,hWinMain,WM_SETICON,ICON_BIG,eax
    
    		invoke	ShowWindow,hWinMain,SW_SHOWNORMAL
    		invoke	UpdateWindow,hWinMain
    ;********************************************************************
    ; 消息循环
    ;********************************************************************
    		.while	TRUE
    			invoke	GetMessage,addr @stMsg,NULL,0,0
    			.break	.if eax	== 0
    			invoke	TranslateMessage,addr @stMsg
    			invoke	DispatchMessage,addr @stMsg
    		.endw
    		ret
    
    _WinMain	endp
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    DllEntry	proc	_hInstance,_dwReason,_dwReserved
    		local	@dwThreadID
    
    		.if	_dwReason == DLL_PROCESS_ATTACH
    			push	_hInstance
    			pop	hInstance
    			invoke	CreateThread,NULL,0,offset _WinMain,NULL,\
    				NULL,addr @dwThreadID
    			invoke	CloseHandle,eax
    		.endif
    		mov	eax,TRUE
    		ret
    
    DllEntry	Endp
    ;>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    		End	DllEntry
    
    

    mfc_dll.rc

    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    #include		<resource.h>
    //>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
    #define		ICO_MAIN	0x1000
    ICO_MAIN	ICON		"Main.ico"
    

    mfc_dll.def

    EXPORTS
    

    Makefile 见上文

    效果
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44018458/article/details/108935929
今日推荐