The first question is,are you really finding the right instance of
your application? I've had such ill luck using window classes that on
the whole I avoid this method, but if you use it, it is always a good
idea to use GUIGEN to create a guaranteed-unique window name, such as
"App1MainWnd-{5037D2A0-2994-11d2-8868-00AA00080A1D}"
btw, pWnd will NEVER be "TRUE", since "TRUE" is the literal constant
value 1. The correct test is
if(pWnd != NULL)
However, I think the problem is in the code where you call
AfxRegisterClass. The name you give is for a "receiver class" which is
not the class being created, and I don't see where you are
initializing any other members of the WNDCLASS structure. So there is
a chance the AfxRegisterClass would fail because of bogus data in the
WNDCLASS, and having registered the receiver class, you need to create
instances of it (by the time PreCreateWindow is called, the name of
the class that is being created is already determined). Hence your
receiver class window, if it exists at all (you don't show any
instances being created), is a child window, and FindWindow only
enumerates top-level windows. So either you have no instances of this
class, or they are all all child windows.
What I do to find a window I want is as follows:
Create a .h file containing the declaration
#define UWM_FIND_WINDOW_MSG =
"UWM_FIND_WINDOW-{5037D2A1-2994-11d2-8868-00AA00080A1D}"
Note the use of GUIDGEN to get a unique suffix string, which
guarantees that nobody, anywhere, can ever replicate your choice of
message string.
In both the client app (the one searching for the window) and server
app (the one with the window you want), add this declaration:
static WORD UWM_FIND_WINDOW =
::RegisterWindowMessage(UWM_FIND_WINDOW_MSG);
In the server app, add the following
ON_REGISTERED_MESSAGE(UWM_FIND_WINDOW, OnFindWindow)
LRESULT CMainFrame::OnFindWindow(WPARAM, LPARAM)
{
return (LRESULT)m_hWnd;
}
In the client app, add the following:
CMyClientMainFrame::OnCreate(...)
{
PostMessage(UWM_LOCATE_TARGET);
}
(This represents a bias I have about not doing too much during the
actual window creation. UWM_LOCATE_TARGET is a user-defined message
local to your client app, which my choice is to always use a
registered window message; I no longer believe in WM_USER+n for
anything because I've been done in by too many controls that use
WM_USER+n for their own purposes)
In the client app:
LRESULT CMyClientMainFrame::OnLocateTarget(WPARAM, LPARAM)
{
target = NULL;
::EnumWindows(findApplication, (LPARAM)this);
// ... if target is non-NULL, it is the handle of the
// destination
}
// Declare this function as 'static'!!!!!!
BOOL CALLBACK CRCONInterfaceDlg::findApplication(HWND hWnd, LPARAM
lParam)
{
CMyClientMainFrame* self = (CMyClientMainFrame *)lParam;
return self->finder(hWnd, UWM_QUERY_RCON, self->hClient);
}
BOOL CMyClientMainFrame::finder(HWND hWnd)
{
HWNDh = (HWND)::SendMessage(hWnd, UWM_FIND_WINDW, 0, 0);
if(h == NULL)
return TRUE; // not our window, continue search
target = hWnd;
return FALSE; // no more searching required
}
While a bit more elaborate than FindWindow, I find it more reliable. I
do not consider the window class name a decent or valid
representative of the thing I am looking for, since it encodes
"secret" information and introduces a hidden dependency. I've been
done in by this too often to want to take advantage of it any longer.
The code may seem a bit tedious, but I've written it only once, and it
migrates to all my client/server-style applications (I currently have
one with three components). A variant of this theme is that the
SendMessage includes the window handle of the sending window as wParam
or lParam, so the recipient can find the window to send stuff back to
by storing the wParam or lParam value itself.
joe
Quote:
>I have 2 MFC applikations and I would like to tell the one applikation what
>to do from the other.
>I have found out that it is possible to send data from one applikation to
>another with the WM_COPYDATA message, but I can't get the SendMessage to
>work. I have made my sendmessage function this way:
>void CMainFrame::OnSendmessage()
>{
> COPYDATASTRUCT CPDATA;
> CPDATA.dwData=9;
> CWnd *pWnd=CWnd::FindWindow("App1MainWnd",NULL);
> if (pWnd)
> pWnd->SendMessage(WM_COPYDATA,(WPARAM)m_hWnd,(LPARAM) &CPDATA); //never
>comes here ???
>}
>But pWnd is never TRUE, so FindWindow can't the applikation "App1MainWnd" I
>have registered this way:
>BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
>{
> WNDCLASS MyReceiverClass;
> MyReceiverClass.lpszClassName="App1MainWnd";
> //RegisterClass(&MyReceiverClass);
> AfxRegisterClass(&MyReceiverClass);
> return CMDIFrameWnd::PreCreateWindow(cs);
>}
>What AM I doing Wrong ???
>Thomas
Joseph M. Newcomer
http://www3.pgh.net/~newcomer