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.
评论这张
转发至微博
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;
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
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.
评论这张

转发至微博