How to generate a GUID in VBScript?

偶尔善良 提交于 2019-11-28 19:11:48
Roger Lipscombe

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
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)
' Returns a unique Guid on every call. Removes any cruft.
Function CreateGuid()
    CreateGuid = Left(CreateObject("Scriptlet.TypeLib").Guid,38)
End Function
Set tlib = Server.CreateObject("Scriptlet.TypeLib")
strGuid = tlib.Guid

You Can Gen Random GUID Via This Object => "Scriptlet.TypeLib"

Help Link => With...End

Here is My Way =

With CreateObject("Scriptlet.TypeLib")
'' For Del This = {}
 WSH.Echo mid(.Guid ,2, 36)
 WSH.Echo (.Guid)
End With
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!