今天打算研究下白利用的时候出各种问题,复习下dll加载。
1.普通dll文件
int add(int a,int b)
{
return a+b;
}
查看输出表为空
2._declspec申明
_declspec(dllexport) int add(int a,int b)
{
return a+b;
}
导出函数名为?add@@YAHHH@Z,产生一个lib文件,需要在link下添加此lib才能extern到函数,或者在调用程序中申明 _declspec(dllimport) int add(int a,int b)
3.添加DEF文件
LIBRARY 3
EXPORTS
add
没发生名字改编
4.loader
#include "windows.h"
#include "iostream.h"
void main()
{
HINSTANCE h;
h=LoadLibrary("3.dll");
typedef int (*func_add)(int a,int b);
func_add add=(func_add)GetProcAddress(h,"add");
cout<<add(3,5)<<endl;
}
成功加载
5.动态加载message
loader
#include "windows.h"
#include "iostream.h"
void main()
{
HINSTANCE h;
h=LoadLibrary("3.dll");
typedef void (*func_sw)(HWND);
func_sw sw=(func_sw)GetProcAddress(h,"ShowMessage");
sw(FindWindow(NULL,"loader"));
}
dll
#include "windows.h"
int add(int a,int b)
{
return a+b;
}
void ShowMessage(HWND h1)
{
MessageBox(h1,"1","1",NULL);
}
4.dllmain中直接调用MessageBox
dll
#include "windows.h"
#include "stdio.h"
void ShowMessage();
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
MessageBox(NULL,"1","1",MB_OK);
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
void ShowMessage()
{
MessageBox(NULL,"1","1",MB_OK);
}
成功。。。。。
5.dll中定义函数内部调用MessageBox,dllmain中调用该定义函数
dll
#include "windows.h"
#include "stdio.h"
void ShowMessage();
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved)
{
switch (fdwReason)
{
case DLL_PROCESS_ATTACH:
ShowMessage();
break;
case DLL_PROCESS_DETACH:
break;
}
return TRUE;
}
void ShowMessage()
{
MessageBox(NULL,"1","1",MB_OK);
}
loader
#include "windows.h"
#include "iostream.h"
void main()
{
HINSTANCE h;
//LoadLibrary("USER32.dll");
h=LoadLibrary("3.dll");
system("sleep");
}
成功显示