user2514673
user2514673

Reputation: 109

Means for compiling C# run-time code into a .dll in native assembly code (callable from a C++ application)?

My friend and I have put together a C# run-time library which we want to compile, or convert, into a .dll in native assembly code and call from a C++ application.

I suspected we might not be the first ones embarking on this type of an effort. So I looked around and came up with the following:

  1. How to migrate from C++ COM to C++CLI
  2. Managed C++ wrappers for legacy C++ libraries
  3. Writing a managed wrapper for unmanaged (C++) code - custom types/structs
  4. how to convert C# to C++
  5. http://bytes.com/topic/c-sharp/answers/268197-convert-c-form-c-net

However, these links all talk about how to port C++ code to CLI or C#. There is no mention of how to port C# managed code to a standalone .dll library in native assembly callable from a C++ application.

Any thoughts?

Best regards,

Baldur

Upvotes: 3

Views: 1215

Answers (2)

Bassam Alugili
Bassam Alugili

Reputation: 17003

The keyword for your problem COM Component in C++ look the example below:

You can regsiter your .Net assembly with COM after that you can call it from C++.

See the example below:

[Guid(123565C4-C5FA-4512-A560-1D47F9FDFA20")]
public interface ISomeInteface
{
    [DispId(1)]
    string FirstFunction{ get; }

    [DispId(2)]
    void SecondFunction();
}

[ComVisible(true)]
[Guid(123565C4-C5FA-4512-A560-1D47F9FDFA20")]
[ClassInterface(ClassInterfaceType.None)]
public sealed class SomeInteface: ISomeInteface
{
    public SomeInteface()
    {
    }

   public string FirstFunction
   {
    get { return "Work here"; }
   }

    public void SecondFunction()
   {
   }
}

More info;You can take a look in the following links: http://social.msdn.microsoft.com/Forums/vstudio/en-US/440aca96-288a-4d5c-ab9a-87ea2cdd5ca8/how-to-call-c-dll-from-c http://support.microsoft.com/kb/828736

Upvotes: 1

D Stanley
D Stanley

Reputation: 152556

Essentially you need an unmanaged wrapper around a managed library, which is the opposite of the more common problem.

The basic idea is to create a C++ library that has unmanaged exports (native C-style functions) but internally used managed C++ to interact with your library.

Be aware that marshalling unmanaged data across layers may be tricky - hopefully you only need to use very basic types (strings and numbers).

Also note that the client will still need to have the appropriate .NET framework installed - wrapping the library will not get around that requirement.

Here's a few links that may help:

Calling a C# function from unmanaged C++

Using managed code in an unmanaged application

Upvotes: 1

Related Questions