Solved

Calling a function in the Main application from a DLL

Posted on 2003-12-04
7
203 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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. …
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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