Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I call an instance method from an exported method in a MFC DLL?

Status
Not open for further replies.

arnoldw

Programmer
Sep 25, 2007
17
DK
I have created a dll in MFC using the Visual Studio wizard and the dll exports a function called MyExportedMethod:

#include "stdafx.h"
#include "DllProblem.h"

BEGIN_MESSAGE_MAP(CDllProblemApp, CWinApp)
END_MESSAGE_MAP()

CDllProblemApp::CDllProblemApp()
{
}

CDllProblemApp theApp;

BOOL CDllProblemApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}

double _stdcall MyExportedMethod(double param)
{
// Here I would like to call the
// MethodIwouldLikeToInvoke method in class MyClass.
// How do I do that???

return 0;
}

I would like the MyExportedMethod method to invoke a (non-static) method in a class called MyClass:

#include "stdafx.h"
#include "DllProblem.h"
#include "MyClass.h"

IMPLEMENT_DYNAMIC(MyClass, CDialog)

MyClass::MyClass(CWnd* pParent /*=NULL*/): CDialog(MyClass::IDD, pParent)
{
}

MyClass::~MyClass()
{
}

void MyClass::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(MyClass, CDialog)
END_MESSAGE_MAP()

void MyClass::MethodIwouldLikeToInvoke()
{
MessageBox(L"Congratulations! The exported method successfully invoked the correct method!");
}

What should I add to successfully invoke the MethodIwouldLikeToInvoke from the MyExportedMethod method? This may seem like a simple problem and that is ok, this is not a trick question (I'm a beginner C++ programmer). Thank you in advance!
 
You can't call a non-static method without a pointer to the instance of the class containing the method. The best way of doing this is to use a static method to return a pointer for the class instantiation you want to call. If you only want one instance of MyClass running try making MyClass conform to the Singleton design pattern.

Bertha
 
Thanks for your reply. I solved it like this:

class CDllProblemApp...
{
...
MyClass myInstance;
...
};


double _stdcall MyExportedMethod(double param)
{
theApp.myInstance.MethodIwouldLikeToInvoke();
return 0;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top