You are not logged in.

#1 02 Apr 2019 14:29

PiotrMP006
Member
Registered: 02 Apr 2019
Posts: 12

How to check console window width columns in batch script?

Hi


How to check console window width columns in bath script from registry?

HKCU\Console

WindowSize  = 1638480

Columns=80
Rows=25

How to calculate?

Offline

#2 02 Apr 2019 17:12

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: How to check console window width columns in batch script?

Are you asking how the "WindowSize" figure was calculated from the Columns/Rows?

Playing around with the numbers, I managed to reverse engineer them into this:

((80*25*1024*8)/10)+80 = 1638480

But that was dumb luck, and doesn't hold up for other values (it also makes no sense) so you should ignore it big_smile

Looks like this is the operation they are doing:

set rows=25
set columns=80
set /a windowsize="(rows<<16)+columns"

Last edited by bluesxman (02 Apr 2019 17:45)


cmd | *sh | ruby | chef

Offline

#3 02 Apr 2019 17:26

PiotrMP006
Member
Registered: 02 Apr 2019
Posts: 12

Re: How to check console window width columns in batch script?

How to read from registry console columns value?

WindowSize  = 1638480

Offline

#4 02 Apr 2019 17:48

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: How to check console window width columns in batch script?

reg query hkcu\console /v WindowSize

And to get the value as variable (NB this needs to be run in a script):

@echo off
for /f "usebackq tokens=3" %%a in (`reg query hkcu\console /v WindowSize`) do set /a windowsize=%%a

Turning this into your figures...

set /a "rows=(windowsize >> 16)"

set /a "cols=windowsize - (rows << 16)"

Last edited by bluesxman (02 Apr 2019 17:51)


cmd | *sh | ruby | chef

Offline

#5 03 Apr 2019 12:34

PiotrMP006
Member
Registered: 02 Apr 2019
Posts: 12

Re: How to check console window width columns in batch script?

Why rows=(windowsize >> 16 ?

Offline

#6 03 Apr 2019 14:05

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: How to check console window width columns in batch script?

Because that's the way to reverse the bitwise shift they've done under the hood to encode the "80" and "25" in a single value.

Here's what I get when I run the code by hand:

D:\Users\bxm>for /f "usebackq tokens=3" %a in (`reg query hkcu\console /v WindowSize`) do set /a windowsize=%a

D:\Users\bxm>set /a windowsize=0x190050
1638480
D:\Users\bxm>set /a "rows=(windowsize >> 16)"
25
D:\Users\bxm>set /a "cols=windowsize - (rows << 16)"
80

Breaking out the intermediate step because maybe that'll make it clearer:

D:\Users\bxm>set /a "rows_shift=rows << 16"
1638400
D:\Users\bxm>set /a "cols=windowsize - rows_shift"
80

I recommend you read up on Bitwise shifts if you want to understand the underlying process.

Last edited by bluesxman (03 Apr 2019 14:05)


cmd | *sh | ruby | chef

Offline

#7 03 Apr 2019 14:22

PiotrMP006
Member
Registered: 02 Apr 2019
Posts: 12

Re: How to check console window width columns in batch script?

OK but why 16 ?

Offline

#8 04 Apr 2019 14:03

bluesxman
Member
From: UK
Registered: 29 Dec 2006
Posts: 1,129

Re: How to check console window width columns in batch script?

Because they're wanting to store two numbers within a single 32 bit DWORD.  So they store one number in the first 16 bits, and the other in the remaining 16 bits.

25 (dec) = 0000 0000 0001 1001 (bin)
shifting it "left" by 16 bits gives:
0000 0000 0001 1001  0000 0000 0000 0000 (bin) = 1638400 (dec)
adding the columns value to this gives 1638480
1638480 (dec) = 0000 0000 0001 1001  0000 0000 0101 0000 (bin)

rows                 columns
0000 0000 0001 1001  0000 0000 0101 0000

So... you need to bit shift by 16 to get the right part of the DWORD.

Last edited by bluesxman (04 Apr 2019 14:04)


cmd | *sh | ruby | chef

Offline

Board footer

Powered by