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)
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 *
Examples:
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.Quit
Else
wscript.echo "note pad window found"
Excel.ExecuteExcel4Macro "CALL(""user32"",""ShowWindow"",""JJJ""," & hwnd & ","& SW_
RESTORE
&")"
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 & ")")
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 *
Examples:
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.Quit
Else
wscript.echo "note pad window found"
Excel.ExecuteExcel4Macro "CALL(""user32"",""ShowWindow"",""JJJ""," & hwnd & ","& SW_
RESTORE
&")"
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 & ")")
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:
ReplyDeleteWhere do you look to determine how many type strings are needed and of which type? For things like FindWindowA, I attempted to look here (https://www.pinvoke.net/default.aspx/user32.FindWindowA) 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.
https://officevba.info/vbscript-windowsapi/
DeleteEverything is very open with a precise description of the issues. It was really informative. Your site is useful. Thanks for sharing!
ReplyDeleteTechnology
Excellent blog thanks for sharing the valuable information..it becomes easy to read and easily understand the information.
ReplyDeleteUseful article which was very helpful. also interesting and contains good information.
to know about python training course , use the below link.
Python Training in chennai
Python Course in chennai
Excellent blog thanks for sharing the valuable information..it becomes easy to read and easily understand the information.
ReplyDeleteUseful article which was very helpful. also interesting and contains good information.
Java Training in Chennai
Java Course in Chennai
I enjoyed reading the article, really explains everything in detail, the article is very interesting and effective.
ReplyDeleteMicrosoft Azure Course in Marathahalli
Nicely written and has great info. Thank you enough for sharing.
ReplyDeletesap fico training in btm layout
Softlogic is No:1 Software Training Institute in Chennai offers best IT Training Courses for Java, Android, PHP, .NET, DevOps, Oracle with 100% placements.
ReplyDeleteData Science Training in Chennai
python data science course in Chennai