0

I'm trying to avoid COM. I'm designing a mixture C# and C++ controls on a C++ exe.

One method I came up with is PInvoking my C++ exe from C#, and sending windows messages to the C# windows. However the amount of methods I call on the controls base class is too long to justify windows messages.

So, if it's possible to export a whole C# interface to a C++ exe, that would be way easier.

I want to avoid COM because I may have to support windows 2000, and doing COM without relying on the manifest would be a deployment hassle on a software package that currently doesn't set much in the registry.

Lee Louviere
  • 5,162
  • 30
  • 54
  • Too bad you can't use COM since it would make this trivial. – David Heffernan Jun 23 '11 at 21:51
  • Is the only issue with COM that you want to avoid the use of the registry? Generally the registry is used by COM for CoCreate and cross-apartment marshalling; but if you don't need to do these (eg. can use a P/Invoked API instead of CoCreate), then you won't be hitting the registry, and can still use the rest of the CLR's COM interop support. – BrendanMcK Jun 24 '11 at 03:43

2 Answers2

1

You could write a C wrapper for each of your C++ controls, and PInvoke from C#.

For example this C++ class:

class Example
{
  public:
  int MyMethod(int param);
}

and in a extern "C" block in your c++ exe:

void * CreateExample() { return new Example(); }
int Example_MyMethod(void * handle, int param) { reinterpret_cast<Example*>(handle)->MyMethod(param)); }

and in C#:

public class Example
{
 private IntPtr handle;

 public Example()
 {
   handle = _CreateExample();
 }

 public int MyMethod(int param)
 {
    return _MyMethod(param);
 }

 [DllImport("yourdll.exe")]
 private static extern IntPtr _CreateExample();

 [DllImport("yourdll.exe")]
 private static extern int _MyMethod(IntPtr handle, int param);
}
Tom
  • 6,325
  • 4
  • 31
  • 55
0

I use C++/CLI with VS 2010 to do this. You can expose a DLL C interface that can be consumed in the usual way, the implementation would just marshal data from that interface to your C# assembly.

Related question I think Calling C# from C++, Reverse P/Invoke, Mixed Mode DLLs and C++/CLI

Community
  • 1
  • 1
PeskyGnat
  • 2,454
  • 19
  • 22
  • Are we sure you mean that I can make a c++ abstract class with all abstract methods and have C# call those methods. I'm attempting dynamic polymorphism if I can. – Lee Louviere Jun 23 '11 at 20:30