Certain WScript methods and properties not accessible in Visual Basic (279164)



The information in this article applies to:

  • Microsoft Windows Script Host 1.0
  • Microsoft Windows Script Host 2.0
  • Microsoft Visual Basic Professional Edition for Windows 4.0
  • Microsoft Visual Basic Professional Edition for Windows 5.0
  • Microsoft Visual Basic Professional Edition for Windows 6.0
  • Microsoft Visual Basic Enterprise Edition for Windows 4.0
  • Microsoft Visual Basic Enterprise Edition for Windows 5.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0

This article was previously published under Q279164

SUMMARY

WScript methods and properties cannot be created in the context of a Visual Basic object because they are part of the Windows Script Host (WSH) object model and must be hosted within a WScript object, which can only run in a WSH script.

MORE INFORMATION

The WScript object is often confused with the automation objects in WSH, such as WScript.Shell and WScript.Network. These automation objects can exist outside of a WSH script, but they must be created in the ObjectContext of the hosting environment (in other words, Server.CreateObject for Microsoft Active Server Pages [ASP] or WScript.CreateObject for WSH or simply CreateObject for Microsoft Visual Basic). Take note of the differences in the following examples. WSH and ASP have intrinsic methods and properties that are not available outside of their respective environments (WSH - Wscript.Echo, ASP - Response.Write).

WSH Sample
Set objWSH = WScript.CreateObject("WScript.Network")
WScript.Echo objWSH.Userdomain
WScript.Echo objWSH.Username
				

ASP Sample
<%
Set objWSH = Server.CreateObject("WScript.Network")
Response.Write objWSH.Userdomain
Response.Write objWSH.Username
%>
				

Visual Basic Sample
Set objWSH = CreateObject("WScript.Network")
MsgBox objWSH.Userdomain
MsgBox objWSH.Username
				

Unlike the examples above, which demonstrate WScript automation objects, the following example illustrates a WScript method that cannot be called by an ObjectContext.CreateObject method (note that WScript is not instantiated prior to calling WScript.Sleep and can only be used in a WSH script):
WScript.Sleep 1000
objWSH = WScript.Version
objWSH = WScript.ScriptFullName
				
The following properties and methods are dependent on WSH to be running and will cause an error if they are accessed outside of the context of WSH:
  • Properties
    • Application
    • Arguments
    • FullName
    • Name
    • Path
    • ScriptFullName
    • ScriptName
    • Version
  • Methods
    • Echo (for Visual Basic applications, use the MsgBox function or Debug.Print)
    • Sleep (for Visual Basic applications, use the Sleep() Win32 API or SetWaitbleTimer() Win32 API [see "References"]).
    • Popup (for Visual Basic applications, use the MsgBox function)
The StdIn property, StdOut property, and StdErr property can be used from Visual Basic, but they require a console program. These can also be accessed by opening $CON as a file using the proper API.

All of the methods of WSH are duplicated by other API calls that Visual Basic can access.

Although the WSH object model cannot be accessed through Visual Basic objects, the same functionality is available through the Visual Basic language and the Win32 API.

REFERENCES

For more information about the SetWaitbleTimer() Win32 API, click the following article number to view the article in the Microsoft Knowledge Base:

231298 How to use SetWaitableTimer with Visual Basic

For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

190000 How to get started programming with the Windows API (LONG)

202179 How to call Windows API functions with special requirements from Visual Basic

171654 How to attach a console window to your Visual Basic program


Modification Type:MajorLast Reviewed:5/25/2005
Keywords:kbDSWManage2003Swept kbinfo KB279164