Solved

Calling a function in the Main application from a DLL

Posted on 2003-12-04
7
204 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 49
Need to retreive data from .TFS file exported by DVR 8 223
centeredAverage challenge 8 180
Detect file exist or not 3 181
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

820 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