Ver Mensaje Individual
  #16  
Antiguo 23-10-2016
Avatar de escafandra
[escafandra] escafandra is offline
Miembro Premium
 
Registrado: nov 2007
Posts: 2.197
Reputación: 20
escafandra Tiene un aura espectacularescafandra Tiene un aura espectacular
Cita:
Empezado por buenarquero Ver Mensaje
Intento corregir los errores que da el código al compilarlo, pero con mis conocimientos no lo consigo.
También deduzco revisando el código que el número de serie pretende obtenerlo del registro de Windows, ¿me equivoco?. Si es así, no es esto lo que pretendo, sino leerlo directamente del pendrive.
De todas formas gracias por vuestra aportación. A ver si hay alguien que pueda desfacer el entuerto.
Windows cuando instala un nuevo dispositivo USB guarda su número de serie en el registro y es por eso que el código lo busca en el registro de Windows.


El código de cHackAll es un poco antiguo. Lo he reformado para un Win10 64 bits en una unit de un proyecto simple de ejemplo compilado en delphi 7:
Código Delphi [-]
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Label1: TLabel;
    Edit1: TEdit;
    procedure Edit1KeyPress(Sender: TObject; var Key: Char);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

function  StrStrI(s1: PCHAR; s2: PCHAR): PCHAR; stdcall; external 'Shlwapi.dll' name 'StrStrIA';

var
  Form1: TForm1;

implementation
{$R *.dfm}
var
 Device: ShortString;
 ValueName: array [0..15] of Char = '\DosDevices\\:';


function Search(hParent: HKEY; var SubKey: ShortString): LongBool;
var
 hChild: HKEY;
 Index: Cardinal;
 Data: ShortString;
 Size: integer;
 ValueType: DWORD;
begin
 Result:= false;
 Index := 0;
 Size:= sizeof(Data);
 ValueType:=0;
 RegOpenKey(hParent, @SubKey[1], hChild);
 repeat
   RegEnumKey(hChild, Index, @SubKey[1], SizeOf(SubKey) - 1);
   Inc(Index);
 until StrStrI(@SubKey[1], @Device) <> nil;
 hParent:= hChild;
 RegOpenKey(hParent, @SubKey[1], hChild);
 Result:= (0 = RegEnumKey(hChild, 0, @SubKey[1], SizeOf(SubKey) - 1));
 RegCloseKey(hChild);
 RegCloseKey(hParent);
end;

function usbGetSerial(Drive: Char; var SerialNumber: ShortString): LongBool;
var
 lpSerialNumber: PChar;
 hKey: Windows.HKEY;
 Index: Integer;
 Value: Char;
 Size: DWORD;
 b: array[0..8024] of char;
 i: integer;
 ValueType: DWORD;
begin
 ValueType:= 3;
 Size:= SizeOf(Device);
 Result := False;
 ValueName[12] := Drive;
 i:= RegOpenKey(HKEY_LOCAL_MACHINE, 'SYSTEM\MountedDevices', hKey);
 RegQueryValueEx(hKey, @ValueName, nil, @ValueType{REG_BINARY}, @Device, @Size);
 RegCloseKey(hKey);

 Index := 0;
 repeat if Device[(Index + 3) * 2 + 54] <> '#' then
  Value := Device[Index * 2 + 54] else Value := #0;
  Device[Index] := Value;
  Inc(Index);
 until Value = #0;
 SerialNumber[0] := #0;
 lstrcpy(@SerialNumber[1], 'SYSTEM\CurrentControlSet\Enum\USBSTOR');
 if (Device[0] <> #0) and Search(HKEY_LOCAL_MACHINE, SerialNumber) then
  begin
   lpSerialNumber := @SerialNumber[1];
   repeat Inc(SerialNumber[0]);
    Inc(lpSerialNumber);
    if lpSerialNumber[0] = '&' then
     lpSerialNumber[0] := #0;
   until lpSerialNumber[0] = #0;
   Result := True;
  end;
end;

procedure TForm1.Edit1KeyPress(Sender: TObject; var Key: Char);
var
  SerialNumber: ShortString;
begin
  usbGetSerial(Key, SerialNumber);
  Label1.Caption:= SerialNumber;
end;

end.


Saludos.

Última edición por escafandra fecha: 23-10-2016 a las 03:35:34.
Responder Con Cita