您的位置:首页 > 其它

从动态链接库中导出类

2010-11-25 17:53 176 查看
贴代码。

dll的代码

class.h文件

class CMath
{
public:
int _declspec(dllexport) GetMaxConnDiv(int a,int b);
int _declspec(dllexport) GetMinConnDiv(int a,int b);
};

class.def文件

LIBRARY class

EXPORTS
GetMaxConnDiv
GetMinConnDiv

class.cpp文件

#include"class.h"
int CMath::GetMaxConnDiv(int a , int b)
{
int x=1;
int ires;
if(a<b)
return 0;
if(b==0)
return 0;
while(x!=0)
{
x=a%b;
a=b;
ires=b;
b=x;
}
return ires;
}
int CMath::GetMinConnDiv(int a, int b)
{
int x=1;
int ires;
int m,n;
m=a;
n=b;
if(a<b)
return 0;
if(a==0)
return 0;
if(b==0)
return 0;
while(x!=0)
{
x=a%b;
a=b;
ires=b;
b=x;
}
ires=(m*n)/ires;
return ires;
}

测试程序文件

void CTextdllDlg::OnButton1()
{
// TODO: Add your control notification handler code here
CString str1,str2;
m_edit3.GetWindowText(str1);
m_edit4.GetWindowText(str2);
int m,n;
m=atoi(str1);
n=atoi(str2);
int re;
CString result;
typedef int (WINAPI * MyFunc)(int ,int );//必需加上WINAPI,不然会出错
HMODULE hModule=::LoadLibrary("class.dll");
MyFunc Getval=(MyFunc)GetProcAddress(hModule,"GetMaxConnDiv");
re=Getval(m,n);
result.Format("%d",re);
m_edit1.SetWindowText(result);
FreeLibrary(hModule);
}

void CTextdllDlg::OnButton2()
{
// TODO: Add your control notification handler code here
CString str1,str2;
m_edit3.GetWindowText(str1);
m_edit4.GetWindowText(str2);
int m,n;
m=atoi(str1);
n=atoi(str2);
int re;
typedef int (WINAPI * MyFunc) (int ,int );
HMODULE hModule=::LoadLibrary("class.dll");
MyFunc Getval=(MyFunc)GetProcAddress(hModule,"GetMinConnDiv");
re=Getval(m,n);
CString result;
result.Format("%d",re);
m_edit2.SetWindowText(result);
FreeLibrary(hModule);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: