External

System function.

The External function calls an external DLL. Not available in WinTask Lite.

Syntax

var=External(<DLL_name$>,<function_name$>[, <param1>[,<param2>, ...]])

Parameter

<DLL_name$>, string, name of the Windows DLL.

<function_name$>, string, name of the DLL function.

<param1>, <param2>: optional parameters, type integer or unsigned or string. If <paramx> is an output string, its maximum length is 1023 characters. If you need an output string longer, use External$.

Return value

var, return value, either a 32-bit integer or an address type UNSIGNED.

See also

How to call API Windows functions

External$

In WinTask\Advanced_script_examples\Windows_API_Functions_Call folder, some scripts using External are listed.

A WinTask customer has written many scripts using Windows API functions, you can view them at http://www.sqablogs.com/jstrazzere/WinTask

Example code

This example shows the use of the GetWindowsDirectory function to get the name of the directory where Windows is installed.

wdir$=" "
a=External("kernel32","GetWindowsDirectoryA",wdir$,145)
msgbox(wdir$)

This more complex example gives the name of the computer

Dim compName as unsigned 'Space for the computer name string

Dim bufferLen as unsigned 'Space for the length of the string

 

'Initialization

compName = Allocate(255)

bufferLen = Allocate(4)

PokeInteger(bufferLen, 255, 4) 'Initialize the length of the buffer

 

 

' According to MSDN the second parameter of GetComputerName is a pointer to a dword, not a dword.

'BOOL GetComputerName( LPTSTR lpBuffer, // computer name

' LPDWORD lpnSize // size of name buffer);

 

ret=External("kernel32.dll", "GetComputerNameA", compName, bufferLen)

name$=PeekString$(compName)

MsgBox(name$)

 

This more complex example gives the mouse position on the screen

'It calls the Windows API GetCursorPos

'External, structure, allocate, peekinteger, pokeinteger are used.

 

'When you call Windows API functions, data is often written in

'structures (memory areas). A structure can contain various data (integer,

'string, etc ...). The only way to access this data is to use the WinTask

'Peek... and Poke.. functions

 

'The Windows API GetCursorPos function gives the mouse cursor position.

'It requires as a parameter an address of a "point" structure.

'The structure is composed of 2 integers of 4 bytes each (LONG)

'with consecutive addresses. The first address contains the X coordinate.

'The second address contains the Y coordinate.

 

'Define memory address

dim addx as unsigned

dim addy as unsigned

 

 

'allocate memory starting at addx for the coordinates

'addx will contain the X coordinate

addx=allocate(8)

 

'addy will contain the Y coordinate

addy=addx+4

 

'Call the function in a loop

repeat

'address for structure addx is a parameter for GetCursorPos

a=External("user32.dll","GetCursorPos",addx)

if a <>0 then

'Read 4 bytes from memory at address addx (x position)

pos_x=PeekInteger(addx,4)

'Read 4 bytes from memory at address addy (y position)

pos_y=PeekInteger(addy,4)

'Display result

mes$="Cursor X is at : "+str$(pos_x)+"\n\Cursor Y is at : "+str$(pos_y)

msgframe(mes$,1)

pause 1

endif

until i=100

'the function loops for 100 seconds