Calling win32 API from vbscript

Sometimes you need to call win32 API that requires a pointer to struct, here comes the need to XNHost.Struct.

For example to create a win32 Point Struct

Dim Point
Set Point = XNHost.Struct
Point.add  "x", "i32"
Point.add  "y", "i32"
as you can see we first create an empty Struct. then we start adding fields using the Add method
The first parameter is the field name
The second parameter is the field type which could be
"ui32" for unsigned 32 bit
"ui16" for unsigned 16 bit
"ui8" for unsigned 8 bit
"i32" for signed 32 bit
"i16" for signed 16 bit
"i8" for signed 8 bit
"t" for ascii string
"w" for unicode string
for "t" & "w" fileds there exsits another parameter for the size
Below are some examples that show the using of Win32 Struct

Dim User32
Set User32 = XNHost.LoadDll( "user32.dll" )

Dim Point
Set Point = XNHost.Struct
Point.Add "x" , "i32"
Point.Add "y" , "i32"

User32.GetCursorPos Point

MsgBox "Mouse x Position = " & Point.x 
MsgBox "Mouse y Position = " & Point.y
This example gets the cursor position of the mouse using GetCursorPos API then display it.

Dim User32
Set User32 = XNHost.LoadDll( "user32.dll" )

Dim hWnd
hWnd = User32.GetForegroundWindow

Dim Caption
Set Caption = XNHost.Struct
Caption.Add "buffer" , "t", 1024

User32.GetWindowText hWnd, Caption, 1023

MsgBox Caption.buffer,0, "Foreground Window Caption"
In the above sample we see the using of ascii string and GetWindowText API to get the caption of the foreground window