通过U盘方式加密(DELPHI版)

2012年04月05日 程序设计 暂无评论

实现原理:

 1.检查是否是U盘

 2.查检U盘的卷标

 3.通过VolumeLable的返回值对比判断是否你加密的U盘(卷标要定义好)

function CheckUsbDisk(FLable:string):Boolean;       //检验是否是USB优盘
var
  Drive: Integer;
  DriveLetter: Char;
begin
  Result :=false;
  for Drive := 2 to 25 do
  begin
   DriveLetter := Chr(Drive + ord('A'));
   if GetDrivetype(PChar(DriveLetter+':\'))= DRIVE_REMOVABLE then
   begin
     if VolumeID(DriveLetter)=FLable then
     begin
       Result :=true;
       exit;
     end;
   end;
  end;
end;

function VolumeLable(DriveChar: Char): string;       //检验USB优盘的卷标
var
  OldErrorMode: Integer;
  NotUsed, VolFlags: DWORD;
  Buf: array [0..MAX_PATH] of Char;
begin
  OldErrorMode := SetErrorMode(SEM_FAILCRITICALERRORS);
  try
    Buf[0] := #$00;
    if GetVolumeInformation(PChar(DriveChar + ':\'), Buf, DWORD(sizeof(Buf)),
      nil, NotUsed, VolFlags, nil, 0) then
      SetString(Result, Buf, StrLen(Buf))
    else Result := '';
    if DriveChar < 'a' then
      Result := AnsiUpperCaseFileName(Result)
    else
      Result := AnsiLowerCaseFileName(Result);
    Result := Format('%s',[Result]);
  finally
    SetErrorMode(OldErrorMode);
  end;
end;

给我留言

Copyright © 懒猫窝窝 保留所有权利.   主题设计 AB模板网

用户登录