简体   繁体   English

在C#中包装Visual C ++

[英]Wrapping Visual C++ in 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. 我需要使用C ++进行一些过程注入,但是我更愿意将C#用于除底层内容以外的所有其他功能。 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. 我听说过“函数包装”和“封送处理”,并且已经在Google上进行了大量搜索,并且在这里和那里发现了一些信息,但是我仍然很缺乏。

Things I have read in order of usefulness; 我按有用性顺序阅读的内容;
http://msdn.microsoft.com/en-us/library/ms235281(VS.80).aspx http://msdn.microsoft.com/zh-CN/library/ms235281(VS.80).aspx
http://www.drdobbs.com/cpp/184401742 http://www.drdobbs.com/cpp/184401742
http://geeklit.blogspot.com/2006/08/calling-c-lib-from-c.html http://geeklit.blogspot.com/2006/08/calling-c-lib-from-c.html

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#? 如何将所有较低级别的内容(本机C ++)包装在C#中,以便可以用更熟悉的C#语言轻松地命令这些功能?

Any information on the topic is much appreciated. 非常感谢您提供有关该主题的任何信息。

I think P/Invoke is really the most straightforward approach: 我认为P / Invoke确实是最简单的方法:

  • Create a DLL in unmanaged C++, containing all the functionality you need to do the injection. 非托管 C ++中创建一个DLL,其中包含执行注入所需的所有功能。
  • In your C# code, declare some static extern methods, and use the DllImport attribute to point them to your C++ dll. 在您的C#代码中,声明一些static extern方法,并使用DllImport属性将它们指向您的C ++ dll。 For more details, see the link provided by arul in his answer . 有关更多详细信息,请参见arul其答案中提供的链接。

Note that P/Invoke isn't limited to "windows API functions" — you can call functions exposed by any native DLL. 请注意,P / Invoke不限于“ Windows API函数”,您可以调用任何本机DLL公开的函数。

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. P / Invoke的潜在缺点是您必须为C ++函数提供签名,可能指定一些不太明显的编组。 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#. 在这种情况下,您可以考虑构造一个COM服务器而不是“普通” DLL,并使用COM互操作从C#调用代码。

您要使用P / Invoke,请参阅《 MSDN杂志》

If Pinvoking isn't what you want to do, then create a managed C++ application. 如果您不想使用Pinvoking,请创建一个托管的C ++应用程序。 Use native C++ to do the process injection stuff. 使用本机C ++进行过程注入。 Use managed c++ to create a .NET friendly interface to this native behaviour. 使用托管的c ++可以为该本机行为创建一个.NET友好界面。 This can then be called from C#. 然后可以从C#中调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM