Solved

How to get "disk serial number"

Posted on 1997-06-10
2
316 Views
Last Modified: 2010-04-03
I need compare a decrypted string with the serial number of the disk where it is installed, as a way to avoid people to duplicate the software I deliver to them. I have not heard from a clever method to get access to that serial number string.
I presently achieve it (Delphi 1 Windows 3.11) with a not very clever method, though efficient : I use ShellExecute passing the command "command.com /c dir c:\ >text.tmp", launching the DOS command interpreter, using the DIR command, and redirecting the output into a text file, from which I read the line where the useful information is written. Do you have any clever method avoiding this call to DOS ?
0
Comment
Question by:pousticrousti
2 Comments
 
LVL 4

Accepted Solution

by:
erajoj earned 200 total points
Comment Utility
With Win32, this is easy. But with Windows 3.x & Delphi 1, it's worth the 200 pts.
This code is tested and it works fine:

var
  Form1   : TForm1;
  SerialNo: LongInt;

implementation

{$R *.DFM}

type
  TDriveChar = '@'..'Z'; {'@' = current, 'A', 'B', etc.}

  PRealModeRegs = ^TRealModeRegs;
  TRealModeRegs = record
    case Integer of
      0: (
        EDI, ESI, EBP, EXX, EBX, EDX, ECX, EAX: Longint;
        Flags, ES, DS, FS, GS, IP, CS, SP, SS: Word);
      1: (
        DI, DIH, SI, SIH, BP, BPH, XX, XXH: Word;
        case Integer of
          0: (
            BX, BXH, DX, DXH, CX, CXH, AX, AXH: Word);
          1: (
            BL, BH, BLH, BHH, DL, DH, DLH, DHH,
            CL, CH, CLH, CHH, AL, AH, ALH, AHH: Byte));
  end;

function RealModeInt(Int: Byte; var Regs: TRealModeRegs): Boolean;
{ procedure invokes int 31h function 0300h to simulate a real mode }
{ interrupt from protected mode. }
var
  ErrorFlag: Boolean;
begin
  asm
    mov ErrorFlag, 0       { assume success }
    mov ax, 0300h          { function 300h }
    mov bl, Int            { real mode interrupt to execute }
    mov bh, 0              { required }
    mov cx, 0              { stack words to copy, assume zero }
    les di, Regs           { es:di = Regs }
    int 31h                { DPMI int 31h }
    jnc @@End              { carry flag set on error }
  @@Error:
    mov ErrorFlag, 1       { return false on error }
  @@End:
  end;
  Result := not ErrorFlag;
end;

function GetVolSerNo(Drive: TDriveChar): LongInt;
type
  PDiskInfo = ^TDiskInfo;
  TDiskInfo = record
    InfoLevel: Word;
    DiskSerNum: LongInt;
    VolumeLabel: array[0..10] of Byte;
    FileSysType: array[0..7] of Byte;
  end;
var
  L: LongInt;
  P: PDiskInfo;
  R: TRealModeRegs;
begin
  Result := 0;
  L := GlobalDOSAlloc(SizeOf(TDiskInfo));
  if L <> 0 then
  try
    P := Ptr(LoWord(L),0);
    FillChar(P^,sizeof(TDiskInfo),0);
    FillChar(R,sizeof(R),0);
    with R do
    begin
      AX := $6900;
      BL := Ord(Drive)-64;
      BH := 0;
      DS := HiWord(L);
      DX := 0;
    end;
    if RealModeInt($21,R) then
      Result := P^.DiskSerNum
  finally
    GlobalDosFree(LoWord(L));
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  SerialNo := GetVolSerNo('C');
  Label1.Caption := Format('%04x-%04x', [HiWord(SerialNo), LoWord(SerialNo)]); { Only to make it look like the "dir" output }
end;

Enjoy!

/// John

0
 

Author Comment

by:pousticrousti
Comment Utility
Thank you very much for this super fast answer. I trust I can rely on it, and will implement it right now. It's worth 200 points for you !
Olivier de Saint Leger
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now