Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Calling a function in the Main application from a DLL

Posted on 2003-12-04
7
Medium Priority
?
207 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 

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 400 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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

722 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