Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a GUID in VBScript?

I want to generate GUID strings in VBScript. I know that there's no built-in function in VBScript for generating one. I don't want to use random-generated GUIDs. Maybe there is an ActiveX object that can be created using CreateObject() that is sure to be installed on (newer) Windows versions that can generate a GUID?

like image 990
vividos Avatar asked Jun 09 '09 08:06

vividos


People also ask

How do I generate a GUID?

To Generate a GUID in Windows 10 with PowerShell, Type or copy-paste the following command: [guid]::NewGuid() . This will produce a new GUID in the output. Alternatively, you can run the command '{'+[guid]::NewGuid(). ToString()+'}' to get a new GUID in the traditional Registry format.

What is GUID in VB?

A GUID is a 128-bit integer (16 bytes) that can be used across all computers and networks wherever a unique identifier is required.

Can you generate the same GUID?

It's possible to generate an identical guid over and over. However, the chances of it happening are so low that you can assume they are unique.

How do I find my GUID in Visual Studio?

Insert a new GUID by invoking the command under the Edit top-level menu or hit CTRL+K,Space . If Visual Studio is unable to insert the GUID, it will be copied to the clipboard so you can easily paste it in manually. When a GUID is copied to the clipboard, a notification will be displayed in the status bar.


2 Answers

Function CreateGUID   Dim TypeLib   Set TypeLib = CreateObject("Scriptlet.TypeLib")   CreateGUID = Mid(TypeLib.Guid, 2, 36) End Function 

This function will return a plain GUID, e.g., 47BC69BD-06A5-4617-B730-B644DBCD40A9.

If you want a GUID in a registry format, e.g., {47BC69BD-06A5-4617-B730-B644DBCD40A9}, change the function's last line to

CreateGUID = Left(TypeLib.Guid, 38) 
like image 159
Helen Avatar answered Sep 26 '22 21:09

Helen


How Can I Create a GUID Using a Script? (in: Hey, Scripting Guy! Blog) says this:

Set TypeLib = CreateObject("Scriptlet.TypeLib") Wscript.Echo TypeLib.Guid 

However, note that Scriptlet.TypeLib.Guid returns a null-terminated string, which can cause some things to ignore everything after the GUID. To fix that, you might need to use:

Set TypeLib = CreateObject("Scriptlet.TypeLib") myGuid = TypeLib.Guid myGuid = Left(myGuid, Len(myGuid)-2) Wscript.Echo myGuid 
like image 34
Roger Lipscombe Avatar answered Sep 26 '22 21:09

Roger Lipscombe