Search This Blog

Saturday, November 22, 2008

An Example to call MFC DLL using function pointer in C#.net

//An Example to call MFC DLL using function pointer in C#.net
  1. Create new C#.Net windows application.
  2. Copy the "MyDll.dll" to your project path, which we created in this post Click here.
  3. Create two Textbox textBox1 and textBox2.

using System.Runtime.InteropServices; //Add this Line

namespace WindowsApplication1
{
public partial class Form1 : Form
{
private void button1_Click(object sender, EventArgs e)
{
//Add the Following Lines
IntPtr MyDll = LoadLibrary("MyDll.dll");
IntPtr procaddr = GetProcAddress(MyDll, "egAdd");
MyAdd mAdd = (MyAdd)Marshal.GetDelegateForFunctionPointer(procaddr, typeof(MyAdd));
int a = Convert.ToInt32(textBox1.Text);
int b = Convert.ToInt32(textBox2.Text);
int nRes = mAdd(a, b);
MessageBox.Show(nRes.ToString());
}
internal delegate int MyAdd(int a,int b);
[DllImport("kernel32.dll")]
internal static extern IntPtr LoadLibrary(String dllname);
[DllImport("kernel32.dll")]
internal static extern IntPtr GetProcAddress(IntPtr hModule, String procname);
}
}

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
}

}
}

Saturday, November 8, 2008

How to create MFC DLL without wrapper class

Steps to create DLL
  • Select File--> New
  • Select Projects TAB and select MFC AppWizard(dll)
  • Give Project Name as "MyDLL".
  • Select "Regular DLL with MFC statically Linked" and click Finish.
  • Select "Insert" Menu and choose "New Class".
  • Select class type as "Generic Class"
  • Give class name as "MyClass".
  • Open "MyClass.h" in solution explorer window of File View tab.
  • Paste the following code as in Figure 1.
  • class CMyClass
    {
    public:
    __declspec(dllexport) CString Hello(CString csName);
    __declspec(dllexport) CMyClass();
    __declspec(dllexport) virtual ~CMyClass();

    };
  • Open "MyClass.cpp" in solution explorer window of File View tab.
  • Paste the following code as in Figure 2.
  • CString CMyClass::Hello(CString csName)
    {

    csName="Hello"+csName;

    return csName;
    }
  • Now compile and run the program.
  • Now Make a copy "MyClass.h" from you project folder and Copy of "MyDLL.lib" and "MyDLL.dll" from debug folder to create a exe file.
Steps to create EXE apllcation
  • Select File--> New
  • Select Projects TAB and select MFC AppWizard(exe).
  • Give Project Name as "MyEXE".
  • Select "Dialog Based" and click Finish.
  • paste "MyClass.h" from dll project folder to your exe Project folder.
  • Paste "MyDLL.lib" and "MyDLL.dll" from debug of DLL project folder to exe project Folder.
  • Open Project -->settings and select Link tab and type "Debug/MyDll.lib" in object/Library modules
  • Open " File View" tab in solution explorer window.
  • Right click on the project name and select add files to project.
  • select "MyClass.h" and click ok.
  • Now Open " Resource View" tab in solution explorer window and select Dialog and open "IDD_MYEXE_DIALOG".
  • Now add a "Edit Box" to your dialog window and Right click on edit box and select "Class Wizard" .
  • select "member variable" tab and select your edit box and click "Add variable " button.
  • Give member variable name as "m_edName" and category as Control.
  • Now click "ok" button and add the following code in "MyEXEDlg.cpp" as in Figure 3.
  • #include "myclass.h" //at the top
  • void CMyEXEDlg::OnOK()
    {
    CString csName;
    CString csResult;
    m_edName.GetWindowText(csName);
    CMyClass obj;
    csResult= obj.Hello(csName);
    AfxMessageBox(csResult);
    // CDialog::OnOK();
    }
  • Now Run and execute program. and get result as in figure 4.