
@echo off
rem *************** start of 'main'
set DEBUG=
if "%DEBUG%"=="" (set TRACE=echo) else (set TRACE=rem)
rem Note that right now there is a bug in tracerpt.exe cause of which you might want to use tracefmt.exe instead.
rem set the value if you want to use tracefmt.exe instead
set USE_TRACE_FMT=
%TRACE% The value of variable USE_TRACE_FMT is %USE_TRACE_FMT%
rem define variables for each of the switches we support
set SWHELP=h
set SWMODE=mode
set SWTRACELOG=tracelog
set SWMOF=mof
set SWOUTPUT=o
set VALID=
rem define variables for parameters to be passed to tracerpt
set TRACEDIR=%WINDIR%\system32\msdtc\trace
set TRACEFILE1=%TRACEDIR%\dtctrace.log
set TRACEFILE2=%TRACEDIR%\tracetx.log
set MOFFILE=%TRACEDIR%\msdtctr.mof
set ERRORFILE=%TRACEDIR%\errortrace.txt
set OUTPUTFILE=%TRACEDIR%\trace
rem Parse command line and setup variables
set CMDLINE=%*
%TRACE% About to call PARSECMDLINE with the argument %CMDLINE%
call :PARSECMDLINE
rem Validate the command line
%TRACE% About to call the procedure VALIDATE
call :VALIDATE
rem if Vaidation fails, we give up
if "%VALID%"=="" (
%TRACE% Parameter validation failed, exiting ...
goto :EOF
)
rem depending on the value of the mode, set the tracelogfile
call :MYFINDSWITCH %SWMODE%
if not "%RET%"=="" (
if "%RETV%"=="" set TRACEFILE=%TRACEFILE1%
if "%RETV%"=="" set TRACEFILE=%TRACEFILE2%
)
rem if the tracelog switch was used, set the tracelogfile
call :MYFINDSWITCH %SWTRACELOG%
if not "%RET%"=="" (
set TRACEFILE=%RETV%
)
rem if the mof switch was used, set the moffile
call :MYFINDSWITCH %SWMOF%
if not "%RET%"=="" (
set MOFFILE=%RETV%
)
rem if the output switch was used, set the output file
call :MYFINDSWITCH %SWOUTPUT%
if not "%RET%"=="" (
set OUTPUTFILE=%RETV%
)
%TRACE% TRACEFILE=%TRACEFILE%
%TRACE% MOFFILE=%MOFFILE%
%TRACE% OUTPUTFILE=%OUTPUTFILE%
rem if the specified tracelogfile does not exist, display an error message and give up
if not exist %TRACEFILE% (
echo The tracelogfile %TRACEFILE% does not exist. exiting ...
call :HELP
goto :EOF
)
rem if the specified moffile does not exist, display an error message and give up
if not exist %MOFFILE% (
echo The moffile %MOFFILE% does not exist. exiting ...
call :HELP
goto :EOF
)
rem set a variable for output file with extension
set OUTPUTFILEWITHEXT=%OUTPUTFILE%.csv
%TRACE% The value of variable OUTPUTFILEWITHEXT=%OUTPUTFILEWITHEXT%
rem if the specified outputfile exists, ask if the user is ok with it being over-written.
rem if the user wants to continue, delete the old output file, else give up.
if exist %OUTPUTFILEWITHEXT% (
echo The file %OUTPUTFILEWITHEXT% already exists. You may press Control-C to terminate the batch file. Continuing the batch file will overwrite this file.
Pause
del %OUTPUTFILEWITHEXT% >nul >nul
)
rem if the old error file exists, delete it
if exist %ERRORFILE% (
del %ERRORFILE% >nul >nul
%TRACE% Deleted the file %ERRORFILE%
)
rem call the utility with the right arguments
%TRACE% About to call the utility tracerpt.exe ...
if "%USE_TRACE_FMT%"=="" (goto :USE_TRACEPRT_UTILITY) else (goto :USE_TRACEFMT_UTILITY)
:USE_TRACEPRT_UTILITY
%TRACE% Entered the USE_TRACEPRT_UTILITY block, about to call traceprt
tracerpt %TRACEFILE% -o %OUTPUTFILE% -mof %MOFFILE% > %ERRORFILE% >&
rem if output file does not exist, display an error message and give up
if not exist %OUTPUTFILEWITHEXT% (
%TRACE% The file %OUTPUTFILEWITHEXT% does not exist, therefore exiting ...
call :DISPLAY_ERROR_MESSAGE
goto :EOF
)
notepad %OUTPUTFILEWITHEXT%
goto :EOF
:USE_TRACEFMT_UTILITY
%TRACE% Entered the USE_TRACEFMT_UTILITY block, about to call tracefmt
tracefmt %TRACEFILE% -o %OUTPUTFILEWITHEXT% -tmf %MOFFILE% -nosummary > %ERRORFILE% >&
rem if output file does not exist, display an error message and give up
if not exist %OUTPUTFILEWITHEXT% (
%TRACE% The file %OUTPUTFILEWITHEXT% does not exist, therefore exiting ...
call :DISPLAY_ERROR_MESSAGE
goto :EOF
)
notepad %OUTPUTFILEWITHEXT%
goto :EOF
goto :EOF
rem *************** end of 'main'
rem *************** Procedures begin here ****************************
rem *************** start of procedure VALIDATE
:VALIDATE
set ARG=
set SWHELPFOUND=
set SWMODEFOUND=
set SWTRACELOGFOUND=
set SWMOFFOUND=
set SWOUTPUTFOUND=
set OUTNAMENAME=
rem If no arguments are used at all, don't perform any other validation, just display help and give up
if %CMDARGCOUNT% EQU if %CMDSWCOUNT% EQU (call :HELP) & (goto :EOF)
rem If not arguments are given, display help
if %CMDARGCOUNT% GTR goto ERROR_USED_ARGUMENTS_WITHOUT_SWITCHES
rem If the switch SWHELP is used anywhere, don't perform any other validation, just display help and give up
call :MYFINDSWITCH %SWHELP%
if not "%RET%"=="" (call :HELP) & (goto :EOF)
:SWLOOP
if %ARG% GTR %CMDSWCOUNT% goto :SWLOOPEND
call :GETSWITCH %ARG%
set MYSWITCH=%RET:~%
rem make sure no switch is used twice
if /i "%MYSWITCH%"=="%SWHELP%" (if "%SWHELPFOUND%"=="" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWHELPFOUND=))
if /i "%MYSWITCH%"=="%SWMODE%" (if "%SWMODEFOUND%"=="" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWMODEFOUND=))
if /i "%MYSWITCH%"=="%SWTRACELOG%" (if "%SWTRACELOGFOUND%"=="" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWTRACELOGFOUND=))
if /i "%MYSWITCH%"=="%SWMOF%" (if "%SWMOFFOUND%"=="" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWMOFFOUND=))
if /i "%MYSWITCH%"=="%SWOUTPUT%" (if "%SWOUTPUTFOUND%"=="" (goto ERROR_USED_SAME_SWITCH_TWICE) else (set SWOUTPUTFOUND=))
rem make sure that the switches mode and tracelog are not used simultaneously
if "%SWMODEFOUND%"=="" if "%SWTRACELOGFOUND%"=="" goto ERROR_USED_BOTH_MODE_AND_TRACELOG
rem make sure that there is no switch outside our list
if /i not "%MYSWITCH%"=="%SWHELP%" (
if /i not "%MYSWITCH%"=="%SWMODE%" (
if /i not "%MYSWITCH%"=="%SWTRACELOG%" (
if /i not "%MYSWITCH%"=="%SWMOF%" (
if /i not "%MYSWITCH%"=="%SWOUTPUT%" (
(echo Invalid Switch "%RET%") & (call :HELP) & (goto :EOF) )))))
set /a ARG+=
goto :SWLOOP
:SWLOOPEND
rem make sure that either the switch "-mode" or "-tracelog" was used
if "%SWMODEFOUND%"=="" if "%SWTRACELOGFOUND%"=="" (echo Invalid Usage : neither "-%SWMODE%" nor "-%SWTRACELOG%" was specified) & (call :HELP) & (goto :EOF)
rem make sure that the value of the mode entered is valid
call :MYFINDSWITCH %SWMODE%
if not "%RET%"=="" if not "%RETV%"=="" if not "%RETV%"=="" goto ERROR_INVALID_MODE
rem make sure that the value of the outputfile entered does not have any extension
call :MYFINDSWITCH %SWOUTPUT%
for /f "tokens=1* delims=." %%I in ("%RETV%") do (set OUTPUTEXT=%%J)
if not "%OUTPUTEXT%"=="" goto ERROR_USED_OUTPUTFILENAME_WITH_EXTENSION
rem if we have come this far, everything went well, set the valid flag
set VALID=
goto :EOF
:ERROR_USED_SAME_SWITCH_TWICE
(echo Invalid Usage : use the switch %RET% multiple times) & (call :HELP) & (goto :EOF)
:ERROR_USED_BOTH_MODE_AND_TRACELOG
(echo Invalid Usage : cannot use both "-%SWMODE%" and "-%SWTRACELOG%" at the same time) & (call :HELP) & (goto :EOF)
:ERROR_USED_ARGUMENTS_WITHOUT_SWITCHES
call :GETARG
echo Invalid Usage : "%RET%" used without any switch
call :HELP
goto :EOF
:ERROR_INVALID_MODE
(echo Invalid Usage : Valid values for %SWMODE% are and ) & (call :HELP) & (goto :EOF)
:ERROR_USED_OUTPUTFILENAME_WITH_EXTENSION
(echo Invalid Usage : Output filename should not have any extension) & (call :HELP) & (goto :EOF)
rem *************** end of procedure VALIDATE
rem *************** start of procedure HELP
:HELP
echo Usage
echo "msdtcvtr { -MODE {1 | 2} | -tracelog tracelogfilename } [options]"
echo "All switches can be prefixed with either '-' or '/'"
echo Parameters:
echo "-MODE 1 to view background tracing"
echo "-MODE 2 to view tracing generated by ui"
echo "-tracelog <file> binary Trace log file name"
echo Options:
echo "-h OR -? Display Help"
echo "-o <filename> Output Filename without extension"
echo "-mof <filename> Mof Filename"
goto :EOF
rem *************** end of procedure HELP
rem *************** start of procedure DISPLAY_ERROR_MESSAGE
:DISPLAY_ERROR_MESSAGE
echo Failed to convert the binary trace data to text format.
echo Following reasons can cause this to happen:
echo ) The utility TraceFmt.exe is missing
echo ) The file %TRACEFILE% is either missing or corrupted
echo ) The file %MOFFILE% is either missing or corrupted
echo The exact error message can be found in the file '%ERRORFILE%'
goto :EOF
rem *************** end of procedure DISPLAY_ERROR_MESSAGE
rem /////////////////////////////////////////////////////////////////////////
rem INIT procedure
rem Must be called in local state before other procs are used
rem
:INIT
%TRACE% [proc % %*]
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem VARDEL procedure
rem Delete multiple variables by prefix
rem
rem Arguments: %=variable name prefix
rem
:VARDEL
%TRACE% [proc % %*]
for /f "tokens=1 delims==" %%I in ('set %1 2^>nul') do set %%I=
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem PARSECMDLINE procedure
rem Parse a command line into switches and args
rem
rem Arguments: CMDLINE=command text to parse
rem %= for new parse (def) or to append to existing
rem
rem Returns: CMDARG_n=arguments, CMDSW_n=switches
rem CMDARGCOUNT=arg count, CMDSWCOUNT=switch count
rem RET=total number of args processed
rem
:PARSECMDLINE
%TRACE% [proc % %*]
if not {%}=={} (
(call :VARDEL CMDARG_)
(call :VARDEL CMDSW_)
(set /a CMDARGCOUNT=)
(set /a CMDSWCOUNT=)
)
set /a RET=
call :PARSECMDLINE1 %CMDLINE% >nul
set _MTPLIB_T1=
set _LASTARGSWITCH=
set _LASTARGSWITCHNAME=
goto :EOF
:PARSECMDLINE1
if {%}=={} goto :EOF
set _MTPLIB_T1=%
set _MTPLIB_T1=%_MTPLIB_T1:"=%
set /a RET+=
shift /
if "%_MTPLIB_T1:~0,1%"=="/" goto :PARSECMDLINESW
if "%_MTPLIB_T1:~0,1%"=="-" goto :PARSECMDLINESW
if "%_LASTARGSWITCH%"=="" (
set CMDSW_%CMDSWCOUNT%=%_LASTARGSWITCHNAME%:%_MTPLIB_T1%
set _LASTARGSWITCH=
goto :PARSECMDLINE1
)
set /a CMDARGCOUNT+=
set CMDARG_%CMDARGCOUNT%=%_MTPLIB_T1%
set _LASTARGSWITCH=
goto :PARSECMDLINE1
:PARSECMDLINESW
set /a CMDSWCOUNT+=
set CMDSW_%CMDSWCOUNT%=%_MTPLIB_T1%
set _LASTARGSWITCH=
set _LASTARGSWITCHNAME=%_MTPLIB_T1%
goto :PARSECMDLINE1
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem GETARG procedure
rem Get a parsed argument by index
rem
rem Arguments: %=argument index (1st arg has index )
rem
rem Returns: RET=argument text or empty if no argument
rem
:GETARG
%TRACE% [proc % %*]
set RET=
if % GTR %CMDARGCOUNT% goto :EOF
if % EQU goto :EOF
if not defined CMDARG_% goto :EOF
set RET=%%CMDARG_%%%
call :RESOLVE
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem GETSWITCH procedure
rem Get a switch argument by index
rem
rem Arguments: %=switch index (1st switch has index )
rem
rem Returns: RET=switch text or empty if none
rem RETV=switch value (after colon char) or empty
rem
:GETSWITCH
%TRACE% [proc % %*]
(set RET=) & (set RETV=)
if % GTR %CMDSWCOUNT% goto :EOF
if % EQU goto :EOF
if not defined CMDSW_% goto :EOF
set RET=%%CMDSW_%%%
call :RESOLVE
for /f "tokens=1* delims=:" %%I in ("%RET%") do (set RET=%%I) & (set RETV=%%J)
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem FINDSWITCH procedure
rem Finds the index of the named switch
rem
rem Arguments: %=switch name
rem %=search start index (def: )
rem
rem Returns: RET=index ( if not found)
rem RETV=switch value (text after colon)
rem
:FINDSWITCH
%TRACE% [proc % %*]
if {%}=={} (set /a _MTPLIB_T4=) else (set /a _MTPLIB_T4=%)
:FINDSWITCHLOOP
call :GETSWITCH %_MTPLIB_T4%
if "%RET%"=="" (set RET=) & (goto :FINDSWITCHEND)
if /i "%RET%"=="%1" (set RET=%_MTPLIB_T4%) & (goto :FINDSWITCHEND)
set /a _MTPLIB_T4+=
goto :FINDSWITCHLOOP
:FINDSWITCHEND
set _MTPLIB_T4=
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem MYFINDSWITCH procedure
rem Finds the index of the named switch
rem
rem Arguments: %=switch name without the leading / or -
rem %=search start index (def: )
rem
rem Returns: RET=index ( if not found)
rem RETV=switch value (text after colon)
rem
:MYFINDSWITCH
%TRACE% [proc % %*]
if {%}=={} (set /a _MTPLIB_T4=) else (set /a _MTPLIB_T4=%)
:MYFINDSWITCHLOOP
call :GETSWITCH %_MTPLIB_T4%
if "%RET%"=="" (set RET=) & (goto :MYFINDSWITCHEND)
if /i "%RET:~1%"=="%1" (set RET=%_MTPLIB_T4%) & (goto :MYFINDSWITCHEND)
set /a _MTPLIB_T4+=
goto :MYFINDSWITCHLOOP
:MYFINDSWITCHEND
set _MTPLIB_T4=
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem REGSETM and REGSETU procedures
rem Set registry values from variables
rem
rem Arguments: %=reg context (usually script name)
rem %=variable to save (or prefix to save set of vars)
rem
:REGSETM
%TRACE% [proc % %*]
for /f "tokens=1* delims==" %%I in ('set %2 2^>nul') do call :REGSET1 HKLM % %%I "%%J"
goto :EOF
:REGSETU
%TRACE% [proc % %*]
for /f "tokens=1* delims==" %%I in ('set %2 2^>nul') do call :REGSET1 HKCU % %%I "%%J"
goto :EOF
:REGSET1
set _MTPLIB_T10=%
set _MTPLIB_T10=%_MTPLIB_T10:\=\\%
reg add %\Software\MTPScriptContexts\%\%=%_MTPLIB_T10% >nul
reg update %\Software\MTPScriptContexts\%\%=%_MTPLIB_T10% >nul
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem REGGETM and REGGETU procedures
rem Get registry value or values to variables
rem
rem Arguments: %=reg context (usually script name)
rem %=variable to restore (def: restore entire context)
rem
rem Returns: RET=value of last variable loaded
rem
rem WARNING: The "delims" value in the FOR commands below is a TAB
rem character, followed by a space. If this file is edited by
rem an editor which converts tabs to spaces, this procedure
rem will break!!!!!
rem
:REGGETM
%TRACE% [proc % %*]
for /f "delims= tokens=2*" %%I in ('reg query HKLM\Software\MTPScriptContexts\%1\%2 ^|find "REG_SZ"') do call :REGGETM1 %%I "%%J"
goto :EOF
:REGGETU
%TRACE% [proc % %*]
for /f "delims= tokens=2*" %%I in ('reg query HKCU\Software\MTPScriptContexts\%1\%2 ^|find "REG_SZ"') do call :REGGETM1 %%I "%%J"
goto :EOF
:REGGETM1
set _MTPLIB_T10=%
set _MTPLIB_T10=%_MTPLIB_T10:\\=\%
set _MTPLIB_T10=%_MTPLIB_T10:"=%
set %=%_MTPLIB_T10%
set RET=%_MTPLIB_T10%
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem REGDELM and REGDELU procedures
rem Delete registry values
rem
rem Arguments: %=reg context (usually script name)
rem %=variable to delete (def: delete entire context)
rem
:REGDELM
%TRACE% [proc % %*]
call :GETTEMPNAME
echo y >%RET%
reg delete HKLM\Software\MTPScriptContexts\%\% <%RET% >nul
del %RET%
goto :EOF
:REGDELU
%TRACE% [proc % %*]
call :GETTEMPNAME
echo y >%RET%
reg delete HKCU\Software\MTPScriptContexts\%\% <%RET% >nul
del %RET%
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem SRAND procedure
rem Seed the random number generator
rem
rem Arguments: %=new seed value
rem
:SRAND
%TRACE% [proc % %*]
set /a _MTPLIB_NEXTRAND=%
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem RAND procedure
rem Get next random number ( to )
rem
rem Returns: RET=next random number
rem
:RAND
%TRACE% [proc % %*]
if not defined _MTPLIB_NEXTRAND set /a _MTPLIB_NEXTRAND=
set /a _MTPLIB_NEXTRAND=_MTPLIB_NEXTRAND * +
set /a RET=_MTPLIB_NEXTRAND ^>^> ^& 0x7FFF
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem RESOLVE procedure
rem Fully resolve all indirect variable references in RET variable
rem
rem Arguments: RET=value to resolve
rem
rem Returns: RET=as passed in, with references resolved
rem
:RESOLVE
%TRACE% [proc % %*]
:RESOLVELOOP
if "%RET%"=="" goto :EOF
set RET1=%RET%
for /f "tokens=*" %%I in ('echo %RET%') do set RET=%%I
if not "%RET%"=="%RET1%" goto :RESOLVELOOP
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem GETINPUTLINE procedure
rem Get a single line of keyboard input
rem
rem Returns: RET=Entered line
rem
:GETINPUTLINE
%TRACE% [proc % %*]
call :GETTEMPNAME
set _MTPLIB_T1=%RET%
copy con "%_MTPLIB_T1%" >nul
for /f "tokens=*" %%I in ('type "%_MTPLIB_T1%"') do set RET=%%I
if exist "%_MTPLIB_T1%" del "%_MTPLIB_T1%"
set _MTPLIB_T1=
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem GETSYNCFILE procedure
rem Get a sync file name (file will not exist)
rem
rem Returns: RET=Name of sync file to use
rem
:GETSYNCFILE
%TRACE% [proc % %*]
call :GETTEMPNAME
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem SETSYNCFILE procedure
rem Flag sync event (creates the file)
rem
rem Arguments: %=sync filename to flag
rem
:SETSYNCFILE
%TRACE% [proc % %*]
echo . >%
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem DELSYNCFILE procedure
rem Delete sync file
rem
rem Arguments: %=sync filename
rem
:DELSYNCFILE
%TRACE% [proc % %*]
if exist % del %
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem WAITSYNCFILE
rem Wait for sync file to flag
rem
rem Arguments: %=sync filename
rem %=timeout in seconds (def: )
rem
rem Returns: RET=Timeout remaining, or if timeout
rem
:WAITSYNCFILE
%TRACE% [proc % %*]
if {%}=={} (set /a RET=) else (set /a RET=%)
if exist % goto :EOF
:WAITSYNCFILELOOP
sleep
set /a RET-=
if %RET% GTR if not exist % goto :WAITSYNCFILELOOP
goto :EOF
rem /////////////////////////////////////////////////////////////////////////
rem GETTEMPNAME procedure
rem Create a temporary file name
rem
rem Returns: RET=Temporary file name
rem
:GETTEMPNAME
%TRACE% [proc % %*]
if not defined _MTPLIB_NEXTTEMP set /a _MTPLIB_NEXTTEMP=
if defined TEMP (
(set RET=%TEMP%)
) else if defined TMP (
(set RET=%TMP%)
) else (set RET=%SystemRoot%)
:GETTEMPNAMELOOP
set /a _MTPLIB_NEXTTEMP=_MTPLIB_NEXTTEMP * +
set /a _MTPLIB_T1=_MTPLIB_NEXTTEMP ^>^> ^& 0x7FFF
set RET=%RET%\~SH%_MTPLIB_T1%.tmp
if exist "%RET%" goto :GETTEMPNAMELOOP
set _MTPLIB_T1=
goto :EOF
rem These must be the FINAL LINES in the script...
:DOSEXIT
echo This script requires Windows NT
rem /////////////////////////////////////////////////////////////////////////