none
Exposing a C# DLL through (managed) C++ ? RRS feed

  • Question

  • Hi guys,

    I have a C# class library that contains methods that need to be used with an external application. Unfortunately this external application only supports external APIs in C/C++.

    Suppose I have a takeIntReturnDoubleArray method in this C# library that takes an integer and returns an array of doubles. All I need to do is have a C++ method that takes an integer, calls the C# library and returns an array of doubles to the calling application.

    So in essence the C++ library is just acting as an intermediary between the C# wrapper and the external application.

    Is there an easy way to do this? Do I have to do anything special on the C# side to allow it to be imported into C++ easily? I have seen some talk of using the #import statement but I really have no idea what I am doing when it comes to C++.

    Please note I am using Visual C++ so managed code is a preferred option for me.

    I'd like to avoid using COM at all costs because I know absolutely nothing about it.

    I really do need rather explicit instructions because I have never done any C++ coding before. 

    What is the approach I should be taking here?
    Tuesday, February 17, 2009 2:13 AM

Answers

  • You haven't given any details about the extensibility model of the app you're trying to use.  Hard to make the call, but COM is most typically the best way to get a native app that knows nothing about the CLR to use managed code.  Not knowing anything about C++ and COM will be a significant hang-up to get this project to a good end.  Find help first.  Not forum help.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Monday, February 23, 2009 6:58 AM
    Tuesday, February 17, 2009 12:35 PM
    Moderator

All replies

  • You haven't given any details about the extensibility model of the app you're trying to use.  Hard to make the call, but COM is most typically the best way to get a native app that knows nothing about the CLR to use managed code.  Not knowing anything about C++ and COM will be a significant hang-up to get this project to a good end.  Find help first.  Not forum help.

    Hans Passant.
    • Marked as answer by Zhi-Xin Ye Monday, February 23, 2009 6:58 AM
    Tuesday, February 17, 2009 12:35 PM
    Moderator
  • Your "external application" probably needs C exports.

    It is not a good idea to directly export unmanaged wrappers with a managed implementation. It is possible, but not recommended, because the CLR doesn't really play nice being scoped to a DLL.

    As Hans stated, the best solution is to develop the C# DLL to export a (out of process) COM object, and then translate that to unmanaged C exports.

           -Steve
    Tuesday, February 17, 2009 6:33 PM