Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am new learner and developing an MFC application and MFC shared DLL.
Main application has a static function that is being called by dll as a callback and it returns a text string.
I need to show this text in a edit box in form view. Please advise how to do it.

PlotterDoc.h

class PlotterDoc : public CDocument
{
public:
// A static function that is being called from DLL
static void __declspec(dllexport) callMeFromDll(string str) {
CString cstr(str.c_str());
// This cstr need to be shown in a edit box.
AfxMessageBox(cstr);
}
}

What I have tried:

Not sure how to do it in c++. In Flash we use to store class instance and call it's function.
Posted
Updated 7-Feb-21 7:38am

You can't. A static function has no access to any instance of the containing class, so it can't find out which instance of the form holds the control you want to modify. Indeed, because it is a static function, it is not necessary or guaranteed that any instance of the class have been created, or will ever be crated when the function is called.

If you want access to controls, you must use non-static (instance) functions, and call them using the instance you wish to update.
 
Share this answer
 
Comments
Bhanunjay 7-Feb-21 7:27am    
Thanks for your reply. One small ray of hope. There is no way we can store the class instance in a static variable instanceRecoder when class constructor is called? Later we call the class method using the instanceRecoder->doSomething();
OriginalGriff 7-Feb-21 7:58am    
you can, but ... really and truly you don't want to.
There is only one static variable for all instances, and if two pieces of code want to do similar things it will cause some unpredictable effects.

I think you need to rethink why you need a static function, if it's job is to update instance data!
Bhanunjay 7-Feb-21 8:57am    
I am developing an MFC application and MFC shared DLL. The MFC application needs to Pass a callback function to DLL that DLL will call when needed.

At first attempt I tried to pass the member callback pointer and class instance to DLL and store these in DLL memory and execute when needed. But I am not getting proper way to send class object instance. The pointer to function is reaching to DLL but when I try to call the function it shows error trying to execute a non function item

Then I attempted to add static function as callback and got success.
Bhanunjay 7-Feb-21 8:59am    
Please advise what is proper way of sending callback function to DLL so DLL can call it when needed.
Bhanunjay 7-Feb-21 10:14am    
Thanks, problem solved. I got success in passing class instance to DLL finally.
You must create some global access to the edit box, like with a global pointer which is uses in the callback.

When you have threading problems with it you may use the WM_SETTEXT message
.
another way is with a PostThreadMessage to call an message handler in the app. Than store the string in a global value.
Test your solution in different scenarios like Background or Lock Screen.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900