Thread: Using a .net DLL in C

  1. #1
    Registered User
    Join Date
    Mar 2017
    Posts
    12

    Using a .net DLL in C

    Hello,

    I am currently using Visual Studio to write a program in C. My program needs to use c# functions that are in a .net DLL.

    For instance, one of the functions I need to use is in the format of:

    myfunction(out Errorstring)

    Am I just able to call this function the way it is and use it? Or will it have to be wrapped? I'm pretty new to Visual Studio, and really have no experience with C#.

    Any help would be greatly appreciated!

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,633
    Looks like a hassle: Calling C# from C

    Why do you "need" to do this? What does the function do? Maybe there is an alternative.
    A little inaccuracy saves tons of explanation. - H.H. Munro

  3. #3
    Registered User
    Join Date
    Mar 2017
    Posts
    12
    Originally a C API was used, but we are being asked to switch to the .net DLL as their are some issues that arise when using the C dll.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Original answer from stackoverflow:
    There are basically two cases to call a .NET DLL from unmanaged code:

    1. The .NET DLL exposes a COM interface. In this case, you can use COM from your C++ code.
    2. The .NET DLL does not expose a COM interface. In this case, you have two possibilities (to make it simple):
      1. host the CLR as described here: Loading the Common Language Runtime into a Process
      2. write a piece of managed C++ code (another DLL - written in C++/CLI) to wrap the .NET DLL and expose 'old way' DLL exports to unmanaged clients.


    I don't specifically know the sharpbox system, but it looks like it's pure .NET and does not expose COM interfaces, so 2.b might be the best way to do it (not so easy...). Maybe it has a REST/Web easier API you could use.

    PS: you can also add exports to a .NET DLL. This is described here: Is is possible to export functions from a C# DLL like in VS C++? but it's kinda hacky.
    Devoted my life to programming...

  5. #5
    Registered User
    Join Date
    Mar 2017
    Posts
    12
    What does it mean when it says "The .NET DLL exposes a COM interface"?

    How do I know if the DLL I am using exposes a COM interface?

    Thanks!

  6. #6
    Registered User
    Join Date
    Jun 2017
    Posts
    157
    To examine a dll you could try ILSpy. GitHub - icsharpcode/ILSpy: .NET Decompiler

    Another option would be to write a little app in C# that enumerates all the types in the dll and checks if a type has an attribute called IsComObject == true
    Intro into reflection in C#
    Reflection in .NET - CodeProject

Popular pages Recent additions subscribe to a feed

Tags for this Thread