2

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.

Oleg Vazhnev
  • 23,239
  • 54
  • 171
  • 305
  • Hardly can you create dll export table (unless you do it in IL or managed C++); perhaps it is easier to create COM? – Dmitry Bychenko Jun 27 '13 at 10:23
  • COM is slooooow and complicated and i'm writing low-latency software and it's just temporary solution so i would prefer something straightforward easy and fast. – Oleg Vazhnev Jun 27 '13 at 10:24
  • i have c# part that i don't need to rewrite to c++. because i will throw it away once ported to linux. but until that I need to use c# from c++. – Oleg Vazhnev Jun 27 '13 at 10:28
  • In general, a COM call is not slower than plain call to C a method in a dll (in this context), and not much more complicated than using a class. However, even if it were, I can't see how it is relevant for a temporary solution. Unless you can prove the significant difference, I would say that COM is the best way to go. arg/return value marshalling has to be done in both scenarios, and it might easily take majority of time need to execute a call. – Zdeslav Vojkovic Jul 09 '13 at 05:18

2 Answers2

3

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#

Community
  • 1
  • 1
Tigran
  • 61,654
  • 8
  • 86
  • 123
1

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++?

Community
  • 1
  • 1
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • which of these options has better latency? what type of project should I create in VS, C# "Class Library"? – Oleg Vazhnev Jun 27 '13 at 10:40
  • what about https://sites.google.com/site/robertgiesecke/Home/uploads/unmanagedexports ? – Oleg Vazhnev Jun 27 '13 at 10:42
  • Both options have low latency, with changing IL being slightly better. – Dmitry Bychenko Jun 27 '13 at 10:55
  • All we need is a small change in the generated IL, so many libraries could do it (Nuget is one of them). – Dmitry Bychenko Jun 27 '13 at 10:56
  • regarding codeproject reference, i've found there "Resulting assembly will be 32-bit only.", doesn't work for me. will check if `dlltool` supports 64-bit. could you suggest something up-to-date which guarantees to work with VS2012 and 64-bit? – Oleg Vazhnev Jun 27 '13 at 11:46