Solved

Using c++ lib file in c#

Posted on 2010-09-02
5
530 Views
Last Modified: 2012-06-27
Hello,

I have a lib file written in unmanaged c++. (I have access to the source code.)

How can i use it in c# ?
I think that i need to write a wrapper and convert it to a managed dll.
But i don't know where to start.

Can you please show me a working example source code or good links ?
thanks.


0
Comment
Question by:parabellum
5 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 400 total points
ID: 33584520
You can build managed and unmanaged together in one assembly using mixed mode assemblies and IJW (It Just works). You can then access the unmanaged code via the managed C++.

http://msdn.microsoft.com/en-us/library/x0w2664k.aspx

"Mixed assemblies are capable of containing both unmanaged machine instructions and MSIL instructions. This allows them to call and be called by .NET components, while retaining compatibility with components that are entirely unmanaged. Using mixed assemblies, developers can author applications using a mixture of managed and unmanaged functionality. This makes mixed assemblies ideal for migrating existing Visual C++ applications to the .NET Platform."

Below is an example of IJW at work.This simple bit of code shows just how simple it is to use IJW to interface between managed and unmanaged code, no need to mess with GUIDs or other nasty COM things.
// C++ (all in one IJW assembly)

 

#include <iostream>

 

namespace UnmanagedCode

{

        void foo()

        {

                std::cout << "Hello, world" << std::endl;

        }

}

 

namespace ManagedCode

{

        public ref class fooWrapper

        {

        public:

                static void foo()

                {

                        UnmanagedCode::foo();

                }

        };

}

 

 

// C# code (seperate assembly)

 

using System;

using System.Collections.Generic;

using System.Text;

 

namespace scratchcs

{

    class Program

    {

        static void Main(string[] args)

        {

            ManagedCode.fooWrapper.foo();

        }

    }

}

Open in new window

0
 
LVL 14

Assisted Solution

by:Daniel Junges
Daniel Junges earned 100 total points
ID: 33584611
0
 
LVL 1

Expert Comment

by:sureshmunugoti
ID: 33584963

Suppose you are having the unmanged library called TestLib.dll
Then Your managed code should be as shown below...

using System;
using System.Runtime.InteropServices;    
class Consume
{
    [DllImport("TestLib.dll")]
    public static extern void Hello ();

    static void Main ()
    {
                Hello ();
    }
}
0
 
LVL 12

Expert Comment

by:w00te
ID: 33586420
>>ManagedCode.fooWrapper.foo();

How does the C# assembly know the ManagedCode namespace in that example?  I'm guessing it works but I don't know whats happening in the background and I'm interested :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33586954
>> How does the C# assembly know the ManagedCode namespace in that example?

The assembly just needs to be referenced by the C# code (just like any assembly) and that's it. Once a .Net assembly is referenced it is visible globally, unlike unmanaged C++ there things have to be declared.
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now