访问该类的代码:
CILoveYou ily;
ily.SetValue(900);
char s[100];
wsprintf(s,"调用了类中的成员哦,值是:%d",ily.GetValue());
ShowMessage(this->GetSafeHwnd(),s);
MFC 规则DLL
1、 函数
a) DLL
此类DLL有一个继承了CwinApp的类,但是函数可以不放在该类中。
extern "C" __declspec(dllexport) BOOL isOdd(int num)


{
AFX_MANAGE_STATE(AfxGetStaticModuleState());//此句一定要
if(num % 2 == 0)
return true;
else
return false;
}
b) 应用程序
void CTestdll2Dlg::OnButton5()


{
// TODO: Add your control notification handler code here
typedef BOOL ISODD(int);
ISODD *isOdd;
HINSTANCE hm;
if(hm = ::LoadLibrary("mfcdll4.dll"))

{
isOdd = (ISODD *)::GetProcAddress(hm,"isOdd");
if(isOdd)

{
if(isOdd(9))
MessageBox("是偶数");
else
MessageBox("不是偶数");
}
else

{
MessageBox("有问题");
}
::FreeLibrary(hm);
}
else

{
MessageBox("DLL加载失败");
}
}
2、 类
a) DLL中的代码
i. Clzh类的头文件:lzh.h
class AFX_EXT_CLASS Clzh //此处一定要用AFX_EXT_CLASS


{
public:
CString GetValue();
void SetValue(CString str);
Clzh();
private:
CString str;
};
ii. Clzh类的实现文件:lzh.cpp
Clzh::Clzh()


{
}
__declspec(dllexport) void Clzh::SetValue(CString str)


{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
this->str = str;
}
__declspec(dllexport) CString Clzh::GetValue()


{
AFX_MANAGE_STATE(AfxGetStaticModuleState());
return str;
}
b) 应用程序
在StdAfx.h中头文件中加入:class __declspec(dllimport) Clzh;
在要访问该类的地方加入头文件:#include "lzh.h"
程序如下:
void CTestdll2Dlg::OnButton7()


{
// TODO: Add your control notification handler code here
Clzh lzh;
lzh.SetValue("abc");
MessageBox(lzh.GetValue());
}