How to call API Windows functions

Through External and External$ WinTask functions, you can call nearly any function from Windows API, which uses simple data structure. The script below gives a full example. Many ready to use scripts can be found too in \wintask\Advanced_Script_Examples\Windows_API_Functions_Call.

You can find the corresponding script file under Script_Examples, sub-directory of scripts.

 

'This script opens the notepad.exe, finds the Format menu and extracts all the items.

'Finally the items are displayed.

 

'Define the memory address for the text of the item

Dim itemText as unsigned

itemText = Allocate(255) 'allocate the memory for the item text

 

'Maximum length of the item text

nMaxLen = 255

 

'MF_BYPOSITION api option

MF_BYPOSITION = 1024

 

'Start a notepad

Shell("notepad.exe", 1)

 

'Get the window handle of notepad

hWnd = GetWindowHandle("NOTEPAD.EXE|Notepad|Untitled - Notepad")

 

'Get the hMenu handle for the main menu of the notepad window

hMainMenu = External("User32.dll", "GetMenu", hWnd)

 

'Get the number of items in the main menu

nMainItemCount = External("User32.dll", "GetMenuItemCount", hMainMenu)

'nMainItemCount should be 4 because there are four items: File, Edit, Format and Help

 

'Get the Format submenu

hFormatMenu = External("User32.dll", "GetSubMenu", hMainMenu, 2)

'2 is the index of the Format sub menu. the counting starts from 0

 

'Get the number of items in the Format menu

nFormatItemCount = External("User32.dll", "GetMenuItemCount", hFormatMenu)

'nFormatItemCount should be 2 because there are two items: WordWrap and Font

 

'Extract each item text

i = 0

While i < nFormatItemCount

'Get the text of the current item

External("User32.dll", "GetMenuStringA", hFormatMenu, i, itemText, nMaxLen - 1, MF_BYPOSITION)

itemText$ = PeekString$(itemText)

 

'Add the text to the final result

menuText$ = menuText$ + itemText$ + Chr$(13)

 

'Go to the next item

i = i + 1

Wend

 

'Close the notepad window

CloseWindow("NOTEPAD.EXE|Notepad|Untitled - Notepad", 1)

 

'Display the menu text

MsgBox(menuText$)