Solved

Using c++ lib file in c#

Posted on 2010-09-02
5
537 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Want Experts Exchange at your fingertips?

With Experts Exchange’s latest app release, you can now experience our most recent features, updates, and the same community interface while on-the-go. Download our latest app release at the Android or Apple stores today!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

635 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