数据结构:vb的GUID生成算法

来源:软件水平考试    发布时间:2012-11-05    软件水平考试视频    评论

  ´RETURNS:GUID if successful; blank string otherwise.
  ´Unlike the GUIDS in the registry, this function returns GUID
  ´without /"-/" characters.See comments for how to modify if you
  ´want the dash.
  Public Function GUID() As String
  Dim lRetVal As Long
  Dim udtGuid As GUID
  Dim sPartOne As String
  Dim sPartTwo As String
  Dim sPartThree As String
  Dim sPartFour As String
  Dim iDataLen As Integer
  Dim iStrLen As Integer
  Dim iCtr As Integer
  Dim sAns As String
  On Error GoTo errorhandler
  sAns = /"/"
  lRetVal = CoCreateGuid(udtGuid)
  If lRetVal = 0 Then
  ´First 8 chars
  sPartOne = Hex$(udtGuid.PartOne)
  iStrLen = Len(sPartOne)
  iDataLen = Len(udtGuid.PartOne)
  sPartOne = String((iDataLen * 2) - iStrLen, /"0/") _
  & Trim$(sPartOne)
  ´Next 4 Chars
  sPartTwo = Hex$(udtGuid.PartTwo)
  iStrLen = Len(sPartTwo)
  iDataLen = Len(udtGuid.PartTwo)
  sPartTwo = String((iDataLen * 2) - iStrLen, /"0/") _
  & Trim$(sPartTwo)
  ´Next 4 Chars
  sPartThree = Hex$(udtGuid.PartThree)
  iStrLen = Len(sPartThree)
  iDataLen = Len(udtGuid.PartThree)
  sPartThree = String((iDataLen * 2) - iStrLen, /"0/") _
  & Trim$(sPartThree) ´Next 2 bytes (4 hex digits)
  ´Final 16 chars
  For iCtr = 0 To 7
  sPartFour = sPartFour & _
  Format$(Hex$(udtGuid.PartFour(iCtr)), /"00/")
  Next
  ´To create GUID with /"-/", change line below to:
  ´sAns = sPartOne & /"-/" & sPartTwo & /"-/" & sPartThree _
  ´& /"-/" & sPartFour
  sAns = sPartOne & sPartTwo & sPartThree & sPartFour
  End If
  GUID = sAns
  Exit Function
  errorhandler:
  ´return a blank string if there´s an error
  Exit Function
  End Function

视频学习

我考网版权与免责声明

① 凡本网注明稿件来源为"原创"的所有文字、图片和音视频稿件,版权均属本网所有。任何媒体、网站或个人转载、链接转贴或以其他方式复制发表时必须注明"稿件来源:我考网",违者本网将依法追究责任;

② 本网部分稿件来源于网络,任何单位或个人认为我考网发布的内容可能涉嫌侵犯其合法权益,应该及时向我考网书面反馈,并提供身份证明、权属证明及详细侵权情况证明,我考网在收到上述法律文件后,将会尽快移除被控侵权内容。

最近更新

社区交流

考试问答