HWND / CWnd* - compile errors
-
Just for learning purposes, I'm trying to implement the code Mark Petrik Sosa put in a message at 6:47 14 Apr '05 in a reply to 'Send Messages to Other Windows' (4:34 14 Apr '05).
HWND hNotepad = FindWindowEx(0, 0, "Notepad", 0); if(hNotepad) SendMessage(hNotepad, WM_SETTEXT, 0, (LPARAM) "Hah, I found you!"); else MessageBox(0, "Couldn't find notepad.", "Darn", MB_OK); When I compile, I get: error C2440: 'initializing' : cannot convert from 'CWnd*' to 'HWND' error C2660: 'CWnd::SendMessageA': function does not take 4 arguments If I change to CWnd* hNotepad = ... and then SendMessage(hNotePad->m_hWnd, ... I still get the C2660 error. I also get 'CWnd::MessageBoxA': function does not take 4 arguments
I can see that SendMessage has 2 prototypes - one takes 3 arguments and looks like it's for the cases where you do not need to specify a handle. Would appreciate any direction on how to fix the code and/or explanations to help me understand the mechanisms involved here. -
Just for learning purposes, I'm trying to implement the code Mark Petrik Sosa put in a message at 6:47 14 Apr '05 in a reply to 'Send Messages to Other Windows' (4:34 14 Apr '05).
HWND hNotepad = FindWindowEx(0, 0, "Notepad", 0); if(hNotepad) SendMessage(hNotepad, WM_SETTEXT, 0, (LPARAM) "Hah, I found you!"); else MessageBox(0, "Couldn't find notepad.", "Darn", MB_OK); When I compile, I get: error C2440: 'initializing' : cannot convert from 'CWnd*' to 'HWND' error C2660: 'CWnd::SendMessageA': function does not take 4 arguments If I change to CWnd* hNotepad = ... and then SendMessage(hNotePad->m_hWnd, ... I still get the C2660 error. I also get 'CWnd::MessageBoxA': function does not take 4 arguments
I can see that SendMessage has 2 prototypes - one takes 3 arguments and looks like it's for the cases where you do not need to specify a handle. Would appreciate any direction on how to fix the code and/or explanations to help me understand the mechanisms involved here.::SendMessage(hNotepad, WM_SETTEXT, 0, (LPARAM) "Hah, I found you!"); and ::MessageBox(0, "Couldn't find notepad.", "Darn", MB_OK); or MessageBox("Couldn't find notepad.", "Darn", MB_OK);
-
::SendMessage(hNotepad, WM_SETTEXT, 0, (LPARAM) "Hah, I found you!"); and ::MessageBox(0, "Couldn't find notepad.", "Darn", MB_OK); or MessageBox("Couldn't find notepad.", "Darn", MB_OK);
Thanks. It now compiles and executes properly. If you have another minute though, I still get the error 'initializing': cannot convert from CWnd* to HWND unless I declare hNotepad as CWnd*. Then when I do that I have to use hNotepad->m_hWnd, which makes sense. Is there some include file I need such that I can declare hNotepad as HWND and then use it as you and Mark used it (i.e., without the ->m_hWnd)?
-
Thanks. It now compiles and executes properly. If you have another minute though, I still get the error 'initializing': cannot convert from CWnd* to HWND unless I declare hNotepad as CWnd*. Then when I do that I have to use hNotepad->m_hWnd, which makes sense. Is there some include file I need such that I can declare hNotepad as HWND and then use it as you and Mark used it (i.e., without the ->m_hWnd)?
If you dereference your pointer ( *hNotepad ), then operator HWND should come to the rescue. Christian Graus - Microsoft MVP - C++
-
If you dereference your pointer ( *hNotepad ), then operator HWND should come to the rescue. Christian Graus - Microsoft MVP - C++
I haven't found the secret yet. If I still declare with CWnd
CWnd* hNotepad = FindWindowEx(...
then I can use::SendMessage(*hNotePad, ...
But if I declare withHWND hNotepad = FindWindowEx(...
orHWND *hNotepad = FindWindowEx(...
I still get the error message 'initializing' cannot convert from CWnd to HWND Is it maybe because the 2003 compiler is more strict than compilers in the past? I don't have much experience/history with Visual C++.