开发步骤如下:
<1> : 利用VC6.0开发出一个范例:
extern "C" _declspec(dllexport) int __stdcall substract(int a,int b){ return a-b;}extern "C" _declspec(dllexport) int __stdcall add(int a,int b){ return a+b;}
并且配置def文档:
LIBRARY dlo.dllEXPORTSadd @1substract @2
编译得到dll文件,同时还要注意个lib文件.
<2> : 在MFC中调用:
1:新建一个MFC的dialog工程,并且在工程设置的link的Object/library modules 中将上面生成的lib文件添加进去!
2:添加俩个个按钮;
3:按钮点击事件程序如下写:
extern int add(int a,int b);//引入外部函数的声明void CDlltextDlg::OnAdd() { // TODO: Add your control notification handler code here CString str; str.Format("5+3=%d",add(5,3)); MessageBox(str);}extern int substract(int a,int b);void CDlltextDlg::OnAbstracte() { // TODO: Add your control notification handler code here CString str; str.Format("5-3=%d",substract(5,3)); MessageBox(str);}
4:点击按钮进行测试,既可以得到结果!
<3> :在C++应用程序中调用:
1:新建一个C++项目,并且在工程设置的link的Object/library modules中添加lib文件,与其他的lib文件用单空格隔开!
2 :写一段程序测试:
#include<iostream>
using namespace std;
extern int add(int a,int b);
void main(){ int a,b; a=4; b=6; cout<<add(a,b)<<endl;
}
编译运行上面的程序即可得到结果!
<5> :动态导入dll到程序中:
1:MFC程序中:
//extern int add(int a,int b);void CDlltextDlg::OnAdd() { // TODO: Add your control notification handler code here
HINSTANCE hInst; hInst=LoadLibrary("dlo.dll"); typedef int (_stdcall *ADDPROC)(int a,int b); ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"add"); if(!Add){ MessageBox("load library fail !"); return; }
CString str; str.Format("5+3=%d",Add(5,3)); MessageBox(str);}//extern int substract(int a,int b);void CDlltextDlg::OnAbstracte() { // TODO: Add your control notification handler code here HINSTANCE hInst; hInst=LoadLibrary("dlo.dll"); typedef int (_stdcall *ADDPROC)(int a,int b); ADDPROC Substract=(ADDPROC)GetProcAddress(hInst,"substract"); if(!Substract){ MessageBox("load library fail !"); return; }
CString str; str.Format("5-3=%d",Substract(5,3)); MessageBox(str);}
<2> : C++应用程序动态加载动态链接库:
#include<iostream>#include<windows.h>using namespace std;
//extern int add(int a,int b);
void main(){ int a,b;
HINSTANCE hInst; hInst=LoadLibrary("dlo.dll"); typedef int (_stdcall *ADDPROC)(int a,int b); ADDPROC Add=(ADDPROC)GetProcAddress(hInst,"add"); if(!Add){ return; }
a=4; b=6; cout<<Add(a,b)<<endl;}
<3> : VB语言调用动态链接库:
1:将上面开发的dll文件放到系统system32文件夹下;
2 : 下面以调用add函数为例 :
函数声明 :
Public Declare Function add Lib "dlo.dll" Alias "#1" (ByVal num1 As Integer, ByVal num2 As Integer) As Integer
在VC++中形参变量的数据类型为int,在vb中可能不是integer,也可能为long型(没有得到证实,因为我仔细看了)
3 : 新建一个VB工程,在form中添加一个按钮,进入code编辑状态,将上面的函数声明写入;
4 : 编写按钮点击事件:
Private Sub Command1_Click() MsgBox "" & add(6, 4)End Sub
5:运行程序,既可以得到预定的结果.
