我们来做一个组件,一个可以生成GUID的组件
全局唯一标志(GUID)
一个GUID可以表示成32个字符的十六进制格式。COM提供了一个名叫CoCreateGuid的系统级函数,可以用它来生成新的GUID。该函数依靠一种类似使用计算机网卡的唯一标志符和系统时钟等信息的算法,来创建一个在时间和空间上都唯一的GUID.
打开vb6,新建Activex Dll工程。工程名修改为fCom,类名修改为fC8
Option Explicit
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
'取GUID码
'参数:
'返回值:STRING,一个GUID码.
Public Function GetGUIDCode() As String
Dim uGUID As GUID
Dim sGUID As String
Dim bGUID() As Byte
Dim lLen As Long
Dim RetVal As Long
lLen = 40
bGUID = String(lLen, 0)
CoCreateGuid uGUID '把结构转换为一个可显示的字符串
RetVal = StringFromGUID2(uGUID, VarPtr(bGUID(0)), lLen)
sGUID = bGUID
If (Asc(Mid$(sGUID, RetVal, 1)) = 0) Then RetVal = RetVal - 1
GetGUIDCode = Left$(sGUID, RetVal)
End Function
Ok,一个组件就写好了,点击菜单->文件->生成fCom.dll文件
确定,在目录下就会有fCom.dll文件
测试
打开visual interdev6.0,生成一个asp文件
<%@ Language=VBScript %>
<HTML>
<BODY>
<%
dim obj
set obj=server.CreateObject("fCom.fC8")
Response.Write obj.GetGUIDCode()
Response.Write "<br>"
Response.Write obj.GetGUIDCode()
%>
</BODY>
</HTML>
配置好虚拟目录,在ie中执行此asp文件,得到结果如下:
{EF7BF6F9-564C-44D9-91BF-F31695DA6298}
{C8A68CAC-A108-45A6-84F5-0BF92B9CF721}
可以多次刷新,可以看到这个值在不断变化.