Friday, September 25, 2015

VbScript to call windows API Functions (user32 dll)

We can make use excel object and ExecuteExcel4Macro method to call User32 DLL methods.

Below is the syntax:

CALL(dll_name, function_name, type_string, func_arguments1, ..., func_argumentsN)

dll_name - the name of the DLL, which contains the desired function. This name must contain the full path if the DLL is not located in your Windows, System folder, or the folder specified in the environment variable PATH.
function_name - name of the function.
type_string - text string that identifies the data type of the return value and the data types of all parameters. The first character type_string defines the return value.
func_arguments1, ..., func_argumentsN - function parameters. Their types must comply type_string. It may be transmitted up to 27 parameters.
Data types for type_string:
B - 8-byte floating-point number (IEEE), Transferred by Value, C type double.
C - Zero (null) terminated string (max. Length = 255 characters), Transferred by Reference, C type char *
F - Zero (null) terminated string (max. Length = 255 characters), Transferred by Reference (modify in place), C type char *
J - 4 bytes wide signed integer, Transferred by Value, C type long int
P - Excel's OPER data structure, Transferred by Reference, C type OPER *
R - Excel's XLOPER data structure, Transferred by Reference, C type XLOPER *

Dim hwnd

CreateObject("WScript.Shell").Run "notepad", 1, False
With CreateObject("Excel.Application")
 hwnd = .ExecuteExcel4Macro("CALL(""user32"", ""FindWindowA"", ""JCJ"", ""Notepad"", 0)")
 If hwnd = 0 Then
  wscript.echo "note pad window not found"
  wscript.echo "note pad window found"

  Excel.ExecuteExcel4Macro "CALL(""user32"",""ShowWindow"",""JJJ""," & hwnd & ","& SW_

 End If
end with

some other uses:
  GetPIDByHWND = excel.ExecuteExcel4Macro("CALL(""user32"", ""GetWindowThreadProcessId"", ""2JN"", " & CStr(hwnd) & ", 0)")
  hwnd = excel.ExecuteExcel4Macro("CALL(""user32"", ""GetDesktopWindow"", ""J"")")
  hwnd = excel.ExecuteExcel4Macro("CALL(""user32"", ""GetWindow"", ""JJJ"", " & CStr(hwnd) & ", 5)")
' Puts the cursor at the specified X and Y corrdinates
  Excel.ExecuteExcel4Macro("CALL(""user32"",""SetCursorPos"",""JJJ""," & x & "," & y & ")")


  1. Good day to you. First, thank you for this post, it has greatly aided me in working toward my coding goals, and been a good source of examples and knowledge. I would like to ask questions:

    Where do you look to determine how many type strings are needed and of which type? For things like FindWindowA, I attempted to look here ( and only see two strings inside of its function, but I see that you have two J's and a C. How did you determine 3 strings were needed? My goal with this is to figure out how to Call User32.dll's SendMessage function.

    I'm also having trouble with some of your examples and was hoping you could point me in the right direction. Your "FindWindowA" works moderately well for me, will find the hwnd of Notepad if I have it up. But if I open Calculator and replace ""Notepad"" with ""Calculator"", it will not provide a hwnd. Finally, I cannot get your ShowWindow portion to work, though I'm uncertain if it has to do with Windows10.

    Again, thank you for your knowledge. May life treat you well.


  2. Everything is very open with a precise description of the issues. It was really informative. Your site is useful. Thanks for sharing!

  3. Excellent blog thanks for sharing the valuable becomes easy to read and easily understand the information.
    Useful article which was very helpful. also interesting and contains good information.

    Java Training in Chennai

    Java Course in Chennai

  4. This comment has been removed by a blog administrator.