Solved

Calling a function in the Main application from a DLL

Posted on 2003-12-04
7
201 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
  • 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 31
not able to insert into temp table 68 150
Expand data scrubbing tool 13 31
sumHeights  challenge 17 61
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

757 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now