【200分】非托管c++中调用托管C#DLL,请大家帮忙

zyrr159487 2011-09-16 12:33:21
背景:
目前有个项目,对方用C#编写DLL
这边用C++去链接并调用它其中的函数。

进展:
链接成功,以下是使用的方法:
项目属性中打开CLR开关
在头文件中加入
#using <mscorlib.dll>
#using "动态库名称"
using namespace 命名空间;
//在需要使用的函数前加入
#pragma managed


遇到的问题:
① 声明为实体无法在全局中声明,出现提示,这一点我认为是因为我这个类继承了两个基类有关系
错误 2 error C3076: you cannot embed an instance of a reference type, 'XXXDLL::XX', in a native type
如果声明为指针实体
错误 2 error C3265: cannot declare a managed 'p3' in an unmanaged 'CXXXX'
② 调用函数时参数无法通过编译
我所调用的DLL的函数有两个参数,一个是GraphicPath,一个是INT
对方在VB中给的示范是这样的
Dim Path As New System.Drawing.Drawing2D.GraphicsPath()
CLASS.FUNCTION(Path, 3)
我是这样写的
GraphicsPath graphicPath;
CLASS.FUNCTION(&graphicPath, 3);
出现错误提示如下
错误 2 error C2664: 'CLSS::FUNCTION': cannot convert parameter 2 from 'int' to 'int'
错误 1 error C3624: 'System::Drawing::Drawing2D::GraphicsPath': use of this type requires a reference to assembly 'System.Drawing'

感谢回复,请指教
...全文
417 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
graphicgod 2011-09-21
  • 打赏
  • 举报
回复
菜鸟一个 帮 顶
zyrr159487 2011-09-21
  • 打赏
  • 举报
回复
还是不知道为什么不能为全局对象,其他没问题了。
结帖
W1nds 2011-09-17
  • 打赏
  • 举报
回复
帮顶了
zyrr159487 2011-09-16
  • 打赏
  • 举报
回复
感谢10楼的帮忙
zyrr159487 2011-09-16
  • 打赏
  • 举报
回复
To 10 楼 akirya 的回复:]
感谢帮忙,那无法将实体声明为全局是否因为多重继承的缘故呢
zyrr159487 2011-09-16
  • 打赏
  • 举报
回复
To 10 楼 akirya 的回复:]

果然是这个原因
  • 打赏
  • 举报
回复
你传递的也必须是托管的对象啊
GraphicsPath ^ p2 = gcnew GraphicsPath ();
CLASS.FUNCTION( p2 , 3 );
zyrr159487 2011-09-16
  • 打赏
  • 举报
回复
To 7 楼 akirya 的回复:]
感谢回复,调用是没问题了,可以通过调用对方库,但是如果要传递参数的话则不行。

调用函数时参数无法通过编译
所调用的DLL的函数有两个参数,一个是GraphicPath,一个是INT
对方在VB中给的示范是这样的
Dim Path As New System.Drawing.Drawing2D.GraphicsPath()
CLASS.FUNCTION(Path, 3)
我是这样写的
GraphicsPath graphicPath;
CLASS.FUNCTION(&graphicPath, 3);
出现错误提示如下
错误 2 error C2664: 'CLSS::FUNCTION': cannot convert parameter 2 from 'int' to 'int'
错误 1 error C3624: 'System::Drawing::Drawing2D::GraphicsPath': use of this type requires a reference to assembly 'System.Drawing'

zyrr159487 2011-09-16
  • 打赏
  • 举报
回复
to 6 楼 yjgx007 的回复:]
感谢分享,很好的文章,待我仔细看看
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 zyrr159487 的回复:]

to 4 楼 akirya 的回复:]
请详细的说说吧
[/Quote]
建立C#的库工程
代码如下
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
public class Class1
{
public void func()
{
Console.WriteLine( "Hello" );
}
}
}

新建个C++控制台程序
菜单 项目 ->引用 ->添加新引用 -> 浏览->选择C#的dll

C++ 代码

using namespace ClassLibrary1;

int _tmain(int argc, _TCHAR* argv[])
{

Class1^ p = gcnew Class1();
p->func();

return 0;
}

yjgx007 2011-09-16
  • 打赏
  • 举报
回复
http://www.codeproject.com/KB/cs/unmanagedtomanaged.aspx
zyrr159487 2011-09-16
  • 打赏
  • 举报
回复
to 4 楼 akirya 的回复:]
请详细的说说吧
  • 打赏
  • 举报
回复
既然打开clr了
那就用托管C++调用C#的dll。
cocoabird 2011-09-16
  • 打赏
  • 举报
回复
帮顶下哈
宇影周邵 2011-09-16
  • 打赏
  • 举报
回复
不太懂。。也许可以看看:http://topic.csdn.net/u/20090106/22/08A41600-E418-42E8-939B-B409F0605D9F.html
zyrr159487 2011-09-16
  • 打赏
  • 举报
回复
一下子就到底了,居然是零回复

15,471

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 进程/线程/DLL
社区管理员
  • 进程/线程/DLL社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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