Outputting a GUID in VBScript ignores all text aft

2019-04-20 00:53发布

I'm creating a GUID for use in a Classic ASP application, by using TypeLib. However, even a simple test such as writing the GUID out to the screen is giving me problems - it prints the GUID but ignores everything after it (e.g. HTML tags, additional words, anything).

Here's the rudimentary code to test this:

Set typeLib = Server.CreateObject("Scriptlet.TypeLib")
myGuid = typeLib.Guid
Response.Write myGuid & " is the new GUID"
Set typeLib = Nothing

This will display something like {9DDB27D1-F034-41D7-BB88-D0D811DB91CE} and that's it; the rest of the text is ignored and isn't written out. However, if I hard-code that GUID value and reference it from a variable, the rest of the text appears just fine. I've tried explicit conversion to a String value before displaying, but it still happens.

4条回答
We Are One
2楼-- · 2019-04-20 01:43

I seem to have solved my own problem.. it was adding something extra to the text, so I had to do:

myGuid = Left(myGuid, Len(myGuid)-2)

and it now outputs fine. Strange.

查看更多
趁早两清
3楼-- · 2019-04-20 01:44

It adds a vbNullChar or Chr(0) at the end of the GUID. Replace(myGuid, Chr(0), "") will fix it. Better than using Left or Mid functions.

查看更多
做自己的国王
4楼-- · 2019-04-20 01:44

GUID is a struct and not a string, you need to add a ToString() method to output it as a string.

https://msdn.microsoft.com/fr-fr/library/97af8hh4(v=vs.110).aspx

Response.Write myGuid.ToString("D")
查看更多
爷的心禁止访问
5楼-- · 2019-04-20 01:59

I use something like this

Function GetGuid() 
        Set TypeLib = CreateObject("Scriptlet.TypeLib") 
        GetGuid = Left(CStr(TypeLib.Guid), 38) 
        Set TypeLib = Nothing 
End Function 
查看更多
登录 后发表回答