15,466
社区成员
发帖
与我相关
我的任务
分享
//////////////////////////////
//dll.h
CButton* CreateButton( LPCTSTR lpszCaption,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID );
//////////////////////////////
//dll.cpp
CButton* CreateButton(
LPCTSTR lpszCaption,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID)
{
CButton* pButton = new CButton;
BOOL bRet = pButton->Create(lpszCaption,
dwStyle,
rect,
pParentWnd,
nID
);
if (bRet == TRUE)
{
return pButton;
}
else
return NULL;
}
/////////////////////////////////
//dll.def
; Dll.def : Declares the module parameters for the DLL.
LIBRARY "Dll"
DESCRIPTION 'Dll Windows Dynamic Link Library'
EXPORTS
; Explicit exports can go here
CreateButton
//Button的ID
#define IDC_BUTTON 1000
//导入函数
typedef CButton* (CREATE_BUTTON)(LPCTSTR lpszCaption,
DWORD dwStyle,
const RECT& rect,
CWnd* pParentWnd,
UINT nID);
//单击确定按钮,创建一个Button
void CTestDlg::OnOK()
{
// TODO: Add extra validation here
//加载库
HMODULE hMoudle = ::LoadLibrary("Dll.dll");
if (hMoudle)
{
//得到导出函数
CREATE_BUTTON *pFunc = (CREATE_BUTTON* )::GetProcAddress(hMoudle, "CreateButton");
if (pFunc != NULL)
{
RECT rc = {20, 20, 100, 100};
CButton *pButton = pFunc("New Button", BS_PUSHBUTTON, rc, this, IDC_BUTTON);
//显示Button
pButton->ShowWindow(SW_SHOW);
}
}
// CDialog::OnOK();
}