1. 程式人生 > >如何在OD載入程式遇到入口點之前執行程式碼之二:靜態裝載DLL

如何在OD載入程式遇到入口點之前執行程式碼之二:靜態裝載DLL

編譯器:VS2010

需要的知識:DLL的編寫和使用

1.dllmain.cpp

// dllmain.cpp : Defines the entry point for the DLL application.
#include "stdafx.h"

BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
					 )
{
	switch (ul_reason_for_call)
	{
	case DLL_PROCESS_ATTACH:
		MessageBoxA(NULL,"DLL_PROCESS_ATTACH","TEST",MB_OK);
		break;
	case DLL_THREAD_ATTACH:
	case DLL_THREAD_DETACH:
	case DLL_PROCESS_DETACH:
		MessageBoxA(NULL,"DLL_PROCESS_DETACH","TEST",MB_OK);
		break;
	}
	return TRUE;
}

DLL_PROCESS_ATTACH:表示該DLL第一次裝載進程序空間中執行的程式碼

DLL_PROCESS_DETACH:表示該DLL脫離程序空間時候執行的程式碼

MessageBoxA函式在windows.h標頭檔案中

2.dllTesxt.cpp

#include "stdafx.h"
#include "dllTest.h"
#include <iostream>
using namespace std;
void Show(){
	MessageBoxA(0,"DIAT","",MB_OK);
}
3.dllTest.h
#ifndef TestDll_H_
#define TestDll_H_
#ifndef MYLIBDLL
#define MYLIBDLL extern "C" _declspec(dllexport) 
#endif
MYLIBDLL void Show();
#endif
4.dllTest.def
LIBRARY "dllTest"
EXPORTS
Show @1

************************************************************************************

編譯連結生成兩個檔案dllTest.dll和dllTest.lib

建另外一個工程

1.dll.c

#include<stdio.h>
#include"dll.h"
#include<windows.h>
int main(){
	MessageBoxA(0,"1","1",0);
	printf("1");
	Show();
	printf("2");
	MessageBoxA(0,"2","2",0);
}

2.dll.h
#pragma comment(lib, "dllTest.lib")
#ifndef TestDll_H_
#define TestDll_H_

#define MYLIBDLL 
#ifdef __cplusplus
   extern "C"
#endif
__declspec(dllimport) 
MYLIBDLL void Show();
#endif
3.編譯連結生成*.exe檔案,用OD載入

參考資料:

1.《windows核心程式設計》

2.《Windows PE權威指南》