Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Convert ASCII to Unicode or vice versa?

May
572
4
Does anyone have a way to convert ASCII strings to Unicode or vice versa within TCC?

Reason, probably use: Given a scheduled task running TCC, I'd like to find the name of the scheduled task which started the process. The commands within the scheduled task .JOB files are in Unicode, but I'd like to use FFIND to locate the .JOB file containing the name of the running batch file.
 
On Sat, 14 Mar 2009 00:16:40 -0500, dcantor <> wrote:

|Does anyone have a way to convert ASCII strings to Unicode or vice versa within TCC?
|
|Reason, probably use: Given a scheduled task running TCC, I'd like to find the name of the scheduled task which started the process. The commands within the scheduled task .JOB files are in Unicode, but I'd like to use FFIND to locate the .JOB file containing the name of the running batch file.

I don't think there's anything that will make it easy. Strictly speaking jog
files are not Unicode (no byte-order mark and 72 bytes of non-alpha data at the
front) so TCC won't recognize them as Unicode.

With batch routine you could reconstruct the strings by starting at offset 72
and reading 2 bytes at a time. Here's a rough example:

v:\> set h=%@fileopen[ipcheck.job,r,b]

v:\> echo %@fileseek[%h,72,0]
72

v:\> for /l %i in (0,1,60) echos %@fileread[%h,2]
D:\4NT\4nt.exe?/cd:\uty\ipcheck.batD:\4NTvefatica

The exe name is followed by a NUL character (might end a DO loop) then an 0x16
(?). And the args are followed by another NUL character (then 0x07?).
--
- Vince
 
On Sat, 14 Mar 2009 00:16:40 -0500, dcantor <> wrote:

|Does anyone have a way to convert ASCII strings to Unicode or vice versa within TCC?
|
|Reason, probably use: Given a scheduled task running TCC, I'd like to find the name of the scheduled task which started the process. The commands within the scheduled task .JOB files are in Unicode, but I'd like to use FFIND to locate the .JOB file containing the name of the running batch file.

Here's a little tidbit (readjob.btm, far below); it could be much simpler but I
was having so much fun that I got carried away. First, it's output:

v:\> readjob.btm IPcheck.job
ExeName: D:\4NT\4nt.exe
Parameters: /c d:\uty\ipcheck.bat
Directory: D:\4NT
UserName: vefatica

REM READJOB.BTM
if not exist %1 .or. "%@ext[%1]" NE "JOB" quit
setlocal
set /a nBytesNeeded=%@filesize[%1] - 72
set hMem=%@balloc[%nBytesNeeded]
set hFile=%@fileopen[%1,r,b]
set NulCount=0
unset /q String

set nBytesRead=%@bread[%hMem,0,%hFile,72,%nBytesNeeded]

do i=0 to %nBytesRead by 2
set nUnicode=%@bpeek[%hMem,%i,2]
iff %nUnicode == 0 then
set /a NulCount+=1
switch %NulCount
case 1
echos ExeName
case 2
echos Parameters
case 3
echos Directory
case 4
echos UserName
endswitch
echo :^t%String
unset /q String
if %NulCount GE 4 leave
elseiff %nUnicode GT 32 then
set String=%[String]%@char[%nUnicode]
elseiff %nUnicode == 32 then
set String=%[String]` `
endiff
enddo

set hFile=%@fileclose[%hFile]
set hMem=%@bfree[%hMem]
endlocal

I'm no expert on the format of JOB files but this tests OK with the few examples
I have hanging around.
--
- Vince
 
Does anyone have a way to convert ASCII strings to Unicode or vice versa within TCC?

Reason, probably use: Given a scheduled task running TCC, I'd like to find the name of the scheduled task which started the process. The commands within the scheduled task .JOB files are in Unicode, but I'd like to use FFIND to locate the .JOB file containing the name of the running batch file.

Perhaps, instead of examining the .JOB files yourself, you could parse the output of SCHTASKS /QUERY /FO:LIST /V or the like.
 
Vince, that's pretty clever. Thanks.

Charles, I suspect that's what I'll have to do. I was hoping to avoid creating the temporary file needed for that (I'll use /FO CSV, probably), but it will work.

Rex, would you consider (for some future version) conversion functions to go from ASCII to Unicode (and maybe vice versa), and also some kind of modifier to FFIND to allow searching for Unicode strings?
 
On Sat, 14 Mar 2009 00:16:40 -0500, dcantor <> wrote:

...
Here's a little tidbit (readjob.btm, far below); it could be much simpler but I
was having so much fun that I got carried away. First, it's output:


Vince,

I noticed that there is 2-byte count before each Unicode string, so I cleaned it up just a little, and added the comment field. I also added some "defaults" for the directory name and extension of the target file name.

Code:
REM READJOB.BTM  -- Original by Vince Fatica
rem        revised by Dave Cantor
set jobfile=%@unquote[%1]
if not defined jobfile quit
if "%@path[%jobfile]" eq "" set jobfile=%systemroot%\tasks\%jobfile
if "%@ext[%jobfile]" eq "" set jobfile=%jobfile%.job
if not exist "%jobfile" quit
dire "%jobfile"

setlocal
set nBytesNeeded=%@filesize["%jobfile"]
set hMem=%@balloc[%nBytesNeeded]
set hFile=%@fileopen["%jobfile",r,b]
set NulCount=0
unset /q String

set nBytesRead=%@bread[%hMem,0,%hFile,0,%nBytesNeeded]

set bufpos=70

do ii = 1 to 5
  set len=%@bpeek[%hMem,%bufpos,2]
  set bufpos=%@eval[%bufpos + 2]
  set str=%@bpeekstr[%hMem,%bufpos,u,%len]
  set bufpos=%@eval[%bufpos + %len + %len]
  echo %@word[%ii,x Exename Parameters Directory Username Comment ]:^t%str
 enddo

:done
set hFile=%@fileclose[%hFile]
set hMem=%@bfree[%hMem]
endlocal
Result:

Code:
> readjob "task watch" 
Volume in drive C is IRVING-C       Serial number is 28aa:b2d9 
Directory of  C:\WINDOWS\tasks\task watch.job
03-14-2009  18:59             516  Task Watch.job               
   516 bytes in 1 file and 0 dirs 
   4,096 bytes allocated    11,756,498,944 bytes free
Exename:        C:\Program Files\JPSoft\TCMD10\tcc.exe
Parameters:     /C call c:\work\TASKWATCH1.BAT min
Directory:      C:\WORK
Username:       DCantor
Comment:        Enable / disable standard tasks depending on whether logged in or out.
 
On Sat, 14 Mar 2009 18:55:20 -0500, dcantor <> wrote:

|I noticed that there is 2-byte count before each Unicode string, so I cleaned it up just a little, and added the comment field. I also added some "defaults" for the directory name and extension of the target file name.

Nice catch! I didn't dwell on what those might be (should have since offsets of
the next string are often seen in Win32 API structures). Knowing the string
lengths makes it a lot neater.
--
- Vince
 

Similar threads

Back
Top