abcprep.com - your onestop GMAT, GRE, PMP hub
.NET FAQs Unleashed!
 
    



    



Calling unmanaged code from C#.NET

 

By: Pranav Thakur

Date: 12th Nov 2009

 

You may have come across a situation where you have to call some unmanaged code. The main purpose of calling unmanaged code is code reusability (or some dependency), which is already written in unmanaged environment such as native C,C++. languages.

 

What is Managed Code? -managed code provides metadata (information) to allow CLR to locate methods encoded in assembly modules to execute. Refer http://www.dotnetuncle.com/Framework/38_managed.aspx

 

What is Unmanaged Code?:- unmanaged code is not executed by CLR. But yes, it is understood by CLR through workarounds. Older programming languages which were used in several applications since the 1970’s such as C,C++, all produce unmanaged code, when compiled by their respective compilers.

 

Following are some easy steps to handle unmanaged code by C#.NET:

 

1) Convert C++ code into a DLL using Visual Studio Win 32 project.

           

a) Open visual studio 2008 and go to File->New->Project

 

            

 

            b) Select Win32 Project under Visual C++ project type. Give a name as “TestProject”.

                       

c) Copy paste  C++ method testMethod() which takes two interger and returns the sum of the two numbers in TestProject.CPP file .

 

           

 

Function definitions should include the extern "C" directive keyword. This forces C linkage (i.e., turns off C++ name mangling).

Note :-We do not have any "direct" way to call unmanaged code, but we must inform the compiler, what we want to call, how, and where is the needed code located.

 

//Sample C++ Code Below

#include "stdafx.h"

 

#include<stdio.h>

#include<conio.h>

 

extern "C"

{

//Note: must use __declspec(dllexport) to make (export) methods as 'public'

      __declspec(dllexport) int testMethod(int a,int b)

      {

                  int c=a+b;

                  return c;

      }

}

 

Above code contains a method which takes two parameters and returns the sum of the two numbers.

 

d) Press Shift+F6. After this a DLL by name TestProject.DLL will be created at debug folder of root directory.

 

2) Call the DLL method by Using System.Runtime.InteropServices namespace

 

            a) Open a new web based project from where we have to call the DLL created above

               

                  Go File->New->Web Site

                       

           

 

 

b) In the code behind page, say Default.aspx.cs page, include the     namespace

 

using System.Runtime.InteropServices;

 

             c) Import the DLL as follows, in your .aspx.cs page:

 

 

public partial class _Default : System.Web.UI.Page

{

 

    [DllImport("C:\\TestProject.dll", EntryPoint = "testMethod", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]

   

public static extern void testMethod();

 

 

    protected void Page_Load(object sender, EventArgs e)

    {

      int c; 

c=testMethod(4,5); //This is a call to the unmanaged DLL

 

    }

}

 

Happy Programming!









Know Your Breath, Know Your Life!!!