×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

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

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

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

(OP)
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!

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

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

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

(OP)
Thanks for your reply. I solved it like this:

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


double _stdcall MyExportedMethod(double param)
{
   theApp.myInstance.MethodIwouldLikeToInvoke();
   return 0;
}

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close