GetWindowRect in VB.NET 2005?

Megalith

Well-known member
Joined
Aug 21, 2006
Messages
66
Programming Experience
10+
I have been working on an application in VB.NET 2005, essentially the routine i'm currently developing finds the handle of a window then it is meant to locate the desktop location and size of this window. I decided that GetWindowRect would probably be the best way to achieve my desired result however i get the following exception:-

System.AccessViolationException was unhandled
Message="Attempted to read or write protected memory. This is often an indication that other memory is corrupt."

I have declared the API function like this

VB.NET:
    Declare Function GetWindowRect Lib "user32" _
            (ByVal hwnd As Integer, _
            ByVal lpRect As Rectangle) _
            As Integer
The Subroutine that causes the violation is
VB.NET:
    Private Sub DesktopSize(hWndParent)
        ' first define a rectangle
        Dim RECT As Rectangle
        'Now a point for the mouse location
        Dim Pnt As Point
        ' Get the coordinates for the box on the screen
        [COLOR=Red]GetWindowRect(hWndParent, RECT) [/COLOR]
        ' set Pnt to equal just inside bottom left corner
        Pnt.X = RECT.Left + 8
        Pnt.Y = RECT.Bottom - 24
        ' set the mouse to this location
        'SetCursorPos(Pnt.X, Pnt.Y)
End Sub
the line in red is the one that throws the exception. The routine to find the handle is correct and the correct handle is passed to the Sub. I think it is to do with the Rectangle but i'm at a loss i have tried a few things but always get the same error. I'm overlooking something obvious i feel, can anybody point me in the right direction?:eek:
 
It will do, you will need to define a RECT structure not use the rectangle one provided by the framework..

VB.NET:
<StructLayout(LayoutKind.Sequential)> _ 
Private Structure RECT
 Public Left As Int32
 Public Top As Int32
 Public Right As Int32
 Public Bottom As Int32
End Structure
 
Your declaration of GetWindowRect is wrong, correct is:
VB.NET:
Declare Function GetWindowRect Lib "user32.dll" ( _ 
ByVal hwnd As Int32, _ 
ByRef lpRect As RECT) As Int32
 
<StructLayout(LayoutKind.Sequential)> _ 
Structure RECT
Public Left As Int32
Public Top As Int32
Public Right As Int32
Public Bottom As Int32
End Structure
Get ApiViewer 2004 and configure it to use VB.Net syntax.

EDIT: vis781 and I agreed on this at the same time almost, anyways ApiViewer should provide you help with those 'protected memory' declaration problems.
 
Thx guys yet again you saved the day :)

I did wonder about this, i got my initial code from API-Guide and had to change it (VB6 - VB.NET) i take it that the Rectangle is infact a graphics object then?
 
Nope, Rectangle in the framework is a structure but it is the order that counts when it comes to Win32 API. Also the rectangle structure is also an object with lots of other methods. Using it in a Win32 API call will unbalance the stack and couse all kinds of peculier errors.
 
Ah :) makes sense i did see a lot of properties while i was using the Rectangle object. thanks again vis and John ive got the thing working now lol so much to learn with .NET took me something like 6 hours trying to get that to work before i gave up and came for help :) you guys found it right away. I feel small right now lol
 
Back
Top