Oleg Vazhnev
Oleg Vazhnev

Reputation: 24067

create c# dll which can be used from both c# and c++

I'm in the middle of moving my solution from c# to c++. I've created several native c++ dll and they just fine. It's easy to connect native dll to both c++ and c#. For c# i'm using P/Invoke for methods execution and delegate for callback, i just pass pointer to unmanaged memory and read it using Marshall class.

Now I have opposite situation - I need to create C# project that NOW will be used from another C# project but LATER i will rewrite main project to C++ and so I need to access C# project from C++. It should offer just several methods: PlaceOrder(void* pointerToStruct) CancelOrder(void* pointerToStruct) and one call-back delegate OrderUpdated(void* pointerToStruct)

I'm looking for hints and examples. How can I create C# project which will be usable from both native C++ and C# and offer several methods + one callback.

In particular I don't now what should I do with memory - should I write to unmanaged memory at c# and read from it at c++ or should I write to managed memory at c# and read managed memory in c++ somehow... Pointer to structures which I pass at parameters should point to unmanaged memory or managed memory etc.

Upvotes: 2

Views: 471

Answers (2)

Dmitrii Bychenko
Dmitrii Bychenko

Reputation: 186678

The problem with dll written on C# is that it's impossible to make export table (mark static methods as being exported): there's DllImportAttribute but there's no DllExportAttribute in C#. There're two by-ways though:

  1. Write the dll (at least partially) on managed C++ that is specially designed for this purpose.
  2. Change generated IL after C# source code compilation: e.g. http://winch.pinkbile.com/wiki/index.php/Main/Dlltool http://www.codeproject.com/Articles/37675/Simple-Method-of-DLL-Export-without-C-CLI

See also

Is is possible to export functions from a C# DLL like in VS C++?

Upvotes: 1

Tigran
Tigran

Reputation: 62246

You may want on creation of COM component: Create COM component and ActiveX controls in .Net

If you are developing in Windows8, you may think of creation Windows Component:

Creating Windows Runtime Components in C#

Upvotes: 3

Related Questions