Recent

Author Topic: how get real serialnumber hdd  (Read 11592 times)

elzawey2020

  • New Member
  • *
  • Posts: 32
how get real serialnumber hdd
« on: May 12, 2019, 12:42:05 am »
hiiii

how get real serialnumber  hdd  such componnent hddinfo by delphi

elzawey2020

  • New Member
  • *
  • Posts: 32
Re: how get real serialnumber hdd
« Reply #1 on: May 12, 2019, 11:06:18 pm »
thanks

esvignolo

  • Full Member
  • ***
  • Posts: 159
  • Using FPC in Windows, Linux, Macos
Re: how get real serialnumber hdd
« Reply #2 on: May 13, 2019, 01:27:05 am »

Thaddy

  • Hero Member
  • *****
  • Posts: 18305
  • Here stood a man who saw the Elbe and jumped it.
Re: how get real serialnumber hdd
« Reply #3 on: May 13, 2019, 07:22:59 am »
The original hddinfo simply works for fpc too.. (windows only)
Code: Pascal  [Select][+][-]
  1. unit hddinfo;
  2. {$ifdef fpc}{$mode delphi}{$endif}
  3. interface
  4.  
  5. uses Windows, SysUtils, Classes;
  6.  
  7. const
  8.   IOCTL_STORAGE_QUERY_PROPERTY = $2D1400;
  9.  
  10. type
  11. THDDInfo = class (TObject)
  12. private
  13.   FDriveNumber: Byte;
  14.   FFileHandle: Cardinal;
  15.   FInfoAvailable: Boolean;
  16.   FProductRevision: string;
  17.   FProductId: string;
  18.   FSerialNumber: string;
  19.   FVendorId: string;
  20.   procedure ReadInfo;
  21.   procedure SetDriveNumber(const Value: Byte);
  22. public
  23.   constructor Create;
  24.   property DriveNumber: Byte read FDriveNumber write SetDriveNumber;
  25.   property VendorId: string read FVendorId;
  26.   property ProductId: string read FProductId;
  27.   property ProductRevision: string read FProductRevision;
  28.   property SerialNumber: string read FSerialNumber;
  29.   function SerialNumberInt: Cardinal;
  30.   function SerialNumberText: string;
  31.   function IsInfoAvailable: Boolean;
  32. end;
  33.  
  34. implementation
  35.  
  36. type
  37.  
  38. STORAGE_PROPERTY_QUERY = packed record
  39.   PropertyId: DWORD;
  40.   QueryType: DWORD;
  41.   AdditionalParameters: array[0..3] of Byte;
  42. end;
  43.  
  44. STORAGE_DEVICE_DESCRIPTOR = packed record
  45.   Version: ULONG;
  46.   Size: ULONG;
  47.   DeviceType: Byte;
  48.   DeviceTypeModifier: Byte;
  49.   RemovableMedia: Boolean;
  50.   CommandQueueing: Boolean;
  51.   VendorIdOffset: ULONG;
  52.   ProductIdOffset: ULONG;
  53.   ProductRevisionOffset: ULONG;
  54.   SerialNumberOffset: ULONG;
  55.   STORAGE_BUS_TYPE: DWORD;
  56.   RawPropertiesLength: ULONG;
  57.   RawDeviceProperties: array[0..511] of Byte;
  58. end;
  59.  
  60. function ByteToChar(const B: Byte): Char;
  61. begin
  62.   Result := Chr(B + $30)
  63. end;
  64.  
  65. function SerialNumberToCardinal (SerNum: String): Cardinal;
  66. begin
  67.   HexToBin(PChar(SerNum), PChar(@Result), SizeOf(Cardinal));
  68. end;
  69.  
  70. function SerialNumberToString(SerNum: String): String;
  71. var
  72.   I, StrLen: Integer;
  73.   Pair: string;
  74.   B: Byte;
  75.   Ch: Char absolute B;
  76.  
  77. begin
  78.   Result := '';
  79.   StrLen := Length(SerNum);
  80.  
  81.   if Odd(StrLen) then Exit;
  82.  
  83.   I := 1;
  84.  
  85.   while I < StrLen do
  86.   begin
  87.     Pair := Copy (SerNum, I, 2);
  88.     HexToBin(PChar(Pair), PChar(@B), 1);
  89.     Result := Result + Chr(B);
  90.     Inc(I, 2);
  91.   end;
  92.  
  93.   I := 1;
  94.  
  95.   while I < Length(Result) do
  96.   begin
  97.     Ch := Result[I];
  98.     Result[I] := Result[I + 1];
  99.     Result[I + 1] := Ch;
  100.     Inc(I, 2);
  101.   end;
  102. end;
  103.  
  104. constructor THddInfo.Create;
  105. begin
  106.   inherited;
  107.  
  108.   SetDriveNumber(0);
  109. end;
  110.  
  111. function THDDInfo.IsInfoAvailable: Boolean;
  112. begin
  113.   Result := FInfoAvailable
  114. end;
  115.  
  116. procedure THDDInfo.ReadInfo;
  117. type
  118.   PCharArray = ^TCharArray;
  119.   TCharArray = array[0..32767] of Char;
  120.  
  121. var
  122.   Returned: Cardinal;
  123.   Status: LongBool;
  124.   PropQuery: STORAGE_PROPERTY_QUERY;
  125.   DeviceDescriptor: STORAGE_DEVICE_DESCRIPTOR;
  126.   PCh: PChar;
  127.  
  128. begin
  129.   FInfoAvailable := False;
  130.   FProductRevision := '';
  131.   FProductId := '';
  132.   FSerialNumber := '';
  133.   FVendorId := '';
  134.  
  135.   try
  136.     FFileHandle := CreateFile(
  137.                      PChar('\\.\PhysicalDrive' + ByteToChar(FDriveNumber)),
  138.                      0,
  139.                      FILE_SHARE_READ or FILE_SHARE_WRITE,
  140.                      nil,
  141.                      OPEN_EXISTING,
  142.                      0,
  143.                      0
  144.                    );
  145.  
  146.     if FFileHandle = INVALID_HANDLE_VALUE then RaiseLastOSError;
  147.  
  148.     ZeroMemory(@PropQuery, SizeOf(PropQuery));
  149.     ZeroMemory(@DeviceDescriptor, SizeOf(DeviceDescriptor));
  150.  
  151.     DeviceDescriptor.Size := SizeOf(DeviceDescriptor);
  152.  
  153.     Status := DeviceIoControl(
  154.                 FFileHandle,
  155.                 IOCTL_STORAGE_QUERY_PROPERTY,
  156.                 @PropQuery,
  157.                 SizeOf(PropQuery),
  158.                 @DeviceDescriptor,
  159.                 DeviceDescriptor.Size,
  160.                 Returned,
  161.                 nil
  162.               );
  163.  
  164.     if not Status then RaiseLastOSError;
  165.  
  166.     if DeviceDescriptor.VendorIdOffset <> 0 then
  167.     begin
  168.       PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.VendorIdOffset];
  169.       FVendorId := PCh;
  170.     end;
  171.  
  172.     if DeviceDescriptor.ProductIdOffset <> 0 then
  173.     begin
  174.       PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.ProductIdOffset];
  175.       FProductId := PCh;
  176.     end;
  177.  
  178.     if DeviceDescriptor.ProductRevisionOffset <> 0 then
  179.     begin
  180.       PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.ProductRevisionOffset];
  181.       FProductRevision := PCh;
  182.     end;
  183.  
  184.     if DeviceDescriptor.SerialNumberOffset <> 0 then
  185.     begin
  186.       PCh := @PCharArray(@DeviceDescriptor)^[DeviceDescriptor.SerialNumberOffset];
  187.       FSerialNumber := PCh;
  188.     end;
  189.  
  190.     FInfoAvailable := True;
  191.   finally
  192.     if FFileHandle <> INVALID_HANDLE_VALUE then CloseHandle(FFileHandle);
  193.   end;
  194. end;
  195.  
  196. function THDDInfo.SerialNumberInt: Cardinal;
  197. begin
  198.   Result := 0;
  199.   if ((IsInfoAvailable = True) and (FSerialNumber <> '')) then Result := SerialNumberToCardinal(FSerialNumber)
  200. end;
  201.  
  202. function THDDInfo.SerialNumberText: string;
  203. begin
  204.   Result := '';
  205.   if ((IsInfoAvailable = True) and (FSerialNumber <> '')) then Result := SerialNumberToString(FSerialNumber)
  206. end;
  207.  
  208. procedure THDDInfo.SetDriveNumber(const Value: Byte);
  209. begin
  210.   FDriveNumber := Value;
  211.   ReadInfo;
  212. end;
  213.  
  214. end.
