Office中国论坛/Access中国论坛

标题: 【源码通用函数】一个生成同步复制ID的函数(即GUID,全球同步唯一标识符) [打印本页]

作者: 红尘如烟    时间: 2010-12-11 19:27
标题: 【源码通用函数】一个生成同步复制ID的函数(即GUID,全球同步唯一标识符)
同步复制ID,即GUID(全球同步唯一标识符),通过特定的算法,生成的一个128位的字符串,在纳秒级的时间内,任意两台电脑生成的GUID都不会相同(理论上在公元3400年以前如此),从而确保标识的唯一性。
  1. Private Type GUID
  2. Data1 As Long
  3. Data2 As Integer
  4. Data3 As Integer
  5. Data4(7) As Byte
  6. End Type

  7. Private Declare Function CoCreateGuid Lib "ole32.dll" (pguid As GUID) As Long

  8. '生成GUID(同步复制ID) ===红尘如烟=======
  9. Public Function GetGUID() As String
  10. Dim typGUID As GUID
  11. If (CoCreateGuid(typGUID) = 0) Then
  12. GetGUID = GetGUID & String(8 - Len(Hex$(typGUID.Data1)), "0") & Hex$(typGUID.Data1) & "-"
  13. GetGUID = GetGUID & String(4 - Len(Hex$(typGUID.Data2)), "0") & Hex$(typGUID.Data2) & "-"
  14. GetGUID = GetGUID & String(4 - Len(Hex$(typGUID.Data3)), "0") & Hex$(typGUID.Data3) & "-"
  15. GetGUID = GetGUID & IIf((typGUID.Data4(0) < &H10), "0", "") & Hex$(typGUID.Data4(0))
  16. GetGUID = GetGUID & IIf((typGUID.Data4(1) < &H10), "0", "") & Hex$(typGUID.Data4(1)) & "-"
  17. GetGUID = GetGUID & IIf((typGUID.Data4(2) < &H10), "0", "") & Hex$(typGUID.Data4(2))
  18. GetGUID = GetGUID & IIf((typGUID.Data4(3) < &H10), "0", "") & Hex$(typGUID.Data4(3))
  19. GetGUID = GetGUID & IIf((typGUID.Data4(4) < &H10), "0", "") & Hex$(typGUID.Data4(4))
  20. GetGUID = GetGUID & IIf((typGUID.Data4(5) < &H10), "0", "") & Hex$(typGUID.Data4(5))
  21. GetGUID = GetGUID & IIf((typGUID.Data4(6) < &H10), "0", "") & Hex$(typGUID.Data4(6))
  22. GetGUID = GetGUID & IIf((typGUID.Data4(7) < &H10), "0", "") & Hex$(typGUID.Data4(7))
  23. End If
  24. End Function
复制代码
[attach]44349[/attach]

作者: tmtony    时间: 2010-12-11 21:25
我也用一个,与红尘差不多,也凑个热闹


Private Type GUID
  Data1 As Long
  Data2 As Long
  Data3 As Long
  Data4(8) As Byte
End Type

Private Declare Function CoCreateGuid Lib "ole32.dll" ( _
  pguid As GUID) As Long

Private Declare Function StringFromGUID2 Lib "ole32.dll" ( _
  rguid As Any, _
  ByVal lpstrClsId As Long, _
  ByVal cbMax As Long) As Long


Public Function gt_GetGuid() As String

Dim g As GUID
Dim b() As Byte
Dim lSize As Long
Dim lR As Long

CoCreateGuid g

lSize = 40
ReDim b(0 To (lSize * 2) - 1) As Byte
lR = StringFromGUID2(g, VarPtr(b(0)), lSize)
gt_GetGuid = Left$(b, lR - 1)

End Function

作者: 82077802    时间: 2010-12-12 08:23
不错,收藏

作者: 红尘如烟    时间: 2010-12-12 15:48
tmtony 发表于 2010-12-11 21:25
我也用一个,与红尘差不多,也凑个热闹

赞一个,老大这个比我用的效率要高的多
作者: tmtony    时间: 2010-12-13 12:00
GUID就是太长了点, 红尘常用吗?  我一般是需要同步的才用它, 但内部操作还是用长整型
作者: t小宝    时间: 2010-12-14 15:55
暂时没有用到,收藏起来




欢迎光临 Office中国论坛/Access中国论坛 (http://www.office-cn.net/) Powered by Discuz! X3.3