How to generate a GUID in VBScript?

99封情书 提交于 2019-11-27 12:01:42

问题


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?


回答1:


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



回答2:


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)



回答3:


' Returns a unique Guid on every call. Removes any cruft.
Function CreateGuid()
    CreateGuid = Left(CreateObject("Scriptlet.TypeLib").Guid,38)
End Function



回答4:


Set tlib = Server.CreateObject("Scriptlet.TypeLib")
strGuid = tlib.Guid



回答5:


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


来源:https://stackoverflow.com/questions/968756/how-to-generate-a-guid-in-vbscript

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!