在VC ++中使用动态链接库 [英] Using a dynamic linklibrary in vc++

查看:101
本文介绍了在VC ++中使用动态链接库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如何使用在C#中创建的VC ++中的dll.请提供一些想法.

谢谢,
Laje.

解决方案

我的建议是在C ++/CLI中使用包装dll. C ++/CLI的优点是您可以同时使用托管和非托管(本机)代码:

-在visual studion中创建一个新项目(项目类型= Visual C ++,CLR.类库).
-例如,将其命名为"wrapper.dll"
-添加对C#dll的引用,以便您可以访问其所有内容.
-然后在本机C/C ++中编写一些函数并将其导出(您可能还需要重写一些结构).
-编译wrapper.dll之后,就可以从不受管的应用程序中使用它.

我用Ionic.Zip dll做了一次.这是一个非常易于使用的免费库,用于压缩/解压缩文件,我想从本机代码中使用它.我写了一个包装器,效果很好:)


Olivier已经用最好的方法回答了这个问题.这是我书中的免费章节,可能会帮助您采用这种方法:

http://www.manning.com/sivakumar/sample-ch04_sivakumar.pdf [ ^ ]

查看 4.2.1从本机代码访问托管库部分.它涵盖了两种方法,第一种使用CCW,第二种使用基于C ++/CLI的互操作(上面提到的Olivier).我喜欢基于它的答案.
还有另一种鲜为人知的方法,该方法基于将方法从.NET程序集直接导出到非托管方法.尽管这是不可能的,但实际上CLI标准允许这样做.此方法使解决方案非常平滑,并且不需要了解C ++或C ++/CLI.

我已经在旧答案中提供了全面的信息和参考:

请参阅:在MFC中加载C#DLL [ 解决方案

My suggestion is to use a wrapper dll in C++/CLI. The advantage of C++/CLI is you can use both managed and unmanaged (native) code:

- In visual studion create a new project (Project type = Visual C++, CLR. Class Library).
- Name it "wrapper.dll" for example
- Add a reference to your C# dll, so you you can access all its content.
- Then write a few functions in native C/C++ and export them (you might need to rewrite a few structures as well).
- Once you compile wrapper.dll, you can use it from an unmanaged application.

I did this once with Ionic.Zip dll. This is a very easy to use and free library for zipping/unzipping files, and I wanted to use it from native code. I wrote a wrapper and it works nicely :)


Olivier has already answered this with the best possible approach. Here''s a free chapter from my book that will possibly help you with taking that approach:

http://www.manning.com/sivakumar/sample-ch04_sivakumar.pdf[^]

Take a look at the section 4.2.1 Accessing a managed library from native code. It covers two approaches, the first one uses CCW, while the second one uses C++/CLI based interop (what Olivier mentioned above).


C++/CLI is a powerful thing; I like the answers based on it.
There is another, little know method based on direct exporting of the method from .NET assembly to unmanaged. Many though it was impossible, but in fact CLI standard allows it. This method makes the solution very smooth, and knowledge of C++ or C++/CLI is not required.

I already gave comprehensive information and references in my old answer:

Please see: loading C# DLL in MFC[^].

—SA


这篇关于在VC ++中使用动态链接库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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