C#调用(C++)DLL导出类的求助

hpinger 2009-04-14 07:16:16
现,用C++编写一个DLL,程序由以下几部分组成:
1)DLLBASE.h//该文件定义接口虚函数
#ifndef DLLBASE_H
#define DLLBASE_H
//////////////////////////////////////////////////////////////////////////
#ifndef DLLBASE_API
#define DLLBASE_API __declspec(dllimport)
#endif
//////////////////////////////////////////////////////////////////////////
class DLLBASE_API DllBase
{
public:
virtual int GetNum()=0;
virtual void SetNum(int)=0;
};
//////////////////////////////////////////////////////////////////////////
extern "C" DLLBASE_API //外部接口导入
DllBase *CreateObject(int type);//接口函数,将在DllDemo.cpp中产生一个具体的实现
//////////////////////////////////////////////////////////////////////////
#endif //DLLBASE_H

2)DLLDEMO.H //DLL类、函数的定义
#ifndef DLLDEMO_H
#define DLLDEMO_H
#include <stdio.h>
//////////////////////////////////////////////////////////////////////////
#include "DllBase.h"
//创建基于DllBase的类DllDemo
class DllDemo : public DllBase{
private:
int m_nNum;
public:
DllDemo();
DllDemo(int);
~DllDemo();
void CreateObject(int type);//自有函数
//DllBase虚函数的具体实现/////////////////////////////////////////
int GetNum(); //virtual int GetNum()=0;
void SetNum(int); //virtual void SetNum(int)=0;
//////////////////////////////////////////////////////////////////////////
};
//////////////////////////////////////////////////////////////////////////
#endif /*DLLDEMO_H*/

3)DllDemo.cpp //类的实现
#define DLLDEMO_API __declspec(dllexport)
//////////////////////////////////////////////////////////////////////////
//DllDemo类的实现
#include "DllDemo.h"
////函数的定义
DllDemo::DllDemo()
:m_nNum(0)
{
printf("In Default Constructor of DllBase in DLL1. m_nNum = %d\n", m_nNum);
}

DllDemo::~DllDemo()
{
printf("In Destructor of DllBase in DLL1. m_nNum = %d\n", m_nNum);
}

DllDemo::DllDemo(int n)
{
m_nNum = n;
printf("In Second Constructor of DllBase in DLL1. m_nNum = %d\n", m_nNum);
}

int DllDemo::GetNum()
{
printf("In GetNum of DllBase in DLL1. m_nNum = %d\n", m_nNum);
return m_nNum;
}

void DllDemo::SetNum(int n)
{
m_nNum = n;
printf("In SetNum of DllBase in DLL1. m_nNum = %d\n", m_nNum);
}

void DllDemo::CreateObject(int type)
{
if (type==1)
{
printf("Create An Object!\n");
}
else
printf("No Object Created!\n");
}

//////////////////////////////////////////////////////////////////////////
//
DLLDEMO_API //导出Dll
DllBase* CreateObject(int type) //接口函数的实现
{
DllDemo *demo = new DllDemo; //产生类的一个具体实现,即导出类
demo->CreateObject(type); //调用自有函数
return demo; //返回指针变量,即导出类的入口地址
}

4)Dll1.cpp //DLL入口函数
// Dll1.cpp : Defines the entry point for the DLL application.
#include <windows.h>
#include <stdio.h>
//////////////////////////////////////////////////////////////////////////
#include "DllDemo.h"
//////////////////////////////////////////////////////////////////////////
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{

switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
printf("Loading DLL1\n");
break;
case DLL_PROCESS_DETACH:
printf("Unloading DLL1\n");
break;
}


return TRUE;
}

调用该程序生成的DLL,一般做法是使用三个文件:DllBase.h、Dll1.dll、Dll1.lib
实现方法:
#include "DllBase.h"
#pragma comment(lib,Dll1.lib")
DllBase *test = CreateObject(1);
test->SetNum(100);
--------------------------------------------------------------------------------
现在的问题是,C#如何调用这个DLL?
...全文
364 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
roamer2005 2012-06-29
  • 打赏
  • 举报
回复
怎么解决的?教程在哪里?郁闷死了。能发个教程吗roamer2000@163.com
gaofeng0546 2009-11-10
  • 打赏
  • 举报
回复
小弟现在正遇到这个问题,不置可否赐教,能否把解决方案机代码发至邮箱 gaofeng0546@qq.com
不胜感激!!
蓝海D鱼 2009-04-20
  • 打赏
  • 举报
回复
qiadi

hpinger 2009-04-20
  • 打赏
  • 举报
回复
没那么容易
flyingsky00 2009-04-18
  • 打赏
  • 举报
回复
用 DLLImport 很方便的
hpinger 2009-04-17
  • 打赏
  • 举报
回复
经过大量搜索,问题已得到解决。过几天我写个教程
hpinger 2009-04-15
  • 打赏
  • 举报
回复
请高手点拨以下
hpinger 2009-04-15
  • 打赏
  • 举报
回复
能否具体赐教?
mb_1985 2009-04-14
  • 打赏
  • 举报
回复
用反射啊,DLL中的元数据都可以调出来的

111,126

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