//MFC code to get the Path of Appliction
CString GetExePath ()
{
TCHAR szPath[_MAX_PATH];
szPath[0] = '\0';
GetModuleFileName( NULL, szPath, sizeof(szPath) );
CString csPath(szPath);
int nPos = csPath.ReverseFind('\\');
if ( nPos != -1)
{
csPath = csPath.Left(nPos+1);
}
else
{
csPath = "";
}
return csPath ;
}
//MFC code to get the Path of DLL if it is loaded from different path
#include <atlbase.h>
CString GetDLLPath ()
{
TCHAR szPath[_MAX_PATH];
szPath[0] = '\0';
GetModuleFileName((HINSTANCE)&__ImageBase, szPath, sizeof(szPath) );
CString csPath(szPath);
int nPos = csPath.ReverseFind('\\');
if ( nPos != -1)
{
csPath = csPath.Left(nPos+1);
}
else
{
csPath = "";
}
return csPath ;
}
Search This Blog
Showing posts with label DLL. Show all posts
Showing posts with label DLL. Show all posts
Saturday, February 21, 2009
MFC code to get the Path of Appliction and DLL
Wednesday, December 10, 2008
Calling DLL with wrapper from Application exe
- Create a MFC Appwizard(exe) the project name "TestDLL"
- Using the wizard select dialog based application.
- Add three "Edit Box" and add member name as m_edA, m_edB and m_edResult and choose Category as variable and Type as int.
- Add a new class with class name "CWrapper" with class type "Generic Class".
- Add a new header file with name "MyAPI.H".
- Edit the following files as the instruction given below.
- copy the "MyDll.dll" DLL created earlier and paste it where your exe application created. By default in "Debug" folder of your Project.
// Add the following lines in MyAPI.H
#define DLLNAME "MyDLL.dll"
//function pointers for Exportable API's
typedef int (*egAddFn) (int A, int B);
// Add the following lines in TestDLLDlg.h
#include "Wrapper.h"
class CTestDLLDlg : public CDialog
{
private:
CWrapper m_Wrapper; //add this line
};
//Add the following lines in Wrapper.h
#include "MyAPI.h"
class CWrapper
{
public:
OnInitialize(); //add this line
OnUnInitialize(); //add this line
HINSTANCE m_hInstance; //add this line
int egAdd(int a,int b); //add this line
egAddFn m_egAddFn; //add this line
};
//Add the following lines in TestDLLDlg.cpp
void CTestDLLDlg::OnBtnAdd()
{
UpdateData(TRUE);
int a=0,b=0,c=0;
a=m_edA; //name of the edit box with type int
b=m_edB; //name of the edit box with type int
if(m_Wrapper.m_egAddFn)
{
c=m_Wrapper.m_egAddFn(a,b);
}
m_edResult=c; //name of the edit box with type int
UpdateData(FALSE);
}
//Add the following lines in Wrapper.cpp:
#include "Wrapper.h"
CWrapper::CWrapper()
{
m_hInstance = NULL;
//Initialize all function pointers to NULL
m_egAddFn = NULL;
OnInitialize();
}
CWrapper::~CWrapper()
{
OnUnInitialize();
}
int CWrapper::OnInitialize()
{
CString csDllPath,csLeft;
TCHAR szPath[MAX_PATH];
int nPos;
szPath[0] = NULL;
GetModuleFileName(NULL,szPath,MAX_PATH);
csDllPath = szPath;
nPos = csDllPath.ReverseFind ('\\');
if(nPos != -1)
{
csLeft = csDllPath.Left (nPos);
csDllPath.Format ("%s\\%s",csLeft,DLLNAME);
}
else
{
csDllPath = DLLNAME;
}
m_hInstance = LoadLibrary(csDllPath);
if(m_hInstance)
{
m_egAddFn = (egAddFn) GetProcAddress (m_hInstance, "egAdd");
}
else
{
m_egAddFn = NULL;
return 0;
}
return 1;
}
int CWrapper::OnUnInitialize()
{
if(m_hInstance)
{
FreeLibrary(m_hInstance);
}
else
{
m_hInstance = NULL;
//Initialize all function pointers to NULL
m_egAddFn=NULL;
return 0;
}
return 1;
}
int CWrapper::egAdd(int a ,int b)
{
if (m_egAddFn)
{
return m_egAddFn (a,b);
}
return 0;
}
MFC DLL with wrapper
- Create a MFC DLL with the project name "MyDLL"
- Add a new class with class name "CMyClass" with class type "Generic Class".
- Edit the following files as the instruction given below.
// Add the following lines in MyClass.h
class CMyClass
{
public:
int Add(int a , int b); //add this Line
};
// Add the following lines in MyDLL.h
#include "MyClass.h"
#ifndef EXP
#define EXP extern "C" __declspec( dllexport )
#endif
#define APPPTR ((CMyDLLApp*) AfxGetApp())
class CMyDLLApp : public CWinApp
{
public:
CMyClass m_MyObj; //add this line
};
// Add the following lines in MyClass.cpp:
#include "stdafx.h"
#include "MyDLL.h"
#include "MyClass.h"
int CMyClass::Add(int a , int b)
{
return(a+b);
}
// Add the following lines in MyDLL.cpp
#include "stdafx.h"
#include "MyDLL.h"
#include "MyClass.h"
EXP int egAdd(int a, int b)
{
int c=0;
c= APPPTR->m_MyObj.Add(a,b);
return c;
}
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.
- 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.
Subscribe to:
Posts (Atom)