Call C++ library in C# [closed]

2019-01-07 14:39发布

问题:

I have a lot of libraries written in C++. I want to call these libraries from C#, however, I have met many problems. I want to know if there is a book or guideline to tell me how to do that.

回答1:

  1. DllImport - http://msdn.microsoft.com/en-us/library/aa984739(VS.71).aspx
  2. Wrapper class - http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/67cc9eea-a4fe-48bd-b8d5-f3c8051ba896


回答2:

If you google "c++ c# interop", you'll find tons of information on this topic.

A couple of links:

http://msdn.microsoft.com/en-us/magazine/cc301501.aspx
http://msdn.microsoft.com/en-us/library/ms235281(VS.80).aspx



回答3:

I recently had to wrap some c++ code in .NET. Although the c++ code was packaged as a dll, the interface was too unfriendly for P/Invoke, so I decided to write it in managed c++, or C++/CLI as it is apparently known now.

I found this tutorial very useful on the syntax. It's not so easy on the eye, but the content seemed pretty good.



回答4:

I'm a big fan of the book C++/CLI in Action which has a couple of useful sample chapters online, at that address.

This intro on CodeProject is a good starting point.

The author of C++/CLI in Action has a number of articles on CodeProject, scroll down to the C++/CLI section on his index.

The Wikipedia article on P/Invoke has a number of reasons why you might not want to use that approach, with which I agree:

  • loss of typing support by the compiler
  • possible data type or alignment issues as you have to map types by hand
  • need to pin garbage-collected objects

The best starting point on MSDN is the summary article.



标签: c# c++ function