Search This Blog

Saturday, November 22, 2008

C#.Net code to call MFC DLL

This is the code to call a function from vc++ DLL
(for example "MyDll.dll" which we created in this post click here )
  1. Create a new C#.net windows application.
  2. Copy the "MyDll.dll" to your project path, which we created in this post Click here.
  3. Add this this following lines as below.
//using System.Runtime.InteropServices is used to call dll function in C#
using System.Runtime.InteropServices; //add this line

namespace WindowsApplication1
{
public partial class Form1 : Form
{

[DllImport("MyDll.dll", CharSet=CharSet.Auto)] //add this line
internal static extern Int32 egAdd(int a, int b); //add this line

public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{
int nRes = egAdd(2, 4); //add this line
MessageBox.Show(nRes.ToString()); //add this line
}

}
}

No comments: