Windows API中的所有函数都包含在DLL中。其中有3个最重要的DLL,Kernel32.dll,它包含用于管理内存、进程和线程的各个函数;User32.dll,它包含用于执行用户界面任务(如窗口的创建和消息的传送)的各个函数;GDI32.dll,它包含用于画图和显示文本的各个函数。
dumpbin -exports dll.dll
有些时候由于某种安装原因,dumpbin被认为是无效命令,接下来在
C:/Program Files/Microsoft Visual Studio/VC98/Bin/下找到VCVARS32.bat并在命令行运行,之后就能执行dumpbin命令了。
ADDPROC Add=(ADDPROC)GetProcAddress(hInst,MAKEINTRESOURCE(1));
DllMain
The DllMain function is an optional entry point into a dynamic-link library (DLL). If the function is used, it is called by the system when processes and threads are initialized and terminated, or upon calls to the LoadLibrary and FreeLibrary functions.
DllMain is a placeholder for the library-defined function name. You must specify the actual name you use when you build your DLL. For more information, see the documentation included with your development tools.
BOOL WINAPI DllMain(
HINSTANCE hinstDLL,
DWORD fdwReason,
LPVOID lpvReserved
);
当我们的动态链接库不再使用时可以调用FreeLibrary使动态链接库使用计数减1,当使用计数为零时,系统会收回模块资源
FreeLibrary
The FreeLibrary function decrements the reference count of the loaded dynamic-link library (DLL). When the reference count reaches zero, the module is unmapped from the address space of the calling process and the handle is no longer valid.
BOOL FreeLibrary(
HMODULE hModule
)
#ifdef DLL1_API
#else
#define DLL1_API extern "C" _declspec(dllimport)
#endif
DLL1_API int _stdcall add(int a,int b);
DLL1_API int _stdcall subtract(int a,int b);
//class /*DLL1_API*/ Point
/**//*{
public:
DLL1_API void output(int x,int y);
void test();
};*/
#define DLL1_API extern "C" _declspec(dllexport)
#include "Dll1.h"
#include <Windows.h>
#include <stdio.h>
int _stdcall add(int a,int b)
...{
return a+b;
}
int _stdcall subtract(int a,int b)
...{
return a-b;
}

/**//*void Point::output(int x,int y)
{
HWND hwnd=GetForegroundWindow();
HDC hdc=GetDC(hwnd);
char buf[20];
memset(buf,0,20);
sprintf(buf,"x=%d,y=%d",x,y);
TextOut(hdc,0,0,buf,strlen(buf));
ReleaseDC(hwnd,hdc);
}
void Point::test()
{
}*/DLL2
LIBRARY Dll2
EXPORTS
add
subtract
int _stdcall add(int a,int b)
...{
return a+b;
}
int _stdcall subtract(int a,int b)
...{
return a-b;
}
TEST
void CDllTestDlg::OnPaint() 
...{
if (IsIconic())
...{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
...{
CDialog::OnPaint();
}
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CDllTestDlg::OnQueryDragIcon()
...{
return (HCURSOR) m_hIcon;
}
//extern int add(int a,int b);
//extern int subtract(int a,int b);
//_declspec(dllimport) int add(int a,int b);
//_declspec(dllimport) int subtract(int a,int b);
void CDllTestDlg::OnBtnAdd() 
...{
// TODO: Add your control notification handler code here
/**//* CString str;
str.Format("5+3=%d",add(5,3));
MessageBox(str);*/
HINSTANCE hInst;
hInst=LoadLibrary("Dll3.dll");
typedef int (/**//*_stdcall*/ *ADDPROC)(int a,int b);
//ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"?add@@YAHHH@Z");
ADDPROC Add=(ADDPROC)GetProcAddress(hInst,MAKEINTRESOURCE(1));
if(!Add)
...{
MessageBox("获取函数地址失败!");
return;
}
CString str;
str.Format("5+3=%d",Add(5,3));
MessageBox(str);
FreeLibrary(hInst);
}
void CDllTestDlg::OnBtnSubtract() 
...{
// TODO: Add your control notification handler code here
/**//* CString str;
str.Format("5-3=%d",subtract(5,3));
MessageBox(str);*/
}
void CDllTestDlg::OnBtnOutput() 
...{
// TODO: Add your control notification handler code here
/**//* Point pt;
pt.output(5,3);*/
_declspec(dllexport) int add(int a,int b)
...{
return a+b;
}
本文详细介绍了Windows系统中DLL的工作原理及使用方法,包括DLL的基本概念、如何通过不同方式加载DLL,以及如何在程序中调用DLL中的函数。此外,还提供了具体的代码示例来展示DLL的加载过程。
11万+

被折叠的 条评论
为什么被折叠?



