Delphi实现Subst

本文介绍如何使用 Delphi 实现 Windows 的 subst 命令功能,包括创建、删除和查询虚拟驱动器映射。提供了完整的代码示例,并区分了不同平台下的实现细节。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Delphi实现Subst
2008-10-24 16:49
{2008-10-24 16:26 制作了等同于SUBST的:AskDriver('X:')NewDriver('X:','\??\d:');//如果对一个已经存在的设备进行这个操作,可能导致无法使用DelDriver('X:','\??\d:');U盘        \Device\Harddisk1\DP(1)0-0+7CDROM      \Device\CdRom0硬盘1分区1 \Device\HarddiskVolume1COM1       \Device\Serial0LPT1       \Device\Parallel0}unit Subst;interfaceuses windows;function AskDriver(Drv:string):string;function NewDriver(Drv:string; Map:string):boolean;function DelDriver(Drv:string; Map:string):boolean;implementation//DefineDosDevice 这个函数非常关键,是实现虚拟光驱和网络监测等技术的基础function AskDriver(Drv:string):string;var device : PChar;    temps : string;beginGetMem(device, 1024);FillChar(device, 0, 1024);QueryDosDevice(PChar(Drv),device,1024);temps:=device;FreeMem(device, 1024);Result:=temps;end;
function NewDriver(Drv:string; Map:string):boolean;beginResult:=DefineDosDevice(DDD_RAW_TARGET_PATH,PChar(Drv),PChar(Map));end;
function DelDriver(Drv:string; Map:string):boolean;beginResult:=DefineDosDevice(DDD_RAW_TARGET_PATH or DDD_REMOVE_DEFINITION or DDD_EXACT_MATCH_ON_REMOVE,PChar(Drv), PChar(Map));end;
end.
//========================================================================================
unit uSUBST; interface
uses
  SysUtils, Windows;

function SUBST_Create(ADrv: Char; const APath:
string): Boolean;
function SUBST_Remove(ADrv: Char): Boolean;
function
SUBST_Query(ADrv: Char): string;

implementation
{$WARN SYMBOL_PLATFORM OFF} //
屏蔽"平台相关"的警告信息
procedure VxDCall; external Kernel32 index 1;
{$WARN
SYMBOL_PLATFORM ON}  // 重新打开警告信息

function SUBST_Create(ADrv: Char; const APath:
string): Boolean;
var
  byDrvNo: Byte;
  szBuff: array[0..256] of Char;
  sPath: string;
begin
  // 指定的目录必须已经存在, 否则失败退出
  if not DirectoryExists(APath) then
  begin
    Result := False;
    Exit;
  end;

  // 假如路径为x:\yyy\, 则将路径转换成x:\yyy
  if (Length(APath) > 3) and (APath[Length(APath)] = '\') then
    sPath := Copy(APath, 1, Length(APath) - 1) // 假如路径为x:, 则将路径转换成x:\
  else

  if(APath[Length(APath)] = ':') then
    sPath := sPath + '\'
  else  // 否则, 路径保持原样不变
    sPath := APath;

  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    szBuff[0] := ADrv;
    szBuff[1] := ':';
    szBuff[2] := #0;
    Result := DefineDosDevice(0, szBuff, PChar(sPath));
  end
  else
begin
    sPath := ExtractShortPathName(sPath);
    if sPath <> '' then
    begin
      byDrvNo := Ord(UpperCase(ADrv)[1]) - Ord('A');
      StrPCopy(szBuff, sPath);
      asm    
        pushad
        push es
        xor  ebx, ebx
        mov  bh, 0
        mov  bl, byDrvNo
        lea  edx, szBuff
        push 0  //
        ECX(unused)
        push 71AAh
        push 2A0010h
        call VxDCall
        pop  es
        popad
      end;
    end;
  Result := (AnsiUpperCase(SUBST_Query(ADrv)) = AnsiUpperCase(sPath));
  end;
end;

function SUBST_Remove(ADrv: Char):
Boolean;
var
  byDrvNo: Byte;
  sDrive, sPath: string;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    SetLength(sDrive, 3);
    sDrive[1] := ADrv;
    sDrive[2] := ':';
    sDrive[3] := #0;
    sPath := SUBST_Query(ADrv);
    Result := DefineDosDevice(DDD_REMOVE_DEFINITION, PChar(sDrive), PChar(sPath));
  end
  else begin
    byDrvNo := Ord(UpperCase(ADrv)[1]) - Ord('A');
    asm
      pushad
      push es
      xor  ebx, ebx
      mov  bh, 1
      mov  bl, byDrvNo
      push 0  // ecx(unused)
      push 71AAh
      push 2A00A0h
      call VxDCall
      pop  es
      popad
    end;
    Result := SUBST_Query(ADrv) = '';
  end;
end;

function SUBST_Query(ADrv: Char):
string;
var
  byDrvNo: Byte;  // 盘符ADrv所对应的驱动器序号
  szBuff:
array[0..256] of Char;
  szPath: array[0..256] of Char;
begin
  if Win32Platform = VER_PLATFORM_WIN32_NT then
  begin
    szBuff[0] := ADrv;
    szBuff[1] := ':';
    szBuff[2] := #0;
    szPath[0] := #0;
    QueryDosDevice(szBuff, szPath, 256);
    Result := StrPas(szPath);
    if Copy(Result, 1, 4) = '\??\' then
      Result := Copy(Result, 5, Length(Result))
    else
      Result := '';
  end

else begin
    byDrvNo := Ord(UpperCase(ADrv)[1]) - Ord('A');
  
szPath[0] := #0;
    asm //获得subst驱动器对应的文件夹的路径
      pushad
      push es
      xor  ebx, ebx
      mov  bh, 2
      mov  bl, byDrvNo
      lea  edx, szPath
      push 0  // ecx(unused)
      push 71AAh
      push 2A0010h
      call VxDCall
      pop  es
      popad
    end;
    Result := StrPas(szPath);
    if Result = '' then Exit;
    asm // 将获得的路径转换成长路径形式
      pushad
      push ds
      push es
      xor  ebx, ebx
      lea  esi, szPath
      lea  edi, szBuff
      mov  ecx, 0
      mov  cl, 2
      mov  ch, 0
      push ecx
      push 7160h
      push 2A0010H
      call VxDCall
      pop  es
      pop  ds
      popad
    end;
    Result := StrPas(szBuff);
end;
end;

end.



        
        



        







        
          
            
            评论这张
          
        


          
            
               Delphi实现Subst - yyimen - yyimen的博客
            
            转发至微博
          
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值