.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> .Net Framework >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Interoperability between Visual C++ and Visual C# languages.

Posted By:Ravi Ranjan Kumar       Posted Date: October 29, 2011    Points: 200    Category: .Net Framework    URL: http://www.dotnetspark.com  

This visual article enable you to understand about Interoperability between vc++ and C# languages.
 

Hey! I am just elaborating my previous article of this forum to giving intention to bargainer for learning Interoperability between C++ and C#.
Have a take look there -[ Interoperability between vb.net and C# languages. ] for my previous article focusing Interoperability between Vb.net and C#. 
from our previous article we learned the the .NET Framework (pronounced dot net) is a software framework that runs primarily on Microsoft Windows. It includes a large library and supports several programming languages which allows language interoperability (each language can use code written in other languages). Programs written for the .NET Framework execute in a software environment (as contrasted to hardware environment), known as the Common Language Runtime (CLR), an application virtual machine that provides important services such as security, memory management, and exception handling. The class library and the CLR together constitute the .NET Framework. 

How to make Interoprebility between C++ and C#

here I am going to create a simple .dll using vc++ languages for simple arithmetic calculation of two parameter. and then calling these .dll using C# code to display the accomplishment of Interoperability feature.

How to design vb.Net code 

Follow the procedure step by step to design dll using vb.net


when you press OK button next you will get an welcome to Win32 Application Wizard then click on Next button as given below :- 


It will open an Application Setting Wizard window. here you need to do minor changes. like you need to select dll from "Application Type" and Empty project from "Additional options" as given below Images 

From -  

To -


Now you are ready to create dll using vc++ language. to create dll you need to crate a file with .cpp extension. follow the given step to create a .cpp extension file. 


It will open Add new Item. here you need to select C++ File(.cpp) and then need to write a file name as Cmdll.


Now given code is used to create dll which will responsible to return arithmetic output based on two number. 

 #include 
extern "C"
{
   __declspec(dllexport) int Add(int x,int y)
{
    return x+y;
}
}
extern "C"
{
         __declspec(dllexport) int Sub(int x,int y)
{
    return x-y;
}
}


Now Design C# program for using dll created in C++ languages. 

to design C# program you can take help from previous article from there

you need to use using InteropServices namespace to excess dll written in C++ languages.  take a look at below program to learn how to use dll for accomplishment of Interoperability. 

First Insert System.Runtime.InteropServices namespace.
using System.Runtime.InteropServices;
and then write below code 
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace PrgrmIntroprblty2
{
    class Program
    {
        // Code will show Interoperability between VC++ and VC#

        [DllImport("Cmdll.dll")]
        public static extern int Add(int x, int y);
       
        [DllImport("Cmdll.dll")]
        public static extern int Sub(int x, int y);

      

        public static void Display(int res, string condition)
        {
            Console.WriteLine("The " + condition + " of two number is : " + res);
        }
        static void Main(string[] args)
        {

            char ch;
            do
            {
                Console.WriteLine("Enter Your Option");
                Console.WriteLine("1 For Addition");
                Console.WriteLine("2 For Substraction");
              
                Console.WriteLine("----------------------------------------------------");
                Console.WriteLine("Enter your Option");
                int op = int.Parse(Console.ReadLine());
                Console.WriteLine("----------------------------------------------------");

                Console.WriteLine("Enter the First Value");
                int n1 = int.Parse(Console.ReadLine());
                Console.WriteLine("Enter the First Value");
                int n2 = int.Parse(Console.ReadLine());

                Console.WriteLine("----------------------------------------------------");
                switch (op)
                {
                    case 1:
                        Display(Add(n1,n2), "Addition");  // Here Add(n1,n2) is function written in VC++
                        break;
                    case 2:
                        Display(Sub(n1, n2), "Substraction");
                        break;              
                 
                    default:
                        Console.WriteLine("Invalid Option Try again...");
                        break;


                }
              Console.WriteLine("Do you want to Carry On...if Yes press Y");
                ch = char.Parse(Console.ReadLine());
            
                Console.WriteLine("\n");
            }
            while (ch == 'y' || ch == 'Y');

            Console.ReadLine();

        }
    }
}

from given code you can see how we are able to see the function written from c++ languages to C# languages. its all happen just because of Interoperability supported by .Net framework. 



Now Finally run program to get the output as given below 

.

highlighting same concept between vb.net and C#.



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend