mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by c3VzaHU » Sat, 24 Apr 2004 23:41:02 GMT

I am writing a COleControlModule application and I am trying to PostMessage(...) from a global function into the COleControl message queue.
The problem is that if I us
AfxGetMainWnd()->PostMessage(WM_USER+1, 0, 0); it will "go" to the container and not the control
How can I reference the control class to do this
Below, I've listed some pseudo code to illustrate my indent
Please, provide any suggestions, hints, tips,... anything that can point me to a solution
Thank you

////////////////////CUserApp.h/////////////////////////////////
class CUserApp : public COleControlModul

public
BOOL InitInstance()
int ExitInstance()
}
////////////////////CUserCtrl.h/////////////////////////////////
class CUserCtrl : public COleContro
{
.............
public
CUserCtrl()
UserDisplay m_Display; //UserDisplay class derived from CDialo
.............
protected
// Message map
//{{AFX_MSG(CUserCtrl
//}}AFX_MS
afx_msg LRESULT OnErrorMsg(WPARAM wParam, LPARAM lParam)
DECLARE_MESSAGE_MAP(
..............


////////////////////CUserCtrl.cpp/////////////////////////////////
..............
BEGIN_MESSAGE_MAP(CUserCtrl, COleControl
//{{AFX_MSG_MAP(CUserCtrl
..............
//}}AFX_MSG_MA
ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties
ON_MESSAGE((WM_USER+1), OnErrorMsg
END_MESSAGE_MAP(
..............
LRESULT CUserCtrl::OnErrorMsg(WPARAM wParam, LPARAM lParam

LRESULT lResult = 1L;
AfxMessageBox("Got the error msg!")
return lResult


////////////////////UserDisplay.h/////////////////////////////////
class UserDisplay : public CDialo

..............
}

////////////////////UserDisplay.cpp/////////////////////////////////
void UserDisplay::OnTest()

...............
//call the GLOBAL function
ErrorDispatcher(1)
...............


////////////////// GLOBAL function ////////////////////////////////
//////////////////Not a class member///////////////////////////////
void ErrorDispatcher(int error

if (error == XYZ)

//How to use PostMessage(WM_USER+1,....) to reach the CUserCtrl message handler??

}

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by Perry S. Morris » Sun, 25 Apr 2004 01:33:12 GMT


sushu,

Well, if I am reading your pseudo code correctly, you can just use something
like this in your ErrorDispatcher() function:

GetParent()->PostMessage(<msg>, <param>, <param>);

Just make sure your UserDisplay class (the dialog) is a child of CUserCtrl
and you'll be set.




PostMessage(...) from a global function into the COleControl message queue.
container and not the control.
me to a solution.
message handler???

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by Ajay Kalra » Sun, 25 Apr 2004 06:11:38 GMT


PostMessage(...) from a global function into the COleControl message queue.
container and not the control.
me to a solution.
message handler???

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by Ajay Kalra » Sun, 25 Apr 2004 06:14:48 GMT

Since you are sending the message from a global function, you will need to
have access to your CUserCtrl object. I dont know where is this being
created for you. Once you have this object, you can use m_hWnd member to get
the window handle and then use ::PostMessage() on the handle you just
obtained.

In addition, you would be better off to use RegisterWindowMessage/WM_APP
instead of WM_USER as MFC itself uses it internally.




PostMessage(...) from a global function into the COleControl message queue.
container and not the control.
me to a solution.
message handler???

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by c3VzaHU » Sun, 25 Apr 2004 07:11:02 GMT

erry,

Thank you for helping.
I cannot really use GetParent(HWND), because I don't know the HWND. The global function ErrorDispatcher() can't be modified to pass the handle as an argument, for example, so this will not work for me.
I guess, I can declare a global varibale of type HWND and set it to m_hWnd in the CUserCtrl::OnCreate(...), but then what happens if I have multiple CUserCtrl instances within the same ActiveX container???


----- Perry S. Morris wrote: -----

sushu,

Well, if I am reading your pseudo code correctly, you can just use something
like this in your ErrorDispatcher() function:

GetParent()->PostMessage(<msg>, <param>, <param>);

Just make sure your UserDisplay class (the dialog) is a child of CUserCtrl
and you'll be set.


"sushu" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
> I am writing a COleControlModule application and I am trying to
PostMessage(...) from a global function into the COleControl message queue.
> The problem is that if I use
> AfxGetMainWnd()->PostMessage(WM_USER+1, 0, 0); it will "go" to the
container and not the control.
> How can I reference the control class to do this?
> Below, I've listed some pseudo code to illustrate my indent.
> Please, provide any suggestions, hints, tips,... anything that can point
me to a solution.
> Thank you!
>> ////////////////////CUserApp.h//////////////////////////////////
> class CUserApp : public COleControlModule
> {
> public:
> BOOL InitInstance();
> int ExitInstance();
> };
> ////////////////////CUserCtrl.h//////////////////////////////////
> class CUserCtrl : public COleControl
> {
> ..............
> public:
> CUserCtrl();
> UserDisplay m_Display; //UserDisplay class derived from CDialog
> ..............
> protected:
> // Message maps
> //{{AFX_MSG(CUserCtrl)
> //}}AFX_MSG
> afx_msg LRESULT OnErrorMsg(WPARAM wParam, LPARAM lParam);
> DECLARE_MESSAGE_MAP()
> ..............
> }
>> ////////////////////CUserCtrl.cpp//////////////////////////////////
> ...............
> BEGIN_MESSAGE_MAP(CUserCtrl, COleControl)
> //{{AFX_MSG_MAP(CUserCtrl)
> ...............
> //}}AFX_MSG_MAP
> ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties)
> ON_MESSAGE((WM_USER+1), OnErrorMsg)
> END_MESSAGE_MAP()
> ...............
> LRESULT CUserCtrl::OnErrorMsg(WPARAM wParam, LPARAM lParam)
> {
> LRESULT lResult = 1L;
> AfxMessageBox("Got the error msg!");
> return lResult;
> }
>> ////////////////////UserDisplay.h//////////////////////////////////
> class UserDisplay : public CDialog
> {
> ...............
> }
>> ////////////////////UserDisplay.cpp//////////////////////////////////
> void UserDisplay::OnTest()
> {
> ...............
> //call the GLOBAL function.
> ErrorDispatcher(1);
> ...............
> }
>> ////////////////// GLOBAL function /////////////////////////////////

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by Perry S. Morris » Sun, 25 Apr 2004 09:17:32 GMT

ushu,

Since you can't modify the function prototype, how about getting a little
fancy with it by doing something like passing it a pointer to a object and
do some typecasting. That way you may get the data needed (like, error
number, window handle, any other data), etc?

Can something like this be done with your code?

"sushu" < XXXX@XXXXX.COM > wrote in message
news: XXXX@XXXXX.COM ...
global function ErrorDispatcher() can't be modified to pass the handle as an
argument, for example, so this will not work for me.
in the CUserCtrl::OnCreate(...), but then what happens if I have multiple
CUserCtrl instances within the same ActiveX container???
something
CUserCtrl
queue.
point
////////////////////CUserCtrl.cpp//////////////////////////////////
////////////////////UserDisplay.h//////////////////////////////////
////////////////////UserDisplay.cpp//////////////////////////////////
/////////////////////////////////
member////////////////////////////////
CUserCtrl



mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by Joseph M. Newcomer » Sun, 25 Apr 2004 14:58:29 GMT

t is a VERY BAD idea to use codes like WM_USER+n, because they may already be defined. At
the very least, use WM_APP+n, or a Regsitered Window Message.

And NEVER code something like "WM_USER+n" or "WM_APP+n" into your program! Do

#define UWM_WHATEVER (WM_APP+3)

and use UWM_WHATEVER for your message name.

To post to a specific window, you must have a window handle. The problem is that you can't
do this from a global variable, because if you have more than one instance of the control,
all instances share the same global variable, so some control will get the message, not
necessarily the one you want to send it to.

So you need some way to uniquely identify the window you want to notify. I think the use
of the global function is what is going to get you into serious trouble.
joe

On Sat, 24 Apr 2004 08:41:02 -0700, "sushu" < XXXX@XXXXX.COM > wrote:


Joseph M. Newcomer [MVP]
email: XXXX@XXXXX.COM
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by Alexander Nickolov » Sun, 25 Apr 2004 16:18:06 GMT

I'm sorry to be the first to tell you, but what you ask for is
simply impossible. You'll need to modify your design so
your global function accepts some form of argument that
identifies your control. In fact, using a global function is
the first design problem IMO - a class initialized with a
pointer to the control would be infinitely better...

P.S. True, this question has little to do with MFC, but if
you have MFC questions, it would be wise not to cross-
post them into the ATL group...

--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: XXXX@XXXXX.COM
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================


PostMessage(...) from a global function into the COleControl message queue.
container and not the control.
me to a solution.
message handler???

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by c3VzaHU » Mon, 26 Apr 2004 01:56:01 GMT

oe, thanks for the reply
The actual code doesn't use the "WM_USER+1" within the function calls; this was just to illustrate what I was trying to accomplish
Unfortunately, I can't really get away from using the global function and can't change it prototype
Thanks again

----- Joseph M. Newcomer wrote: ----

It is a VERY BAD idea to use codes like WM_USER+n, because they may already be defined. A
the very least, use WM_APP+n, or a Regsitered Window Message.

And NEVER code something like "WM_USER+n" or "WM_APP+n" into your program! D

#define UWM_WHATEVER (WM_APP+3

and use UWM_WHATEVER for your message name.

To post to a specific window, you must have a window handle. The problem is that you can'
do this from a global variable, because if you have more than one instance of the control
all instances share the same global variable, so some control will get the message, no
necessarily the one you want to send it to

So you need some way to uniquely identify the window you want to notify. I think the us
of the global function is what is going to get you into serious trouble
jo

On Sat, 24 Apr 2004 08:41:02 -0700, "sushu" < XXXX@XXXXX.COM > wrote

>I am writing a COleControlModule application and I am trying to PostMessage(...) from a global function into the COleControl message queue.
>The problem is that if I us
>AfxGetMainWnd()->PostMessage(WM_USER+1, 0, 0); it will "go" to the container and not the control
>How can I reference the control class to do this
>Below, I've listed some pseudo code to illustrate my indent
>Please, provide any suggestions, hints, tips,... anything that can point me to a solution
>Thank you
>>////////////////////CUserApp.h/////////////////////////////////
>class CUserApp : public COleControlModul
>
>public
> BOOL InitInstance()
> int ExitInstance()
>}
>////////////////////CUserCtrl.h/////////////////////////////////
>class CUserCtrl : public COleContro
>{
>.............
>public
> CUserCtrl()
> UserDisplay m_Display; //UserDisplay class derived from CDialo
>.............
>protected
> // Message map
> //{{AFX_MSG(CUserCtrl
> //}}AFX_MS
> afx_msg LRESULT OnErrorMsg(WPARAM wParam, LPARAM lParam)
> DECLARE_MESSAGE_MAP(
>..............
>
>>////////////////////CUserCtrl.cpp/////////////////////////////////
>..............
>BEGIN_MESSAGE_MAP(CUserCtrl, COleControl
> //{{AFX_MSG_MAP(CUserCtrl
> ..............
> //}}AFX_MSG_MA
> ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties
> ON_MESSAGE((WM_USER+1), OnErrorMsg
>END_MESSAGE_MAP(
>..............
>LRESULT CUserCtrl::OnErrorMsg(WPARAM wParam, LPARAM lParam
>
> LRESULT lResult = 1L;
> AfxMessageBox("Got the error msg!")
> return lResult
>
>>////////////////////UserDisplay.h/////////////////////////////////
>class UserDisplay : public CDialo
>
>..............
>}
>>////////////////////UserDisplay.cpp/////////////////////////////////
>void UserDisplay:

mfc >> How can I PostMessage(WM_USER) to COleControl derived class from a global function?

by c3VzaHU » Mon, 26 Apr 2004 02:16:03 GMT

hank you for your reply
Unfortunately, I can't change the design not to use the global function the way it is

P.S
Ok, I agree that I should've never posted this question into the ATL newsgroup, and you say it is not an MFC question. Where should this question go

----- Alexander Nickolov wrote: ----

I'm sorry to be the first to tell you, but what you ask for i
simply impossible. You'll need to modify your design s
your global function accepts some form of argument tha
identifies your control. In fact, using a global function i
the first design problem IMO - a class initialized with
pointer to the control would be infinitely better..

P.S. True, this question has little to do with MFC, but i
you have MFC questions, it would be wise not to cross
post them into the ATL group..

--
====================================
Alexander Nickolo
Microsoft MVP [VC], MCS
email: XXXX@XXXXX.COM
MVP VC FAQ: http://www.mvps.org/vcfa
====================================
"sushu" < XXXX@XXXXX.COM > wrote in messag
news: XXXX@XXXXX.COM ..
> I am writing a COleControlModule application and I am trying t
PostMessage(...) from a global function into the COleControl message queue
> The problem is that if I us
> AfxGetMainWnd()->PostMessage(WM_USER+1, 0, 0); it will "go" to th
container and not the control
> How can I reference the control class to do this
> Below, I've listed some pseudo code to illustrate my indent
> Please, provide any suggestions, hints, tips,... anything that can poin
me to a solution
> Thank you
>> ////////////////////CUserApp.h/////////////////////////////////
> class CUserApp : public COleControlModul
>
> public
> BOOL InitInstance()
> int ExitInstance()
> }
> ////////////////////CUserCtrl.h/////////////////////////////////
> class CUserCtrl : public COleContro
>
> .............
> public
> CUserCtrl()
> UserDisplay m_Display; //UserDisplay class derived from CDialo
> .............
> protected
> // Message map
> //{{AFX_MSG(CUserCtrl
> //}}AFX_MS
> afx_msg LRESULT OnErrorMsg(WPARAM wParam, LPARAM lParam)
> DECLARE_MESSAGE_MAP(
> .............
>
>> ////////////////////CUserCtrl.cpp/////////////////////////////////
> ..............
> BEGIN_MESSAGE_MAP(CUserCtrl, COleControl
> //{{AFX_MSG_MAP(CUserCtrl
> ..............
> //}}AFX_MSG_MA
> ON_OLEVERB(AFX_IDS_VERB_PROPERTIES, OnProperties
> ON_MESSAGE((WM_USER+1), OnErrorMsg
> END_MESSAGE_MAP(
> ..............
> LRESULT CUserCtrl::OnErrorMsg(WPARAM wParam, LPARAM lParam
>
> LRESULT lResult = 1L
> AfxMessageBox("Got the error msg!")
> return lResult
>
>> ////////////////////UserDisplay.h/////////////////////////////////
> class UserDisplay : public CDialo
>
> ..............
>
>> ////////////////////UserDisplay.cpp/////////////////////////////////
> void UserDisplay::OnTest(
>
> ..............
> //call the GLOBAL