Share this page 

Use WSH-VBScript functionalitiesTag(s): WinAPI/Registry WinAPI/Registry


Here how you get the network username using the Windows Scripting Host :

OleObject wsh
Integer  li_rc

wsh = CREATE OleObject
li_rc = wsh.ConnectToNewObject( "WScript.Network" )
IF li_rc = 0 THEN
 MessageBox( "Domain", String( wsh.UserDomain ) )
END IF

By calling WSH-VBScript functions, we can achieve some useful tasks very easily.

The next example shows you how to start Notepad and send some keys to it.

OleObject wsh
Integer  li_rc

wsh = CREATE OleObject
li_rc = wsh.ConnectToNewObject( "WScript.Shell" )

wsh.Run("Notepad")
Sleep(500)
wsh.AppActivate("Untitled - Notepad")
wsh.SendKeys("hello from PB")
The declaration for the Sleep API is
[local external function declaration]
SUBROUTINE Sleep(Long lMilliSec) LIBRARY "Kernel32.dll"
NOTE: Recent version of WSH have their own Sleep function.

This one is calling the Windows Calculator

oleobject wsh
long ll_rc

wsh = CREATE oleobject
ll_rc = wsh.ConnectToNewObject("WScript.Shell")
IF ll_rc < 0 THEN
 messagebox("error","error")
END IF
wsh.Run( "calc")
Sleep (100)
wsh.AppActivate( "Calculator")
Sleep (100)
wsh.SendKeys( "1{+}")
Sleep (500)
wsh.SendKeys ("2")
Sleep (500)
wsh.SendKeys( "=")
Sleep (500)
wsh.SendKeys( "*4" )
Sleep (500)
wsh.SendKeys( "=" )
// 1+2 = 3  * 4 = 12
SendKeys can send "special key" using the following code :
BACKSPACE {BACKSPACE}, {BS}, or {BKSP}
BREAK {BREAK}
CAPS LOCK {CAPSLOCK}
DEL or DELETE {DELETE} or {DEL}
DOWN ARROW {DOWN}
END {END}
ENTER {ENTER} or ~
ESC {ESC}
HELP {HELP}
HOME {HOME}
INS or INSERT {INSERT} or {INS}
LEFT ARROW {LEFT}
NUM LOCK {NUMLOCK}
PAGE DOWN {PGDN}
PAGE UP {PGUP}
PRINT SCREEN {PRTSC}
RIGHT ARROW {RIGHT}
SCROLL LOCK {SCROLLLOCK}
TAB {TAB}
UP ARROW {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
SHIFT +
CTRL ^
ALT %

You can use some vbscript to do things which can't be done easily in powerscript like binary operations or hexadecimal conversion :

OleObject wsh
integer  li_rc, i, j , k
long m
string s

wsh = CREATE OleObject
li_rc = wsh.ConnectToNewObject( "MSScriptControl.ScriptControl" )
wsh.language = "vbscript"

i = 1
j = 2

k = integer(wsh.Eval( string(i) + " xor " + string(j)))

MessageBox( "Result" , string(i) + " xor " + string(2) + " = " + string(k))

//  or  CInt for integer
m = long(wsh.Eval( "CLng(~"&HFF0F~")"))
MessageBox( "Result" , " HEX &HFF0F -> DEC " + string(m))

s = wsh.Eval( "hex(255)")
MessageBox( "Result" , " DEC 255 -> HEX " + s)

s = wsh.Eval( "hex(65000)")
MessageBox( "Result" , " DEC 65000 -> HEX " + s)

Call the Windows RUN dialog :

OleObject wsh

wsh = CREATE OleObject
wsh.ConnectToNewObject( "Shell.Application" )

wsh.filerun

You can even create some VBScript code on the fly with PB and execute it.

OleObject wsh
Integer  li_rc, i, j , k

wsh = CREATE OleObject
li_rc = wsh.ConnectToNewObject( "MSScriptControl.ScriptControl" )
wsh.language = "vbscript"
wsh.addcode("function retfnc(s)  retfnc=s  end function")
wsh.executestatement ('msgbox retfnc("true")')

See this related howto