Solved

Calling a function in the Main application from a DLL

Posted on 2003-12-04
7
206 Views
Last Modified: 2013-11-20
Hi,

There is this application that has a scenario like this:

Main Application:
SDI with CListView
BaseDialog

DLL:
MFC Extension DLL
Derived Dialog (base from BaseDialog)

I'm using dynamic loading to create an instance of the DeriveDialog and pass it to the BaseDialog so that I can manipulate the functions of DeriveDialog in the Main Application.

But this is the problem, how about if there is an event in the DeriveDialog that it needs to call a function in the Main Application?

I tried to pass a pointer of the main application to function inside the DerivedDialog (implemented also as virtual in the BaseDialog) but I'm having a link error when I call the function inside the main Application

Example:

Main Application: (Implemented as SDI with CListView)
TestView::Hello() - function
BaseDialog:
virtual void Test(TestView* m_pView)

DLL: (Implemented as MFC Extension DLL)
DerivedDialog:

void Test(TestView* m_pView)
{
   m_pView->Hello() - Linking error
}


I hope someone can help on my problem.

Mhelinx


0
Comment
Question by:mhelinx09
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9880255
Use PostMessage.
Pass the view handle (not pointer) to the dll function
Good luck
0
 

Author Comment

by:mhelinx09
ID: 9880294
Rosmon,

You're my savior!!!

Can you elaborate it? I checked MSDN but they have a small definition about it.

Thanks!!!

Mhelinx
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9880376
Main Application: (Implemented as SDI with CListView)


in header
afx_msg long Hello();

#define WM_USER_HELLO WM_USER+100

BEGIN_MESSAGE_MAP()
      ON_MESSAGE(WM_USER_HELLO, Hello)
END_MESSAGE_MAP()

TestView::Hello() - function




BaseDialog:
virtual void Test(HWND hWnd)

DLL: (Implemented as MFC Extension DLL)
DerivedDialog:

#define WM_USER_HELLO WM_USER+100

void Test(HWND hWnd)
{
      ::PostMessage(hWnd, WM_USER_HELLO, 0, 0);
}


Call this dll function from view like this

*pfnTest(this->m_hWnd);

here this->m_hWnd is the window handle of the View class

Good Luck
0
Get MySQL database support online, now!

At Percona’s web store you can order your MySQL database support needs in minutes. No hassles, no fuss, just pick and click. Pay online with a credit card.

 

Author Comment

by:mhelinx09
ID: 9880453
Many thanks rosmon,

How about if you have parameter on the function (e. g. CString)

TestView::Hello(CString sInput) - function

Thanks!

Mhelinx
0
 
LVL 23

Accepted Solution

by:
Roshan Davis earned 100 total points
ID: 9880492
PostMessage with String is unsafe....

Use another function in DLL like GetString.... that will read the string when that event comes.....


or use

char *psz = new cha[100];
lstrcpy(psz, "Test String");
::PostMessage(hWnd, WM_USER_HELLO, (ULONG)psz, 0);



Main Application: (Implemented as SDI with CListView)


in header
afx_msg long Hello(WPARAM wParam, LPARAM lParam);

#define WM_USER_HELLO WM_USER+100

BEGIN_MESSAGE_MAP()
    ON_MESSAGE(WM_USER_HELLO, Hello)
END_MESSAGE_MAP()

TestView::Hello(WPARAM wParam, LPARAM lParam) - function
{
       char *psz = (char *) wParam;

    // DELETE THE STRING AFTER USE

    delete []psz;
}

Rosh :)


0
 

Author Comment

by:mhelinx09
ID: 9882411
Thanks a lot savior.

This what I did.

Base Dialog:
GetHandle(HWND hWnd)
protected:
HWND m_hWndHandle;

Main Application:
GetHandle(this->hWnd);

DO you think this is safe?

Thanks again.
Mhelinx
0
 
LVL 23

Expert Comment

by:Roshan Davis
ID: 9882429
Yes, its fine...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…

630 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question