Code Examples
  Home arrow Code Examples arrow Page 3 - Coding a Custom Object with WSC
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
CODE EXAMPLES

Coding a Custom Object with WSC
By: Nilpo
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 3
    2009-03-31

    Table of Contents:
  • Coding a Custom Object with WSC
  • Adding code for the object methods
  • Adding objects and resources
  • Registering and using your component
  • Protecting your intellectual property

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Coding a Custom Object with WSC - Adding objects and resources


    (Page 3 of 5 )

    Because Windows Script Components need to support COM-enabled applications outside of scripts alone, they have the ability to do things that you’re probably not used to seeing if you’re not an application developer.  I’m not going to get into too much detail here, but I’ll show you a few things that you can use to your advantage.

           </registration>

     

           <object id="objShell" progid="Shell.Application">

           <object id="objFso" progid="Scripting.FileSystemObject"/>

    Below my registration section I’m adding two <object> elements.  The <object> element allows me to specify an object that I want to be globally available in my component.  I can provide the name I want to use, as well as specify the Prog ID.  Here I’m just creating my Shell and FileSystem objects.

    While using the <object> element makes my object available in the global namespace, that doesn’t provide much benefit over using CreateObject() in my script.  However, I do not have access to the WScript.CreateObject() method in a script component.  This means that objects can only be loaded using VBScript’s internal CreateObject() method.  As you may or may not know, this has a limitation when it comes to event scripting.  VBS’s CreateObject method cannot create an event handle in scripts.  The <object> element can.  All I need to do is set its optional “events” attribute to true and my component will begin handling any events fired by that object.

    Below my <object> elements I’m now going to add some object references.  Reference elements are used to create a reference to a resource’s Type Library.

           <reference object="ADODB.Stream"/>

           <reference object="Scripting.FileSystemObject"/>

    If you’ve never developed in .Net or some higher-level programming language, you may not understand what resource references are used for.  Many COM objects expose enumerations, or lists of constants, used by the object.  In scripting we’re accustomed to defining these constants in our code because VBScript does not provide built-in support for enumerations.  You’re probably used to writing code like this:

    Const ForWriting = 2

    Set objFile = objFso.OpenTextFile(strPath, ForWriting, True)

    Here we’re defining a constant ForWriting with an integer value of 2 that the FileSystemObject uses when calling the OpenTextFile method.  The fact is that this constant is already defined inside of the FileSystemObject, but VBScript can’t see it.  By providing a reference to the FileSystemObject, I can write this type of code inside of my component:

    Set objFile = objFso.OpenTextFile(strPath, ForWriting, True)

    Because my component has a reference to the FileSystemObject, it has access to any constants that are exposed by its enumerations.  I can safely use ForWriting without having to assign it first because it’s already been provided for me.

    For those of you who only do general scripting, this may cause some problems, since you aren’t familiar with the enumerations presented by the various objects.  You can dig around in the MSDN and get the enumeration lists, but it may or may not be worth your time.  There are some script editors, however, that are capable of using this reference in conjunction with their auto-completion features.  This will allow your script editor to auto-complete properties and methods for objects in the same way that it does for VBScript’s internal methods.  The only editor that I’m aware of, however, that supports listing enumerated constants is SystemScripter 6.0.

    More Code Examples Articles
    More By Nilpo


     

    CODE EXAMPLES ARTICLES

    - Bipartite Graphs
    - Connectivity in Graphs
    - The Ford-Fulkerson Algorithm
    - Critical Paths
    - The Bellman-Ford and Roy-Floyd Algorithms
    - Shortest Path Algorithms in Graphs
    - Minimum Spanning Tree
    - Articulation Edges and Vertexes
    - Circles and Connectivity in Graphs
    - Depth-First Search in Graphs
    - Breadth-First Search in Graphs
    - The Prufer Code and the Floyd-Warshall Algor...
    - An Insight into Graphs
    - Coding a Custom Object with WSC
    - Creating a Custom Object with WSC





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek