473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,386 developers and data experts.

Windows Script Host Object library

FishVal
2,653 Expert 2GB
Windows Script Host Object library.

Full name: Windows Script Host Object Model
LibName: IWshRuntimeScripting
Location: ...\WINDOWS\system32\wshom.ocx

The present tip is closely related to the previous stuff written by ADezii concerning Scripting Runtime library. Both libraries share the same functionality as for file functions but the library in the topic provides additional possibilities which may be advantageously used.

Here is a brief overview:
  • A whole bunch of file functions (the same as in Scripting Runtime library)
  • Network and printer functions.
  • Creating shortcuts.
  • Shell functions including: advanced application launch, event log and registry functions etc.

The present tip is devoted to two most widely used aspects: creating shortcuts and application launch. Those interested in more comprehensive information should take a time to go through MSDN: Windows Script Host.


Creating shortcuts.

The code below, quite self-explaining I guess, creates URL shortcut in "Favorites" and file shortcuts on Desktop and in Start Menu. It uses IWshRuntimeLibrary.WshShell.SpecialFolders collection to obtain paths to a relevant directories. The full list of the names of SpecialFolders collection one could be found in MSDN: SpecialFolders object.
Expand|Select|Wrap|Line Numbers
  1. Public Sub CreateShortcuts()
  2.  
  3.     'variables declaration
  4.     Dim objWshShell As New IWshRuntimeLibrary.WshShell
  5.     Dim objWshShortcut As IWshRuntimeLibrary.WshShortcut
  6.     Dim objWshURLShortcut As IWshRuntimeLibrary.WshURLShortcut
  7.     Dim strTargetFolder As String
  8.  
  9.     'create shortcut in "Favorites"
  10.     With objWshShell
  11.         'get path to "Favorites"
  12.         strTargetFolder = .SpecialFolders("Favorites")
  13.         'create an URL shortcut in "Favorites"
  14.         Set objWshURLShortcut = .CreateShortcut(strTargetFolder & _
  15.             "\TheScripts.url")
  16.     End With
  17.     'set the URL shortcut taget and save it
  18.     With objWshURLShortcut
  19.         .TargetPath = "http://www.thescripts.com"
  20.         .Save
  21.     End With
  22.  
  23.     'create shortcut on Desktop available for all users
  24.     With objWshShell
  25.         'get path to "All Users Desktop"
  26.         strTargetFolder = .SpecialFolders("AllUsersDeskTop")
  27.         'create a shortcut in "All Users Desktop"
  28.         Set objWshShortcut = .CreateShortcut(strTargetFolder & _
  29.             "\MyShortcut.lnk")
  30.     End With
  31.     With objWshShortcut
  32.         'set the shortcut target, description and save it
  33.         .TargetPath = "C:\SomeFile.txt"
  34.         .Description = "Link to my file"
  35.         .Save
  36.     End With
  37.  
  38.     'create the same shortcut in Start Menu available for all users
  39.     With objWshShell
  40.         strTargetFolder = .SpecialFolders("AllUsersStartMenu")
  41.         Set objWshShortcut = .CreateShortcut(strTargetFolder & _
  42.             "\MyShortcut.lnk")
  43.     End With
  44.     With objWshShortcut
  45.         .TargetPath = "C:\SomeFile.txt"
  46.         .Description = "Link to my file"
  47.         .Save
  48.     End With
  49.  
  50.     'destroy object variables
  51.     Set objWshURLShortcut = Nothing
  52.     Set objWshShortcut = Nothing
  53.     Set objWshShell = Nothing
  54.  
  55. End Sub
  56.  
Launch an application.

WSHOM library provides a better then VBA Shell function method to run an application which doesn't support automation.
In the following example Calc.exe is launched when a toggle button is pressed and terminated when it is released or form is closed. Additionally Form_Timer event handler monitors Calc.exe application status and whenever it is closed the toggle button is being released.
Expand|Select|Wrap|Line Numbers
  1. 'form public variable to store WshExec object referring
  2. 'to launched Calc.exe application
  3. Dim objCalc As IWshRuntimeLibrary.WshExec
  4.  
  5. Private Sub Form_Close()
  6.  
  7.     'terminate Calc.exe when form is being closed
  8.     If Not objCalc Is Nothing Then
  9.         objCalc.Terminate
  10.         Set objCalc = Nothing
  11.     End If
  12.  
  13. End Sub
  14.  
  15. 'form timer event handler to monitor Calc.exe application status
  16. Private Sub Form_Timer()
  17.  
  18.     'if public WshExec variable does not exist then leave procedure
  19.     If objCalc Is Nothing Then Exit Sub
  20.     'set toggle button state according to Calc.exe status
  21.     With Me
  22.         If objCalc.Status = WshRunning Then
  23.             .tglCalc = True
  24.         Else
  25.             .tglCalc = False
  26.         End If
  27.     End With
  28.  
  29. End Sub
  30.  
  31. Private Sub tglCalc_AfterUpdate()
  32.  
  33.     Dim objWshShell As New IWshRuntimeLibrary.wshShell
  34.  
  35.     If Me.tglCalc Then
  36.         'if toggle button is pressed then run Calc.exe
  37.         'and assign returned WshExec object to form public variable
  38.         Set objCalc = objWshShell.Exec("calc.exe")
  39.     Else
  40.         'if toggle button is released then terminate Calc.exe
  41.         objCalc.Terminate
  42.     End If
  43.  
  44.     Set objWshShell = Nothing
  45.  
  46. End Sub
  47.  
Mar 3 '08 #1
3 18072
dima69
181 Expert 100+
My concern is (as usual :-)) how robust this solution is. Personally, I don't like using OCX, as they often make trouble with installation because of the version compatibility.
Can this WshShell object be used via late binding ? (i.e., by CreateObject) ?
Mar 4 '08 #2
FishVal
2,653 Expert 2GB
There are no 100% robust solutions at all. ;)
And, you are right, WSHO objects could not be instantiated via CreateObject.
Mar 4 '08 #3
ADezii
8,834 Expert 8TB
Nice job, FishVal! Any chance of a sequel demonstrating the Printing and Networking functionality of the Windows Script Host Object Library?
Mar 8 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: raghavendra | last post by:
Hi, How to run automatically windows service by using setup deployment insatllation script using visual studio 2003.? What i did is :-- 1. created a windows service & tested the same. 2....
7
by: IntraRELY | last post by:
I am using the following command to launch and .NET VB Executable, which is a simple Windows Form (printCheckClient.exe). I am using a HyperLink Control and works perfect....
2
by: Miky | last post by:
Hi, I have the following problem: I have a .CFM web page and I have to populate, from VB, two textboxes on it. I was thinking using sendkeys (even if I don't like it too much) but when I...
3
by: Chris Paul | last post by:
I'm having trouble with PHP & PostgreSQL/OpenLDAP/Apache on Windows. I've set this up countless times on BSD (piece of cake) but I'm trying to do this on Windows now so that my developer can work...
3
by: Angus Comber | last post by:
Hello Can javascript call/reference COM object. VBScript can do this (as it is from Microsoft). Can javascript? If so how? Angus
4
by: korund | last post by:
Is there any difference to read & write to windows Registry with VBScript or JavaScript? Both scripting languages have similar syntax. What is preferable use for this? thanks.
69
by: Peter Olcott | last post by:
Does JavaScript represent its controls internally as Microsoft Windows controls, or does it build them from scratch like Java?
7
by: jeddiki | last post by:
Hi, As I am in Turkey at present, I can not see vidoes on youtube. So I have tried a few proxies but keep finding them slow or not working. So I have installed myphpProxy on my server under...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.