Solved

Using c++ lib file in c#

Posted on 2010-09-02
5
534 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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C# Service FileSystemwatcher isse 9 65
WPF user control poped up from a window 1 51
Set custom font WPF 2 26
C# Web service insert into Oracle table 8 46
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

752 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