3,881
社区成员
发帖
与我相关
我的任务
分享class __declspec(dllexport) CDll {
public:
CDll(void){};
virtual void fun(){
cout<<"this is from dll\n"<<endl;
};
};
extern "C" __declspec(dllexport) CDll* fnDll(void)
{
return new CDll();
}
只要找到fnDll的地址,就可以获得类的对象指针
#ifndef TEST_DLL_H
#define TEST_DLL_H
#ifdef TEST_API
#else
#define TEST_API __declspec(dllimport)
#endif
class TEST_API Delegate
{
public:
virtual void Print()const = 0;
virtual ~Delegate()
{}
};
class TEST_API PrintAdd : public Delegate
{
public:
PrintAdd() : result(0)
{
}
virtual void Print()const;
void Add(int x, int y);
private:
int result;
};
#endif
//test_dll.cpp
#include <iostream>
#define TEST_API __declspec(dllexport)
#include "test_dll.h"
using namespace std;
PrintAdd* PrintAdd::CreateObj()
{
return new PrintAdd;
}
void PrintAdd::Print() const
{
cout<<result<<endl;
}
void PrintAdd::Add( int x, int y )
{
result = x + y;
}
调用方
#include "test_dll.h"
#pragma comment(lib, "test_dll.lib")
int _tmain(int argc, _TCHAR* argv[])
{
PrintAdd p;
p.Add(3, 5);
p.Print();
system("pause");
return 0;
}