包装的Visual C ++在C# [英] Wrapping Visual C++ in C#

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

问题描述

我需要用C ++做一些进程注入,但我会preFER用C#的一切比低层次的东西等。我听说过功能包装和整编,并做了相当多的谷歌搜索和发现比特的信息在这里和那里,但我还是很缺乏。

I need to do some process injection using C++ but I would prefer to use C# for everything other than the low level stuff. I have heard about "function wrapping" and "marshaling" and have done quite a bit of google searching and have found bits of information here and there but I am still really lacking.

的东西;
http://msdn.microsoft.com/en-us/library/ms235281 (VS.80)的.aspx
http://www.drdobbs.com/cpp/184401742
http://geeklit.blogspot.com/2006/08 /calling-c-lib-from-c.html

Things I have read in order of usefulness;
http://msdn.microsoft.com/en-us/library/ms235281(VS.80).aspx
http://www.drdobbs.com/cpp/184401742
http://geeklit.blogspot.com/2006/08/calling-c-lib-from-c.html

我如何包装所有低层次的东西(原生C ++)在C#中,所以我可以很容易地命令我更舒服,C#语言的功能呢?

How can I wrap all the lower level stuff (native C++) in C# so I can easily command those functions in a language I am more comfortable with, C#?

在主题的任何信息,多AP preciated。

Any information on the topic is much appreciated.

推荐答案

我想的P / Invoke是真正的最直接的方法:

I think P/Invoke is really the most straightforward approach:

  • 创建一个DLL中的非托管的C ++中,包含了所有你需要做注射的功能。
  • 在C#code,声明一些静态外部方法,并使用的DllImport 属性指向他们到C ++ DLL。欲了解更多详细信息,请参阅 arul 中的他的回答
  • Create a DLL in unmanaged C++, containing all the functionality you need to do the injection.
  • In your C# code, declare some static extern methods, and use the DllImport attribute to point them to your C++ dll. For more details, see the link provided by arul in his answer.

注意的P / Invoke不限于Windows API函数—你可以调用任何本机DLL导出的函数。

Note that P/Invoke isn't limited to "windows API functions" — you can call functions exposed by any native DLL.

P /调用的一个潜在缺点是,你必须提供签名的C ++函数,可能指定一些不那么明显的编组。在这种情况下,你可以考虑构建,而不是一个普通DLL COM服务器,并使用的 COM互操作调用从C#的code。

A potential downside of P/Invoke is that you will have to provide the signatures for your C++ functions, possibly specifying some less-than-obvious marshalling. In that case, you could consider constructing a COM server instead of a "plain" DLL, and using COM interop to call your code from C#.

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

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