学习笔记:第19课 动态联接库

本文详细介绍了Windows系统中DLL的工作原理及使用方法,包括DLL的基本概念、如何通过不同方式加载DLL,以及如何在程序中调用DLL中的函数。此外,还提供了具体的代码示例来展示DLL的加载过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  Windows API中的所有函数都包含在DLL中。其中有3个最重要的DLL,Kernel32.dll,它包含用于管理内存、进程和线程的各个函数;User32.dll,它包含用于执行用户界面任务(如窗口的创建和消息的传送)的各个函数;GDI32.dll,它包含用于画图和显示文本的各个函数。

静态库:函数和数据被编译进一个二进制文件(通常扩展名为.LIB)。在使用静态库的情况下,在编译链接可执行文件时,链接器从库中复制这些函数和数据并把它们和应用程序的其它模块组合起来创建最终的可执行文件(.EXE文件)。
动态库:在使用动态库的时候,往往提供两个文件:一个引入库和一个DLL。引入库包含被DLL导出的函数和变量的符号名,DLL包含实际的函数和数据。在编译链接可执行文件时,只需要链接引入库,DLL中的函数代码和数据并不复制到可执行文件中,在运行的时候,再去加载DLL,访问DLL中导出的函数。
动态链接库加载的两种方式
 隐式链接
显示加载
1。如果要查找*dll中包含信息,可在命令行下进入Debug所在目录,输入以下命令
dumpbin -exports dll.dll  
有些时候由于某种安装原因,dumpbin被认为是无效命令,接下来在
C:/Program Files/Microsoft Visual Studio/VC98/Bin/下找到VCVARS32.bat并在命令行运行,之后就能执行dumpbin命令了。
2。如果我们在动态链接库中使用标准调用约定_stdcall,而在可执行程序中使用动态加载DLL,会发生名字重编,如果知道DLL中函数的序号,这时可以使用宏MAKEINTRESOURCE把序号转变成名字,如:
ADDPROC Add=(ADDPROC)GetProcAddress(hInst,MAKEINTRESOURCE(1));
3。

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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

进击的横打

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值