0

I am trying to learn Interop from C# and C++/CLI. I am starting with creating a C++/CLI project. Most of the examples I found talks about interop with COM however when I am creating a C++/CLI project in Visual studio 2010, under the project templates for Visual C++ project, I don't see any type for COM. Any ideas? Also can I create just a Visual C++ Class Library project to use for this purpose?

imak
  • 6,489
  • 7
  • 50
  • 73
  • COM interop is used to access a COM server. If you want a C# assembly to use a native C++ class, you can compile the C++ class as mixed-mode. – Morten Frederiksen Oct 01 '11 at 20:07
  • Sorry if I was not clearn enough but I am working towards a scenario where I have a C++ dll that need to be used in C# application. – imak Oct 01 '11 at 20:20

1 Answers1

1

You are mixing it up. There are three distinct ways to interop with native code from a managed program, you don't use them all at the same time:

  • pinvoke through the [DllImport] attribute. Good for C style native APIs
  • interop through COM. Very well supported by the CLR as long as it is the Automation subset and you have a type library. Just works out of the box, no work needed. Adding COM to existing native code that isn't COM enabled is not productive unless you already have good COM programming skills (know ATL)
  • wrapper classes created in the C++/CLI language. When the above options are not enough. Required for interop with native C++ classes.

Learning enough C++/CLI to get the job done requires about 3 weeks, truly learning the language is a multi-month effort. Things to focus on when you just use it for writing wrappers is #pragma managed, the proper use of the hat and the difference between the destructor and the finalizer.

It is possibly worth your time to learn more about it, the syntax heavily resembles the syntax of C++/CX, the language extensions added to the MSVC++ compiler that support writing Metro apps for Windows 8. Knowing C++/CLI is 95% of knowing C++/CX.

Sample quick-start wrapper class in C++/CLI in this answer.

Community
  • 1
  • 1
Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536