How to generate an unique computer id on Delphi?
Asked Answered
L

4

6

How can I quickly generate an unique computer id for a delphi app? I used to do it easly with c#, but this failed sometimes. I do want the ID to be "static" but I don't care if the id changes because of a hardware change or OS reinstallation, I was planning to store it in the registry and check it when the app starts, and if it changed update the registry. (I know how to code the registry part, I only need help for the unique id).

Thanks.

Lineage answered 5/12, 2009 at 12:53 Comment(0)
J
6

Use the hard disk serial number or MAC address:

https://stackoverflow.com/questions/331106/how-to-get-serial-number-from-hard-disks (dead link)
Generating a unique machine id

Jorgensen answered 6/12, 2009 at 2:44 Comment(1)
Now also on stackoverflow: Link rot.Tacky
A
5

Have a look at SysUtils.CreateGUID which creates a globally unique identifier. Syntax:

function CreateGUID(out Guid: TGUID): HResult; stdcall;

A small example taken from D2010's help:

{
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;

HTH

Ananthous answered 5/12, 2009 at 16:44 Comment(2)
problem here is that it is not machine specific and calling Createguid again will create a different id. I believe the user wanted the ID to be the same each time it is requested.Allerie
@Allerie If GUID already exists in some predetermined location in the registry, there's no need to generate a new one - and therefore the ID will "be the same each time it is requested". However, by using the serial number of a hardware component, the ID changes as soon as that particular component is upgraded. OP didn't explain why this ID was needed, but this approach would certainly produce something acceptable in terms of what OP did actually ask for.Agueda
A
2

I'm not exactly sure what you're asking? It might help if you clarified why you need the "unique computer id".

Option 1

The bit:

"I was planning to store it in the registry and check it when the app starts, and if it changed update the registry."

gives me the impression you're trying to detect a different (newer) version of the app and/or identify different apps within a suite.

If this is the case, then simply pressing Ctrl+Shift+G will generate a unique GUID for you and insert it at your current cursor position in code. You may want to also look at the version information; perhaps auto-increment build numbers?

Option 2

If you're trying to in some way identify unique installations on different computers, take a look at the CreateGUID function in plainth's answer.

Option 3

You mentioned that you "don't care if the id changes because of a hardware change or OS reinstallation". I'm not sure if this is an important fact like: you're trying to detect hardware changes? After all, you did say you don't care; but then why mention this at all?

Option 4

Something else I haven't considered?

Agueda answered 5/12, 2009 at 18:36 Comment(0)
R
0

You may try using an UUID algorithm for the following reasons:

  • It independs hardware
  • It`s really random
  • It is platform independent.

But watch out: to achieve hardware independence, you must choose some algorithm which does not use the MAC address of the machine. Microsoft`s GUID uses it (this was how they discovered who wrote that LOVE YOU virus, remember it?)

Refresher answered 5/12, 2009 at 12:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.