Getting a window's coordinates
I am trying to use GetWindowRect or GetWindowPlacement to find the coordinates of a window that I have the handle for, but every time I try to use GetWindowRect, I get this error:
Code:
An unhandled exception of type 'System.NullReferenceException' occurred in testapplication.exe
Additional information: Object reference not set to an instance of an object.
Here is the code I am using:
VB Code:
Dim r As New RECT
GetWindowRect(hwnd, r)
MsgBox(r.Left)
hwnd is the window handle, the API calls are all declared public in a module. Here is my converted RECT structure:
VB Code:
Public Structure RECT
Dim Left As Integer
Dim Top As Integer
Dim Right As Integer
Dim Bottom As Integer
End Structure
Anyone see why it would give me a null exception error? I know for sure that the window handle is valid because I can use SetForegroundWindow on the same variable and it brings the application to the front of the screen.
Thanks for any help/ideas you can give. :)
Re: Getting a window's coordinates
Nevermind. To make this work, change the ByVal to ByRef in the API declaration:
VB Code:
Public Declare Function GetWindowRect Lib "user32" Alias "GetWindowRect" (ByVal hwnd As Integer, ByRef lpRect As RECT) As Integer
Re: Getting a window's coordinates
Stuff that goes in = ByVal
Stuff that comes out = ByRef.
:)