如何在Delphi上生成唯一的计算机ID?

Seb*_*ian 6 delphi unique serial-number identification

如何快速为delphi应用程序生成唯一的计算机ID?我以前用c#很容易做到这一点,但有时失败了.我确实希望ID是"静态的",但我不在乎是因为硬件更改或操作系统重新安装而导致id更改,我打算将其存储在注册表中并在应用启动时检查它,如果它已更改更新注册表.(我知道如何编写注册表部分,我只需要帮助唯一的ID).

谢谢.

Joh*_*mas 5

看看 SysUtils.CreateGUID,它创建了一个全局唯一标识符。句法:

function CreateGUID(out Guid: TGUID): HResult; stdcall;
Run Code Online (Sandbox Code Playgroud)

取自 D2010 帮助的一个小例子:

{
This example demonstrates the usage of some GUID 
related routines along with the type itself.
}
procedure TForm2.FormCreate(Sender: TObject);
var
  MyGuid0, MyGuid1 : TGUID;

begin
  { Create a new GUID from the string representation. }
  MyGuid0 := StringToGUID('{00020400-0000-0000-C000-000000000046}');
  Memo1.Lines.Add('The GUID is: ' + GUIDToString(MyGuid0));

  {
  Accessing GUID's internal fields
  Using the Format function to obtain the same output as GUIDToString
  }
  Memo1.Lines.Add(Format('GUID using formatting is: ' +
       '{%0.8X-%0.4X-%0.4X-%0.2X%0.2X-%0.2X%0.2X%0.2X%0.2X%0.2X%0.2X}',
       [MyGuid0.D1, MyGuid0.D2, MyGuid0.D3,
       MyGuid0.D4[0], MyGuid0.D4[1], MyGuid0.D4[2], MyGuid0.D4[3],
       MyGuid0.D4[4], MyGuid0.D4[5], MyGuid0.D4[6], MyGuid0.D4[7]]));

  { Autogenerate a random GUID at run time. }
  if CreateGUID(MyGuid1) <> 0 then
     Memo1.Lines.Add('Creating GUID failed!')
  else
     Memo1.Lines.Add('The generated guid is: ' + GUIDToString(MyGuid1));

  { Generating second random GUID. }
  CreateGUID(MyGuid0);

  { Testing if two guids are equal. }
  if IsEqualGUID(MyGuid0, MyGuid1) then
     Memo1.Lines.Add('This cannot happen! CreateGUID guarantees that ' +
                     '2 randomly generated GUIDs cannot be equal!');
end;
Run Code Online (Sandbox Code Playgroud)

HTH

  • 这里的问题是它不是特定于机器的,再次调用 Createguid 将创建不同的 ID。我相信用户希望每次请求时 ID 都相同。 (7认同)