From: FHDB on
I have placed thread control logic for worker threads in theApp, declared as
CdualApp : public CWinApp. But my additions to the message map result in the
error: error C2440: 'static_cast' : cannot convert from 'LRESULT (__cdecl
*)(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
if declared globally, or similarly if made part of class CdualApp.

The problem appears to be that the message map wants message handlers that
are part of a CWnd derived class. Is there a workaround for this, or do I
need to move all the thread control logic into such a class?
From: Giovanni Dicanio on
"FHDB" <FHDB(a)discussions.microsoft.com> ha scritto nel messaggio
news:9E8C74B2-01BE-4658-9C06-609A9608C50C(a)microsoft.com...
> I have placed thread control logic for worker threads in theApp, declared
> as
> CdualApp : public CWinApp. But my additions to the message map result in
> the
> error: error C2440: 'static_cast' : cannot convert from 'LRESULT (__cdecl
> *)(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
> if declared globally, or similarly if made part of class CdualApp.

It would be helpful to read actual code.

However, in general, when you use AfxBeginThread in MFC context to create a
worker thread, you pass as first parameter the address of a function which
can be a *static* member function of a class.
And you can specify as second parameter an LPVOID; you can use this second
parameter to pass the 'this' value associated to the particular instance of
a C++ class.
In this way, the static member function can cast the LPVOID back to 'this'
pointer, and can call a non-static member function.

Joe wrote an interesting essay on worker threads, I think that you will find
it very useful:

http://www.flounder.com/workerthreads.htm

HTH,
Giovanni


From: Ajay Kalra on
On Nov 4, 12:30 am, FHDB <F...(a)discussions.microsoft.com> wrote:
> I have placed thread control logic for worker threads in theApp, declared as
> CdualApp : public CWinApp. But my additions to the message map result in the
> error:  error C2440: 'static_cast' : cannot convert from 'LRESULT (__cdecl
> *)(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
> if declared globally, or similarly if made part of class CdualApp.
>
> The problem appears to be that the message map wants message handlers that
> are part of a CWnd derived class.  Is there a workaround for this, or do I
> need to move all the thread control logic into such a class?

CWinApp is not a window, so you cant really send a message to it.
Since its derived from CWinThread, you can use PostThreadMessage. For
message map, you will need ON_THREAD_MESSAGE.

Also, this object participates in message routing(derived from
CCmdTarget), so it can receive WM_COMMAND types messages as well.

--
Ajay
From: Joseph M. Newcomer on
Note, however, that if the CWinApp is the CWinApp for a GUI-based app that has windows,
you cannot PostThreadMessage to it!

The error message says explicitly that the type of the function is implemented as

LRESULT function(WPARAM, LPARAM)

when it must be implemented as

LRESULT classname::function(WPARAM, LPARAM)

but more seriously, as pointed out here, the classname can *only* be a CWnd class, so it
can't be in a CWinApp-derived class. ON_MESSAGE and ON_REGISTERED_MESSAGE entries can
only be used in CWnd-derived classes.
joe

On Wed, 4 Nov 2009 04:44:31 -0800 (PST), Ajay Kalra <ajaykalra(a)yahoo.com> wrote:

>On Nov 4, 12:30�am, FHDB <F...(a)discussions.microsoft.com> wrote:
>> I have placed thread control logic for worker threads in theApp, declared as
>> CdualApp : public CWinApp. But my additions to the message map result in the
>> error: �error C2440: 'static_cast' : cannot convert from 'LRESULT (__cdecl
>> *)(WPARAM,LPARAM)' to 'LRESULT (__thiscall CWnd::* )(WPARAM,LPARAM)'
>> if declared globally, or similarly if made part of class CdualApp.
>>
>> The problem appears to be that the message map wants message handlers that
>> are part of a CWnd derived class. �Is there a workaround for this, or do I
>> need to move all the thread control logic into such a class?
>
>CWinApp is not a window, so you cant really send a message to it.
>Since its derived from CWinThread, you can use PostThreadMessage. For
>message map, you will need ON_THREAD_MESSAGE.
>
>Also, this object participates in message routing(derived from
>CCmdTarget), so it can receive WM_COMMAND types messages as well.
Joseph M. Newcomer [MVP]
email: newcomer(a)flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
From: Ajay Kalra on
On Nov 4, 8:35 am, Joseph M. Newcomer <newco...(a)flounder.com> wrote:
> Note, however, that if the CWinApp is the CWinApp for a GUI-based app that has windows,
> you cannot PostThreadMessage to it!  
>

Thats very odd. We did it for a GUI without any issue and it has been
in the product for a very long time.

--
Ajay