That is the original code, just fpc added, and not written by me...

(For the highly technical: this is a POSIX api, so we should be able to make it more portable. OP should ignore this remark)
« Last Edit: May 13, 2019, 09:20:02 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Handoko

  • Hero Member
  • *****
  • Posts: 5485
  • My goal: build my own game engine using Lazarus
Re: how get real serialnumber hdd
« Reply #4 on: October 18, 2020, 06:38:03 pm »
I just tested hddinfo, it works. I want to use it but I can't find its license information. Can anyone tell me what its license is?

Sorry to bring up an old thread.

Sieben

  • Sr. Member
  • ****
  • Posts: 380
Re: how get real serialnumber hdd
« Reply #5 on: October 18, 2020, 09:25:29 pm »
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: how get real serialnumber hdd
« Reply #6 on: October 18, 2020, 10:07:39 pm »
Hi!

Be aware of Softpedia!
Their links lead to nowhere until you bought their download managar!

Load it from good old Torry:

https://torry.net/quicksearchd.php?String=THDDInfo&Title=Yes

Winni


Sieben

  • Sr. Member
  • ****
  • Posts: 380
Re: how get real serialnumber hdd
« Reply #7 on: October 18, 2020, 10:14:58 pm »
Since he's got working code already the link was just meant to point out that it seems to be freeware.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: how get real serialnumber hdd
« Reply #8 on: October 18, 2020, 10:46:58 pm »
Hi!

