马克贝 發表於 2024-2-6 14:02:00

delphi GUID相关操作

<h1 id="guid相关操作">GUID相关操作</h1>
<h2 id="代码">代码</h2>
<pre><code class="language-delphi">procedure TForm1.Button1Click(Sender: TObject);
var
GUID1, GUID2: TGUID;
begin
//创建GUID
if CreateGUID(GUID1) &lt;&gt; 0 then
   Memo1.Lines.Add('创建失败');
//转换为字符串
Memo1.Lines.Add(GUIDToString(GUID1));
//分段输出
Memo1.Lines.Add(Format('D1 %x', ));
Memo1.Lines.Add(Format('D2 %x', ));
Memo1.Lines.Add(Format('D3 %x', ));
Memo1.Lines.Add(Format('D4 %x%x', , GUID1.D4]));
Memo1.Lines.Add(Format('D4 %x%x%x%x%x%x', , GUID1.D4, GUID1.D4, GUID1.D4, GUID1.D4, GUID1.D4]));
//将字符串转换为GUID
GUID2 := StringToGUID('{00000000-0000-0000-0000-000000000000}');
//比较是否相等
if IsEqualGUID(GUID1, GUID2) then
    Memo1.Lines.Add('相等')
else
    Memo1.Lines.Add('不相等');
end;
</code></pre>
<h2 id="方法">方法</h2>
<h3 id="systemsysutilscreateguid">System.SysUtils.CreateGUID</h3>
<pre><code class="language-delphi">function CreateGUID(out Guid: TGUID): HResult;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>将 <em>GUID</em> 设置为新创建的全局唯一标识符。</p>
<blockquote>
<p>在 <strong>Windows</strong> 上,<code>CreateGUID</code> 调用 Windows API CoCreateGuid 。</p>
<p>在 <strong>MAC OS</strong> 上,GUID 是使用 libc 运行时的 uuid_generate_time 函数生成的,该函数使用网卡 MAC 地址(如果可用)。</p>
</blockquote>
<h3 id="systemsysutilsguidtostring">System.SysUtils.GUIDToString</h3>
<pre><code class="language-delphi">function GUIDToString(const Guid: TGUID): string;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>将 <em>GUID</em> 转换为字符串。</p>
<p>如果成功,将返回所请求的 GUID 的字符串。如果失败,则会引发 <code>EConvertError</code>异常。</p>
<h3 id="systemsysutilsisequalguid">System.SysUtils.IsEqualGUID</h3>
<pre><code class="language-delphi">function IsEqualGUID(const Guid1, Guid2: TGUID): Boolean;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>表示两个 <code>TGUID</code> 值是否相同。如果值相等,则返回 <em>True</em>。</p>
<h3 id="systemsysutilsstringtoguid">System.SysUtils.StringToGUID</h3>
<pre><code class="language-delphi">function StringToGUID(const S: string): TGUID;
</code></pre>
<p><strong>unit</strong></p>
<p>System.SysUtils</p>
<p>将字符串转换为 <code>TGUID</code> 数据结构。</p>
<p>如果成功,则返回请求的 GUID。如果失败,则会引发<code>EConvertError</code>异常。</p>
<h3 id="systemtguid">System.TGUID</h3>
<pre><code class="language-delphi">TGUID = record
</code></pre>
<p><strong>unit</strong></p>
<p>System</p>
<p><code>TGUID</code> 是全局唯一标识符的结构化形式。</p>
<p>具有以下形式:</p>
<pre><code>['{xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx}']
</code></pre>
<p>其中每个<code>x</code>是一个十六进制数字。可以通过在代码编辑器中按 <code>CTRL+SHIFT+G</code> 来生成 GUID。</p>
<p><code>TGUID</code> 提供对 GUID 的结构化访问:</p>
<ul>
<li>前 8 个十六进制数字映射 <strong>D1</strong>.</li>
<li>接下来的 4 个十六进制数字映射 <strong>D2</strong>.</li>
<li>接下来的 4 个十六进制数字映射 <strong>D3</strong>.</li>
<li>接下来的 4 个十六进制数字映射 <strong>D4</strong> 中的前 2 个字节。</li>
<li>最后 12 个十六进制数字映射 <strong>D4</strong> 中的剩余 6 个字节。</li>
</ul><br><br>
来源:https://www.cnblogs.com/txgh/p/18009613
頁: [1]
查看完整版本: delphi GUID相关操作