15,471
社区成员
发帖
与我相关
我的任务
分享
[code=c]
void CTestDllDlg::OnBnClickedButton1()
{
// TODO: 在此添加控件通知处理程序代码
typedef CExportTest* (*lpCall)(CExportTest*);
typedef int (*fCall)(void);
//创建dll句柄
HINSTANCE hDll;
//装载dll
CExportTest* pInput = new CExportTest();
pInput->m_i = 2;
hDll = LoadLibrary(_T("DllInstenceMfc.dll"));
if(NULL==hDll)
{
AfxMessageBox(_T("DLL加载失败!"));
}
//检索指定DLL中的输出库函数地址
lpCall pFunc = NULL;
fCall pFI = NULL;
pFunc =(lpCall)GetProcAddress(hDll,(LPCSTR)"ExportClass");
pFI = (fCall)GetProcAddress(hDll,(LPCSTR)"TestFunc");
if(NULL==pFunc)
{
AfxMessageBox(_T("DLL内部函数调用失败!"));
}
if(NULL==pFI)
{
AfxMessageBox(_T("DLL内部函数TestFunc调用失败!"));
}
//创建DLL对话框
CExportTest* pDll = pFunc(pInput);
if(pDll != NULL)
{
int i = pDll->ExportInt();
CString str;
str.Format(_T("%d"),i);
AfxMessageBox(str);
}
if(pFI != NULL)
{
int x = pFI();
CString str;
str.Format(_T("%d"),x);
AfxMessageBox(str);
}
FreeLibrary(hDll);
delete pInput;
}
下面代码不使用编译不过啊
#pragma once
class CExportTest
{
public:
CExportTest(void);
virtual ~CExportTest(void);
public:
virtual int ExportInt();
//virtual int ExportStr(int x) = 0;
public:
int m_i;
};
#include "StdAfx.h"
#include "ExportTest.h"
CExportTest::CExportTest(void)
{
m_i = 1;
}
CExportTest::~CExportTest(void)
{
}
int CExportTest::ExportInt(void)
{
return m_i;
}