-
FindWindow Weird Error
Hi im getting a error:
PHP Code:
error C2440: '=' : cannot convert from 'class CWnd *' to 'struct HWND__ *'
Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
Error executing cl.exe.
My Code is:
PHP Code:
HWND i;
HWND x;
i = FindWindow("Program",0);
-
You're mixing MFC and API.
Use ::FindWindow(...).
-
Since you're already using MFC:
Code:
CWnd *pWnd;
pWnd = FindWindow("Program", 0);
Don't forget that the CWnd pointer is not valid any longer than the function you're in. After that it might get deleted at any time.
-
ok i got it using parksies' way. Now can you tell me how do i convert the HWND to Char ?
-
Why would you want to do that? (maybe you can just cast it)
-
How do i cast it ? What is casting it?
-
First please explain me why you want to convert it to a char?
Code:
MessageBox(myhWnd,(LPSTR)myhWnd, "Casting!", MB_OK);
that's casting what's happening over there at the bold part.
I know this example won't work, but it's just to illustrate how casting works. It kinda tells the compiler that you want it to send the HWND as a LPSTR to the MessageBox function.
damn my explaining is totally blurry today, hope you understand what I mean.
-
Mine is more MFC-like...
Whatever. You can't convert HWND (32-bit) to char (8-bit) without loss of most the data you want. If you however (as I assume) want to convert the HWND to a string of some kind then you have three options.
The first I'd recommend the least:
Code:
// To decimal:
char decimalbuf[12];
sprintf(decimalbuf, "%i", hwnd);
// To hex:
char hexbuf[12];
sprintf(hexbuf, "0x%8x", hwnd);
The second is the most MFC-like as it uses the MFC string class CString:
Code:
// To decimal:
CString strDecimal;
strDecimal.Format("%i", hwnd);
// To hex:
CString strHex;
strHex.Format("0x%8x", hwnd);
The third is under normal circumstances (that is, if you're not using MFC) the best and uses the STL classes. Requires <sstream>, <iomanip> and <string> to be included.
Code:
ostringstream ss;
// To decimal:
ss << hwnd;
string strDecimal = ss.str();
// Reset ss:
ss.str("");
// To Hex:
ss << setbase(16) << hwnd;
string strHex = ss.str();
-
Jop, be careful, he might really try what you're posting.
Casting only works to some extent. The above is likely to result in an access violation. If not it would only output garbage.
-
Hehe that wasn't my intention, and while I was posting it I was sure that I wasn't explaining things clearly, so I thought I would illustrate it, which made it even worse :) just ignore my post JasonLpz!