在自己对句柄操作的理解基础上
编写了以下句柄操作模拟代码
大家可以在cmd中挂参数多次运行模拟
看是否符合实际句柄操作后的结果
那么如何判断一个句柄的指向呢
echo 1<&要判定的句柄号 is con
如果显示 con ,那么句柄值为con
否则为nul或者文件
- :: 句柄模拟.cmd - 模拟运算句柄操作后的句柄状态
- :: qzwqzw - 2009-6-12
- @echo off & setlocal EnableDelayedExpansion
- if [%1]==[] (
- echo. 这是句柄操作示例, 模拟运算进行几次句柄操作后的句柄状态
- echo --------------------- call %~n0 "1>&2" "3>nul" ---------------------
- call %0 "1>&2" "3>nul" >nul
- set 句柄
- echo.
- echo --------------------- call %~n0 "1>&2" "4>file1.txt" ---------------------
- call %0 "1>&2" "4>file1.txt" >nul
- set 句柄
- echo.
- echo --------------------- call %~n0 "1>&2" "5>con" ---------------------
- call %0 "1>&2" "5>con" >nul
- set 句柄
- echo.
- pause
- goto :eof
- )
- set "0=con" & set "1=con" & set "2=con"
- for %%h in (%句柄%) do set %%h
- echo.----- 起始状态 Initiation state -----
- for %%h in (0 1 2 3 4 5 6 7 8 9) do set/p=%%h=!%%h! <nul
- echo.
-
- echo.----- 预处理 Pre-process -----
- :loop
- if [%1]==[] goto :end
- echo.----- %1 -------
- set "_tmp=%~1"
- set _句柄=%_tmp:~0,1%
- set _句柄_2=
- set _dev=
- if "%_tmp:~2,1%"=="&" (set _句柄_2=%_tmp:~3%) else (set _dev=%_tmp:~2%)
- for %%h in (0 1 2 3 4 5 6 7 8 9) do (
- if "!%%h!"=="" (
- set/p=b: %_句柄%[!%_句柄%!]=^>%%h[!%%h!] : <nul
- set "%%h=!%_句柄%!"
- set bak_%%h=%_句柄%
- goto :set
- )
- )
- :set
- for %%h in (0 1 2 3 4 5 6 7 8 9) do set/p=%%h=!%%h! <nul
- echo.
- if defined _dev (
- set/p=a: %_dev%=^>%_句柄%[!%_句柄%!] : <nul
- set %_句柄%=%_dev%
- ) else if defined _句柄_2 (
- set/p=a: %_句柄_2%[!%_句柄_2%!]=^>%_句柄%[!%_句柄%!] : <nul
- set %_句柄%=!%_句柄_2%!
- )
- for %%h in (0 1 2 3 4 5 6 7 8 9) do set/p=%%h=!%%h! <nul
- echo.
- shift
- goto :loop
-
- :end
- echo.----- 后处理 Post-process -----
- for %%h in (0 1 2 3 4 5 6 7 8 9) do (
- if not "!bak_%%h!"=="" (
- set/p=r: !bak_%%h![]^<=%%h[!%%h!] : <nul
- set !bak_%%h!=!%%h!
- set %%h=
- set bak_%%h=
- for %%h in (0 1 2 3 4 5 6 7 8 9) do set/p=%%h=!%%h! <nul
- echo.
- )
- )
- set 句柄=
- for %%h in (0 1 2 3 4 5 6 7 8 9) do set 句柄=!句柄! "%%h=!%%h!"
- endlocal & set 句柄=%句柄%
- echo.----- 结束状态 End Status -----
- for %%h in (%句柄%) do set/p=%%~h <nul
- echo.
复制代码
[ 本帖最后由 qzwqzw 于 2009-6-14 08:02 编辑 ] |