Search This Blog

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.

No comments: