How to Get the GUID using VBScript or Javascript (JScript) using Scriptlet.TypeLib?


The GUID represents an unique identifier, and you can get the API to generate one for you.

In WSH (Windows Scripting Host) environment, it turns out to be very easy to generate a new GUID using the TypeLib object. Let’s demonstrate the usage using VBScript and JScript (Microsoft Javascript implementation).

How to Get the GUID using VBScript?

1
2
Set TypeLib = CreateObject("Scriptlet.TypeLib")
WScript.Echo TypeLib.Guid
Set TypeLib = CreateObject("Scriptlet.TypeLib")
WScript.Echo TypeLib.Guid

This prints out something like this:

{95958720-CF02-40F1-AD80-3C2321A2776B}

As seen, with the curly braces.

How to Get the GUID using JavaScript?

With Javascript, it is quite similar.

1
2
var TypeLib = new ActiveXObject("Scriptlet.TypeLib")
WScript.Echo (TypeLib.Guid);
var TypeLib = new ActiveXObject("Scriptlet.TypeLib")
WScript.Echo (TypeLib.Guid);

Everytime, the GUID is different.

{2C4C889E-E938-4B71-A926-4F5B6A1BAD9E}

If you output the length of the GUID, it is 40 characters instead of 38 (as shown in above examples). Why is this? The TypeLib.Guid adds two non-printable characters at the end. If you print them out, e.g. using charCodeAt, you can see the ASCII code of these two characters is ZERO.

Why add double zeros at the end? I dont know... Click To Tweet

If you are bothered with it, you can do this in VBScript:

1
2
Set TypeLib = CreateObject("Scriptlet.TypeLib")
WScript.Echo Left(TypeLib.Guid, 38)
Set TypeLib = CreateObject("Scriptlet.TypeLib")
WScript.Echo Left(TypeLib.Guid, 38)

or in Javascript:

1
2
var TypeLib = new ActiveXObject("Scriptlet.TypeLib")
WScript.Echo (TypeLib.Guid.substr(0, 38));
var TypeLib = new ActiveXObject("Scriptlet.TypeLib")
WScript.Echo (TypeLib.Guid.substr(0, 38));

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
378 words
Last Post: How to Read and Write a Binary File At the Same Time in C#?
Next Post: Multi-Processes Experiments - When Can Windows Utilize All the Cores?

The Permanent URL is: How to Get the GUID using VBScript or Javascript (JScript) using Scriptlet.TypeLib?

Leave a Reply