17

As I understand it I can use reverse P/Invoke to call C# from C++. Reverse P/Invoke is simply a case of:

  1. Create you managed (c#) class.
  2. Create a c++/cli (formerly managed c++) class library project. Use this to call the managed c# class (presumably via a reference).
  3. Call the c++/cli code from native c++.

Questions:

  1. Is this correct?
  2. Is the DLL created at step 2 known as a mixed mode DLL?
  3. Has C++/CLI completely superseded Managed C++ as far as MS are concerned?
  4. Is COM completely avoided using this approach?
  5. At what point would the CLR be created and run, and by whom?

Thanks in advance

ng5000
  • 12,330
  • 10
  • 51
  • 64

3 Answers3

23

Here are the answers to the best of my knowledge:

  1. Yes
  2. Yes, it is a mixed mode DLL (In fact, you can make one file of your native C++ project managed and create this C++/CLI class in that file and call the code directly from that file. You don't even need a separate DLL to accomplish this.
  3. C++/CLI and Managed C++ both represent same thing. The only difference is that in the older version till Visual Studio 2003, it was termed as Managed C++. Later on, the syntax was changed quite a lot and it was renamed as C++/CLI. Have a look at this link for details.
  4. Yes
  5. CLR will be used whenever a call to the managed DLL is made.
Community
  • 1
  • 1
Aamir
  • 14,882
  • 6
  • 45
  • 69
  • Is it realistic to expect to take an existing Win32 C++ library and recompile using C++/CLI? – ng5000 Jul 01 '09 at 12:13
  • 2
    not really... C++/CLI is a different language and C++ with CLR turned on is different. If you want to do this with CLR turned on in a Visual C++ project, then this can be done but there might be issues while compiling. It will depend on the implementation of that specific library. – Aamir Jul 01 '09 at 12:18
  • Sorry if this is a silly question, but what is the difference between "C++/CLI" and "C++ with CLR turned on"? – ng5000 Jul 01 '09 at 12:20
  • 1
    "C++ with CLR turned ON" is any C++ project in which you can turn CLR ON through its properties. You can do this for one file, multiple files or for the whole project as well. A C++/CLI Project is one which is created like this. In VS2005, NewProject->VisualC++->CLR->NewProject. It Has /CLR switch ON by default and you can straightaway write C++/CLI Code in it without having to do anything else. – Aamir Jul 01 '09 at 12:28
2

Note, you can also do a IL roundtrip of the C# dll and export static methods, which work basically the same as the exports in C++/CLI. However, this is always a post-compile step, and it does have some caveats (which your C++/CLI export have too, btw.).

You can ILDASM both the C# and the C++/CLI DLLs to see how exports are don; it is something like this (from a sample on the net):

// unmexports.il
// Compile with : ilasm unmexports.il /dll
assembly extern mscorlib {}
..assembly UnmExports {}
..module UnmExports.dll
// This flag is important
..corflags 0x00000002
// This instructs the CLR to create a marshaling thunk for the unmanaged caller
..vtfixup [1] int32 fromunmanaged at VT_01
..data VT_01 = int32(0)
..method public static void foo()
{
..vtentry 1:1
..export [1] as foo
ldstr "Hello from managed world"
call void [mscorlib]System.Console::WriteLine(string)
ret
}
Lucero
  • 59,176
  • 9
  • 122
  • 152
1

With ilasm 2.0 you need less code because it can generate most of the gunk by itself. Only the .export directive is really needed now.

// unmexports.il
// Compile with : ilasm unmexports.il /dll
.assembly extern mscorlib { auto }
.assembly UnmExports {}
.module UnmExports.dll
.method public static void foo()
{
  .export [1] as foo
  ldstr "Hello from managed world"
  call void [mscorlib]System.Console::WriteLine(string)
  ret
}
Larrys S. Smith
  • 131
  • 1
  • 4