And I wanted to point out point out that this is not true for Softpedia:
They want money.

Winni

Handoko

  • Hero Member
  • *****
  • Posts: 5485
  • My goal: build my own game engine using Lazarus
Re: how get real serialnumber hdd
« Reply #9 on: October 19, 2020, 03:37:19 am »
The oldest page that has THDDInfo I can get is from Delphi-PRAXis in December 2006, but still not the original author:
https://www.delphipraxis.net/564756-post28.html

Translated using Google Translate:
Quote
Hi dbdeath74,
have attached the file hddinfo.pas to you. The original version is not mine, but I have adapted it so that it now also runs with NoAdmin rights (tested with guest rights) and fixed a few bugs.

The THDDInfo from Softpedia 2.x is in July 2010.
The THDDInfo from Torry's Delphi Pages in June 2009.

GAN

  • Sr. Member
  • ****
  • Posts: 388
Re: how get real serialnumber hdd
« Reply #10 on: October 19, 2020, 07:02:25 am »
Hi Handoko, I have a program that reads the serial number of the disks but only for Linux. The attachment here.
Linux Mint Mate (allways)
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite - LazReport

Handoko

  • Hero Member
  • *****
  • Posts: 5485
  • My goal: build my own game engine using Lazarus
Re: how get real serialnumber hdd
« Reply #11 on: October 19, 2020, 08:08:25 am »
Yes, I'm looking for the code to read serial number of disk that run on Linux too. Thank you very much for sharing it. I just tested it, it works.

Can I add your code into my program? What is the license term?

I am writing a tool to allow users to share files over a FTP connection. I already solved many of the problems. For security reason I want each UserID to be associated with an unique serial number of the computer so user cannot faking their ID. The best I can think of is hard disk serial number. I write the program for my friend, so she can share/copy data between the office computers and home. The program is free but if she asks for extra features, I may charge her a price. Because it may be a commercial project, I need to know the license term of the components I use.
« Last Edit: October 19, 2020, 08:13:30 am by Handoko »

GAN

  • Sr. Member
  • ****
  • Posts: 388
Re: how get real serialnumber hdd
« Reply #12 on: October 19, 2020, 08:36:01 am »
I'm sure you can use it, I don't like licenses very much.
And yes, you can use this code in commercial programs freely, you don't need to quote me, or anything.
Linux Mint Mate (allways)
Zeos 7̶.̶2̶.̶6̶ 7.1.3a-stable - Sqlite - LazReport

Sieben

  • Sr. Member
  • ****
  • Posts: 380
Re: how get real serialnumber hdd
« Reply #13 on: October 19, 2020, 11:08:34 am »
The copyright and license info of THHDInfo is included with the download, as well as the author's name and an url that doesn't seem to exist anymore, however.
Lazarus 2.2.0, FPC 3.2.2, .deb install on Ubuntu Xenial 32 / Gtk2 / Unity7

Handoko

  • Hero Member
  • *****
  • Posts: 5485
  • My goal: build my own game engine using Lazarus
Re: how get real serialnumber hdd
« Reply #14 on: October 19, 2020, 12:33:43 pm »
I found the author and its website. THDDInfo is allowed for commercial usage but requires copyright notice: THDDInfo Delphi component Copyright (c) by Artem Parlyuk and link to author's site and email, but unfortunately the domain has already expired since December 2013.

The terms also mentioned distribution of the source code is allowed as long as no alternations are made to the contents. I think I am allowed to modify the THDDInfo as necessary to use in my closed-source project as long as I provide the copyright notice and do not distribute the modified version of THDDInfo, am I correct?

 

TinyPortal © 2005-2018