一、用AppWizard生成了一个dll,(是MFC Appwizard(dll)),命名为MyDll 1、在MyDll.cpp中添加函数://系统是把他作为全局的函数好像 void DllTest(void) { AfxMessageBox("this is a dll function"); } 2、在MyDll.def中添加 DllTest @1二、建立基于对话框的一个应用程序UseDll,添加一个按钮, 1、在UserDllDlg.cpp中定义变量如下: HINSTANCE dll_handle=NULL; typedef void (*DLLTEST)(void); DLLTEST DllTest;//这两个变量是作为全局变量定义在类的外部 2、在OnButton()中添加代码:想显示链接dll并调用DllTest()函数,添加的代码如下: Dll_handle=LoadLibrary("...\...\...\Mydll.dll");//dll文件的路径 if(Dll_handle==NULL) { AfxMessageBox("dll has not be loaded !"); return; } DllTest=(DLLTEST)GetProcAddress(Dll_handle,"DllTest"); if(DllTest==NULL) { AfxMessageBox("dll function has not be loaded !"); return; } AfxMessageBox("begin to use function"); DllTest(); AfxMessageBox("end of use function"); FreeLibrary(Dll_handle);
if a dll is all you have, you have to use LoadLibrary and GetProcAddress, consider to write a wrapper class, seeUsing Run-Time Dynamic Linking http://msdn.microsoft.com/library/en-us/dllproc/base/using_run_time_dynamic_linking.asp?frame=true
1、在MyDll.cpp中添加函数://系统是把他作为全局的函数好像
void DllTest(void)
{
AfxMessageBox("this is a dll function");
} 2、在MyDll.def中添加
DllTest @1二、建立基于对话框的一个应用程序UseDll,添加一个按钮, 1、在UserDllDlg.cpp中定义变量如下: HINSTANCE dll_handle=NULL;
typedef void (*DLLTEST)(void);
DLLTEST DllTest;//这两个变量是作为全局变量定义在类的外部 2、在OnButton()中添加代码:想显示链接dll并调用DllTest()函数,添加的代码如下: Dll_handle=LoadLibrary("...\...\...\Mydll.dll");//dll文件的路径
if(Dll_handle==NULL)
{
AfxMessageBox("dll has not be loaded !");
return;
}
DllTest=(DLLTEST)GetProcAddress(Dll_handle,"DllTest");
if(DllTest==NULL)
{
AfxMessageBox("dll function has not be loaded !");
return;
}
AfxMessageBox("begin to use function");
DllTest();
AfxMessageBox("end of use function");
FreeLibrary(Dll_handle);
#include "MyDll.h" 然后直接使用里面的函数。
http://msdn.microsoft.com/library/en-us/dllproc/base/using_run_time_dynamic_linking.asp?frame=true