Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using c++ lib file in c#

Posted on 2010-09-02
5
Medium Priority
?
538 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 1600 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 400 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

722 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