Delphi GUID[2] 获取GUID值的方式
Delphi GUID[2] 获取GUID值的方式
源码:
function CoCreateGuid(out guid: TGUID): HResult; stdcall; external 'ole32.dll' name 'CoCreateGuid';
function CreateGUID(out Guid: TGUID): HResult;
begin
Result := CoCreateGuid(Guid);
end;
function GUIDToString(const GUID: TGUID): string;
var
P: PWideChar;
begin
if not Succeeded(StringFromCLSID(GUID, P)) then
ConvertError(@SInvalidGUID);
Result := P;
CoTaskMemFree(P);
end;
方式1:
function GetGUID: string;
var
Guid: TGUID;
begin
CreateGUID(Guid);
Result := GUIDToString(Guid);
end;
procedure TForm1.BitBtn1Click(Sender: TObject);
begin
ShowMessage(GetGUID);
end;
方式2:
function Guid_Gen: ansistring;
var
s,gstr: string;
i: longint;
begin
s := '0123456789abcdef';
//8-4-4-4-12
gstr := 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
for i := 1 to length(gstr) do
begin
if gstr[i] = 'x' then
gstr[i] := s[Random(16) + 1];
end;
Result:=gstr;
end;
procedure TForm1.BitBtn2Click(Sender: TObject);
begin
ShowMessage(Guid_Gen);
end;
创建时间:2021.05.25 更新时间:
博客园 滔Roy https://www.cnblogs.com/guorongtao 希望内容对你有所帮助,谢谢!