在C#项目中使用C ++ DLL [英] Using a C++ DLL in a C# Project

查看:67
本文介绍了在C#项目中使用C ++ DLL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个现有的Visual C ++项目,它构建了一个DLL(不是COM或

ATL或任何东西)。我希望在Visual Studio 2005 C#

项目中使用此DLL,而无需在C#项目中再次定义每个函数和结构(我相信使用pinvoke)。 br />

我已经做了一些阅读,似乎我所追求的是

可能。我已经将旧的C ++项目添加到我的新C#解决方案中并设置

编译器使用/ clr。然后我可以添加DLL作为对

C#项目的引用,没有任何错误。但是,我被困在那里。我如何实际调用DLL中的任何方法?
?添加使用< DLL

名称>产生错误,我不知道还有什么可以尝试。


提前致谢,


Ben

解决方案

Ben,


这个dll,它是导出函数还是类?如果它使用函数和

结构,那么你不应该在项目中添加一个引用到dll。

你必须通过P调用这些函数/调用

图层,这需要在代码中重新声明结构和

函数。


如果如果你想访问类,那么你必须在你的类周围创建一个托管的

包装器。只是设置/ clr开关不会

使您的代码可以在.NET中访问。


希望这会有所帮助。

-

- Nicholas Paldino [.NET / C#MVP]

- mv * @ spam.guard.caspershouse.com


" BDowling" < be *********** @ googlemail.comwrote in message

news:11 ******************* **@y66g2000hsf.googlegro ups.com ...


大家好,


我有一个现有的Visual C ++项目构建DLL(不是COM或

ATL或任何东西)。我希望在Visual Studio 2005 C#

项目中使用此DLL,而无需在C#项目中再次定义每个函数和结构(我相信使用pinvoke)。 br />

我已经做了一些阅读,似乎我所追求的是

可能。我已经将旧的C ++项目添加到我的新C#解决方案中并设置

编译器使用/ clr。然后我可以添加DLL作为对

C#项目的引用,没有任何错误。但是,我被困在那里。我如何实际调用DLL中的任何方法?
?添加使用< DLL

名称>产生错误,我不知道还有什么可以尝试。


提前致谢,


Ben



" BDowling" < be *********** @ googlemail.comwrote in message

news:11 ******************* **@y66g2000hsf.googlegro ups.com ...


大家好,


我有一个现有的Visual C ++项目构建DLL(不是COM或

ATL或任何东西)。我希望在Visual Studio 2005 C#

项目中使用此DLL,而无需在C#项目中再次定义每个函数和结构(我相信使用pinvoke)。 br />

我已经做了一些阅读,似乎我所追求的是

可能。我已经将旧的C ++项目添加到我的新C#解决方案中并设置

编译器使用/ clr。然后我可以添加DLL作为对

C#项目的引用,没有任何错误。但是,我被困在那里。我如何实际调用DLL中的任何方法?
?添加使用< DLL

名称>产生错误,我不知道还有什么可以尝试。


提前致谢,


Ben



你不能用C#调用本机类的方法,用/ clr编译你的C ++文件不要
把你的本机类转换为托管类。

您所能做的就是使用VC8实现托管类(包装器或填充程序),这些类可以创建非托管类的实例并将调用转发给非托管方法。


Willy。


3月26日20:52,Nicholas Paldino [.NET / C#MVP]"

< m ... @ spam.guard.caspershouse.comwrote:


Ben,


这个dll,它是导出函数还是类?如果它使用函数和

结构,那么你不应该在项目中添加一个引用到dll。

你必须通过P调用这些函数/调用

图层,这需要在代码中重新声明结构和

函数。


如果如果你想访问类,那么你必须在你的类周围创建一个托管的

包装器。只是设置/ clr开关不会

使您的代码可以在.NET中访问。


希望这会有所帮助。


-

- Nicholas Paldino [.NET / C#MVP]

- m ... @ spam.guard.caspershouse.com


" BDowling" < ben.m.dowl ... @ googlemail.com写信息


新闻:11 ******************* **@y66g2000hsf.googlegro ups.com ...


大家好,


我有一个现有的Visual C ++项目,它构建一个DLL(不是COM或

ATL或任何东西)。我希望在Visual Studio 2005 C#

项目中使用此DLL,而无需在C#项目中再次定义每个函数和结构(我相信使用pinvoke)。


我已经做了一些阅读,似乎我所追求的是

可能。我已经将旧的C ++项目添加到我的新C#解决方案中并设置

编译器使用/ clr。然后我可以添加DLL作为对

C#项目的引用,没有任何错误。但是,我被困在那里。我如何实际调用DLL中的任何方法?
?添加使用< DLL

名称>产生错误,我不知道还有什么可以尝试。


先谢谢,


Ben



嗨Nicholas,


DLL只有函数,没有类。我希望不必使用P / Invoke层来获得
,但这是唯一的选择吗?


如果是这样,有任何自动生成方式P / Invoke定义
C#代码中的
?否则,这将是一个真正的痛苦,因为我

有很多功能。


就像我说的,我有Visual C ++项目,产生了DLL,所以

有什么方法可以修改这个项目,以便更容易与我的C#one集成吗?理想情况下,我希望得到的DLL仍然可以被非.NET项目使用。


谢谢,Ben


Hi all,

I have an existing Visual C++ project that builds a DLL (not COM or
ATL or anything). I wish to use this DLL in a Visual Studio 2005 C#
project without having to define each function and structure again in
the C# project (using pinvoke I believe).

I''ve done some reading around and it seems that what I''m after is
possible. I''ve added the old C++ project to my new C# solution and set
the compiler to use /clr. I can then add the DLL as a reference to the
C# project without any errors. However, I''m stuck there. How do I
actually invoke any of the methods in the DLL? Adding "using <DLL
name>" produces an error, and I''m not sure what else to try.

Thanks in advance,

Ben

解决方案

Ben,

The dll, does it export functions, or classes? If it uses functions and
structures, then you should not add a reference in your project to the dll.
You will have to make the calls to these functions through the P/Invoke
layer, which will require a re-declaration of the structures and the
functions in your code.

If you want to access classs, then you will have to create a managed
wrapper around your classes. Just setting the /clr switch is not going to
make your code accessible in .NET.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"BDowling" <be***********@googlemail.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.com...

Hi all,

I have an existing Visual C++ project that builds a DLL (not COM or
ATL or anything). I wish to use this DLL in a Visual Studio 2005 C#
project without having to define each function and structure again in
the C# project (using pinvoke I believe).

I''ve done some reading around and it seems that what I''m after is
possible. I''ve added the old C++ project to my new C# solution and set
the compiler to use /clr. I can then add the DLL as a reference to the
C# project without any errors. However, I''m stuck there. How do I
actually invoke any of the methods in the DLL? Adding "using <DLL
name>" produces an error, and I''m not sure what else to try.

Thanks in advance,

Ben



"BDowling" <be***********@googlemail.comwrote in message
news:11*********************@y66g2000hsf.googlegro ups.com...

Hi all,

I have an existing Visual C++ project that builds a DLL (not COM or
ATL or anything). I wish to use this DLL in a Visual Studio 2005 C#
project without having to define each function and structure again in
the C# project (using pinvoke I believe).

I''ve done some reading around and it seems that what I''m after is
possible. I''ve added the old C++ project to my new C# solution and set
the compiler to use /clr. I can then add the DLL as a reference to the
C# project without any errors. However, I''m stuck there. How do I
actually invoke any of the methods in the DLL? Adding "using <DLL
name>" produces an error, and I''m not sure what else to try.

Thanks in advance,

Ben


You can''t call methods on native classes from C#, compiling your C++ files with /clr don''t
turn your native classes into managed classes.
All you can do is implement managed classes (wrapper or shim) using VC8, these classes can
create instances of the unmanaged classes and forward the calls to their unmanaged methods.

Willy.


On 26 Mar, 20:52, "Nicholas Paldino [.NET/C# MVP]"
<m...@spam.guard.caspershouse.comwrote:

Ben,

The dll, does it export functions, or classes? If it uses functions and
structures, then you should not add a reference in your project to the dll.
You will have to make the calls to these functions through the P/Invoke
layer, which will require a re-declaration of the structures and the
functions in your code.

If you want to access classs, then you will have to create a managed
wrapper around your classes. Just setting the /clr switch is not going to
make your code accessible in .NET.

Hope this helps.

--
- Nicholas Paldino [.NET/C# MVP]
- m...@spam.guard.caspershouse.com

"BDowling" <ben.m.dowl...@googlemail.comwrote in message

news:11*********************@y66g2000hsf.googlegro ups.com...

Hi all,

I have an existing Visual C++ project that builds a DLL (not COM or
ATL or anything). I wish to use this DLL in a Visual Studio 2005 C#
project without having to define each function and structure again in
the C# project (using pinvoke I believe).

I''ve done some reading around and it seems that what I''m after is
possible. I''ve added the old C++ project to my new C# solution and set
the compiler to use /clr. I can then add the DLL as a reference to the
C# project without any errors. However, I''m stuck there. How do I
actually invoke any of the methods in the DLL? Adding "using <DLL
name>" produces an error, and I''m not sure what else to try.

Thanks in advance,

Ben

Hi Nicholas,

The DLL only has functions, no classes. I was hoping not to have to
use the P/Invoke layer, but is this the only option?

If so is there any automated way to generate the P/Invoke definitions
in the C# code? It is going to be a real pain otherwise, because I
have LOTS of functions.

Like I said, I have the Visual C++ project that produces the DLL, so
is there some way I could modify this project to make it easier to
integrate with my C# one? Ideally I''d like the the resulting DLL still
to be usable by non .NET projects.

Thanks, Ben


这篇关于在C#项目中使用C ++ DLL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