Obtaining Window Location

Hi,

I have the code below I am trying to use to get a Hwnd window location.

When I compile and link, all is well. When i run I get th e error :

An unhandled exception of type 'System.AccessViolationException' occurred in AutoInspectorX.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

I'm passing the callback handler to a callback handler in a class, so it looks like there are 2 callback handlers! Is this an OK way to do it?

Can anyone explain to me why this fails please as it looks fine to me! :confused:

Code :

// Called from my main function...
EnumWindows(SearchWindowsHandle,(LPARAM)this);

// Callback handler to search HWND's
BOOL CALLBACK SearchWindowsHandle(HWND hWnd, LPARAM lparam)
{
AutoInspectorX *pMe = (AutoInspectorX *)lparam;
return pMe->SearchWindowsCallbackHandler(hWnd); // Call class member for callback
}

bool AutoInspectorX::SearchWindowsCallbackHandler(HWND hWnd)
{
char buff[500];
string temp = "";
bool written_flag = 0;

if (!hWnd)
{ return TRUE; } // Not a window
if (!::IsWindowVisible(hWnd))
{ return TRUE; } // Not a visible window

GetWindowText(hWnd,buff,sizeof(buff)*sizeof(char));

if (strstr(buff,""))
{
if (searchSize< WIND_SRCH_SIZE)
{
string temp(buff);
storeStrings[index] = temp;
index++;
}
myWnd = hWnd; // Add the last Hwnd found in category
LPRECT recta;
GetWindowRect(myWnd , recta);
}
return TRUE;
}
[1753 byte] By [BigWinston] at [2007-11-19 19:41:55]
# 1 Re: Obtaining Window Location
Can anyone explain to me why this fails please as it looks fine to me! :confused:

LPRECT recta;
GetWindowRect(myWnd , recta);

No, this is NOT fine. You are passing uninitialized pointer (to non-existant rect) to GetWindowRect() function!
Here is the correct way:
RECT recta;
GetWindowRect(myWnd , &recta);
VladimirF at 2007-11-10 23:43:05 >
# 2 Re: Obtaining Window Location
Vlad, thanks for the help... again! :)

Tried and works. Thanks again.
BigWinston at 2007-11-10 23:44:05 >