flat assembler
Message board for the users of flat assembler.

Index > Windows > Why WriteConsoleA can but not WriteFile?

Author
Thread Post new topic Reply to topic
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate 19 Feb 2022, 18:47
I thought I am going to publish my hexdump for Win32 on here, but I face an issue with WriteFile. It didn't print each single characters ("_short"), hexadecimal offset and hexadecimal values, however, are OK (printed on screen and in file).

WriteConsoleA has no issue with printing each single character.

The reason I prefer WriteFile over WriteConsoleA because it can redirect screen output to file.

Code:
PrintChar:      
        mov     edx, 1
        lea     esi, [_short]
        call    Print
        ret
Print:
        ;invoke  WriteConsole, dword [_stdout], esi, edx, _dummy, 0
        invoke  WriteFile, dword [_stdout], esi, edx, _dummy, 0
        ret              


Do I need to include the whole hexdump for Win32 source file?


Description: WriteConsoleA output
Filesize: 36.62 KB
Viewed: 5627 Time(s)

WriteConsoleA.png


Description: WriteFile output
Filesize: 32.08 KB
Viewed: 5627 Time(s)

WriteFile.png


Post 19 Feb 2022, 18:47
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 949
Location: Russia
macomics 20 Feb 2022, 01:24
For Windows and without such utilities, there are plenty of professional hexedits that allow you to edit disks at the data level, process memory, file system images from individual files with file tree parsing and containing built-in languages for describing data structures for their research and element-by-element editing (see WinHex, etc).

add:
https://docs.microsoft.com/en-us/windows/console/writeconsole wrote:
Remarks:
. . .
WriteConsole fails if it is used with a standard handle that is redirected to a file. If an application processes multilingual output that can be redirected, determine whether the output handle is a console handle (one method is to call the GetConsoleMode function and check whether it succeeds). If the handle is a console handle, call WriteConsole. If the handle is not a console handle, the output is redirected and you should call WriteFile to perform the I/O. Be sure to prefix a Unicode plain text file with a byte order mark. For more information, see Using Byte Order Marks.


Description: Usage: fasm [-m kb] hdmp.asm -d ifile="<file name>" [-d start=N] [-d count=N] [<output name>.TXT]
Download
Filename: hdmp.asm
Filesize: 1.68 KB
Downloaded: 299 Time(s)

Post 20 Feb 2022, 01:24
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate 20 Feb 2022, 06:52
Nice hdmp.asm, yours is only 80 lines with macros, mine is about 260 lines with Win32 API..... hmm.

Anyway, I have sorted out my issue, the "_dummy" in my post #1 should be DWORD, not WORD size.


Description: HEXDUMP for Windows
Download
Filename: HEXDUMPW.asm
Filesize: 6.31 KB
Downloaded: 319 Time(s)

Post 20 Feb 2022, 06:52
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 949
Location: Russia
macomics 20 Feb 2022, 07:29
Code:
_dummy    dd ? ; not dw!!!     
It works fine with dd.
Post 20 Feb 2022, 07:29
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate 20 Feb 2022, 11:17
macomics wrote:
Code:
_dummy    dd ? ; not dw!!!     
It works fine with dd.


Yes, thanks for helping to test.

I compare your hdmp.asm with my hexdumpw.exe, a certain file in %windir%\system32 cannot be opened.

Code:
C:\Users\Administrator\Downloads>\fasmw\fasm -d ifile="\windows\system32\x_80.png" hdmp.asm
flat assembler  version 1.73.29  (1764181 kilobytes memory)
hdmp.asm [36]:
        file name:offset,readed
hdmp.asm [36] match [1]:
        file name:offset,readed
processed: file '\windows\system32\x_80.png':offset,readed
error: file not found.
    


Code:
C:\Windows\System32>hexdumpw x_80.png
INVALID_HANDLE_VALUE
    


Do you know what's wrong?
Post 20 Feb 2022, 11:17
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate 20 Feb 2022, 11:23
@macomics, something unusual about hdmp.asm in opening a test file.

See below (x_80.png is 627 bytes, or 0x273 long)

EDIT: Looks like I need to use "-d count=n" parameter. Sorry.

Code:
C:\FASMW>fasm -d ifile="\fasmw\x_80.png" c:\users\administrator\downloads\hdmp.asm
flat assembler  version 1.73.29  (1354534 kilobytes memory)
0000000000000000  89 50 4E 47  0D 0A 1A 0A  00 00 00 0D  49 48 44 52  ëPNG........IHDR
0000000000000010  00 00 00 50  00 00 00 50  08 06 00 00  00 8E 11 F2  ...P...P.....Ä.≥
0000000000000020  AD 00 00 00  04 73 42 49  54 08 08 08  08 7C 08 64  ¡....sBIT....|.d
0000000000000030  88 00 00 02  2A 49 44 41  54 78 9C ED  DC 41 6E C2  ê...*IDATx£φ▄An┬
0000000000000040  30 10 85 E1  49 AF 91 4A  80 C4 35 2A  F5 EE 1C A4  0.àßI»æJÇ─5*⌡ε.ñ
0000000000000050  A0 22 71 8C  D7 05 19 C9  85 84 4C 88  1D 8F ED F7  á"qî╫..╔àäLê.Åφ≈
0000000000000060  4B DD 94 12  8F BF A6 12  8B D4 22 8C  31 C6 18 63  K▌ö.Å┐ª.ï╘"î1╞.c
0000000000000070  8C B1 F4 01  E8 01 1C 72  CF 11 3B 00  07 00 7D EA  î▒⌠.Φ..r╧.;...}Ω
0000000000000080  45 7A 00 17  00 BF 00 F6  49 17 DB 30  00 FB 61 4F  Ez...┐.÷I.█0.√aO
0000000000000090  97 64 88 00  76 C3 02 5A  15 88 01 9E  76 01 B0 8B  ùdê.v├.Z.ê.₧v.░ï
00000000000000A0  BD C8 0E C0  15 CF 15 8D  38 82 A7 5D  AD 88 1F C6  ╜╚.└.╧.ì8éº]¡ê.╞
00000000000000B0  B5 3A 11 C1  C8 F7 3F 45  E4 54 22 E2  30 F3 49 EE  ╡:.┴╚≈?EΣT"Γ0≤Iε
00000000000000C0  7B 78 7A 59  EE 7B 8E BB  E0 C4 6F AB  B8 3B 31 DB  {xzYε{Ä╗α─o½╕;1█
00000000000000D0  5E 6A 40 CC  BE 87 EC 03  AC C8 CD EC  6E 06 59 90  ^j@╠╛ç∞.¼╚═∞n.YÉ
00000000000000E0  BB 99 DD 0D  F4 22 B7 B3  BA 1D AC A4  19 3D 0F E8  ╗Ö▌.⌠"╖│║.¼ñ.=00000000000000F0  79 B6 7F 79  1C D4 E3 4C  2F 33 0C 3C  F6 81 35 D7  y╢⌂y.╘πL/3.<÷ü5╫
0000000000000100  2C BE F0 B4  99 C1 CF 5B  20 16 8B A7  E5 44 2C 1E  ,╛≡┤Ö┴╧[ .ïºσD,.
0000000000000110  4F CB 81 58  0D 9E 36 6C  E8 B6 05 E2  0C DE AD 38  O╦üX.₧6lΦ╢.Γ.▐¡8
0000000000000120  3C 0D C0 31  35 A2 01 EF  18 63 2F D9  4A 89 58 3D  <.└15ó.∩.c/┘JëX=
0000000000000130  9E 96 02 B1  19 3C 2D 26  62 73 78 5A  0C C4 66 F1  ₧û.▒.<-&bsxZ.─f±
0000000000000140  B4 35 88 CD  E3 69 EF 20  12 EF A1 25  88 C4 9B C8  ┤5ê═πi∩ .∩í%ê─¢╚
0000000000000150  82 48 BC 99  66 10 7F 88  67 68 06 91  78 96 16 20  éH╝Öf.⌂êgh.æxû.
0000000000000160  12 6F 2A 03  A2 3B 3C EB  A3 1D CC 7B  FC 13 5E D1  .o*;<δú.╠{ⁿ.^╤
0000000000000170  02 3C 22 3E  36 83 77 1E  BE 88 38 96  01 6F 0F 7E  .<">6âw.╛ê8û.o.~
0000000000000180  90 1E CF 82  17 FC 2C 11  C3 96 E0 05  EF 21 A2 C8  É.╧é.ⁿ,.├ûα.∩!ó╚
0000000000000190  7B 78 C1 7B  DB 46 5C 83  17 5C A3 4D  C4 18 78 C1  {x┴{█F\â.\úM─.x┴
00000000000001A0  B5 DA 42 8C  89 17 5C B3  0D C4 14 78  C1 B5 EB 46  ╡┌Bîë.\│.─.x┴╡δF
00000000000001B0  4C 89 17 AC  51 27 22 E6  1F ED 88 F6  B8 85 01 B1  Lë.¼Q'"µ.φê÷╕à.▒
00000000000001C0  AC 47 3B 66  36 14 15 CF  B8 66 39 0F  17 E5 C0 33  ¼G;f6..╧╕f9..σ└3
00000000000001D0  AE ED 1F 31  27 9E 71 06  BF 88 1E F0  8C B3 F8 43  «φ.1'₧q.┐ê.≡î│°C
00000000000001E0  F4 84 67 9C  C9 0F A2 47  3C E3 6C F9  11 3D E3 19  ⌠äg£╔.óG<πl∙.=π.
00000000000001F0  67 74 FB 5F  4A 2E F0 34  77 88 25 E1  69 6E 10 4B  gt√_J.≡4wê%ßin.K
1 passes, 2.6 seconds, 2816 bytes.
    


Code:
C:\FASMW>hexdumpw x_80.png
00000000  89 50 4E 47 0D 0A 1A 0A 00 00 00 0D 49 48 44 52   .PNG........IHDR
00000010  00 00 00 50 00 00 00 50 08 06 00 00 00 8E 11 F2   ...P...P........
00000020  AD 00 00 00 04 73 42 49 54 08 08 08 08 7C 08 64   .....sBIT....|.d
00000030  88 00 00 02 2A 49 44 41 54 78 9C ED DC 41 6E C2   ....*IDATx...An.
00000040  30 10 85 E1 49 AF 91 4A 80 C4 35 2A F5 EE 1C A4   0...I..J..5*....
00000050  A0 22 71 8C D7 05 19 C9 85 84 4C 88 1D 8F ED F7   ."q.......L.....
00000060  4B DD 94 12 8F BF A6 12 8B D4 22 8C 31 C6 18 63   K.........".1..c
00000070  8C B1 F4 01 E8 01 1C 72 CF 11 3B 00 07 00 7D EA   .......r..;...}.
00000080  45 7A 00 17 00 BF 00 F6 49 17 DB 30 00 FB 61 4F   Ez......I..0..aO
00000090  97 64 88 00 76 C3 02 5A 15 88 01 9E 76 01 B0 8B   .d..v..Z....v...
000000A0  BD C8 0E C0 15 CF 15 8D 38 82 A7 5D AD 88 1F C6   ........8..]....
000000B0  B5 3A 11 C1 C8 F7 3F 45 E4 54 22 E2 30 F3 49 EE   .:....?E.T".0.I.
000000C0  7B 78 7A 59 EE 7B 8E BB E0 C4 6F AB B8 3B 31 DB   {xzY.{....o..;1.
000000D0  5E 6A 40 CC BE 87 EC 03 AC C8 CD EC 6E 06 59 90   ^j@.........n.Y.
000000E0  BB 99 DD 0D F4 22 B7 B3 BA 1D AC A4 19 3D 0F E8   .....".......=..
000000F0  79 B6 7F 79 1C D4 E3 4C 2F 33 0C 3C F6 81 35 D7   y..y...L/3.<..5.
00000100  2C BE F0 B4 99 C1 CF 5B 20 16 8B A7 E5 44 2C 1E   ,......[ ....D,.
00000110  4F CB 81 58 0D 9E 36 6C E8 B6 05 E2 0C DE AD 38   O..X..6l.......8
00000120  3C 0D C0 31 35 A2 01 EF 18 63 2F D9 4A 89 58 3D   <..15....c/.J.X=
00000130  9E 96 02 B1 19 3C 2D 26 62 73 78 5A 0C C4 66 F1   .....<-&bsxZ..f.
00000140  B4 35 88 CD E3 69 EF 20 12 EF A1 25 88 C4 9B C8   .5...i. ...%....
00000150  82 48 BC 99 66 10 7F 88 67 68 06 91 78 96 16 20   .H..f...gh..x..
00000160  12 6F 2A 03 A2 3B 3C EB A3 1D CC 7B FC 13 5E D1   .o*..;<....{..^.
00000170  02 3C 22 3E 36 83 77 1E BE 88 38 96 01 6F 0F 7E   .<">6.w...8..o.~
00000180  90 1E CF 82 17 FC 2C 11 C3 96 E0 05 EF 21 A2 C8   ......,......!..
00000190  7B 78 C1 7B DB 46 5C 83 17 5C A3 4D C4 18 78 C1   {x.{.F\..\.M..x.
000001A0  B5 DA 42 8C 89 17 5C B3 0D C4 14 78 C1 B5 EB 46   ..B...\....x...F
000001B0  4C 89 17 AC 51 27 22 E6 1F ED 88 F6 B8 85 01 B1   L...Q'".........
000001C0  AC 47 3B 66 36 14 15 CF B8 66 39 0F 17 E5 C0 33   .G;f6....f9....3
000001D0  AE ED 1F 31 27 9E 71 06 BF 88 1E F0 8C B3 F8 43   ...1'.q........C
000001E0  F4 84 67 9C C9 0F A2 47 3C E3 6C F9 11 3D E3 19   ..g....G<.l..=..
000001F0  67 74 FB 5F 4A 2E F0 34 77 88 25 E1 69 6E 10 4B   gt._J..4w.%.in.K
00000200  C4 D3 B2 23 96 8C A7 65 43 AC 01 4F DB 1C B1 26   ...#...eC..O...&
00000210  3C 2D 16 A2 F5 D1 8E A9 53 2C 2E 22 F2 DD 75 DD   <-......S,."..u.
00000220  D9 3A B8 97 86 99 BF 44 E4 3A F6 B2 8C 9F 52 F2   .:.....D.:....R.
00000230  7E 78 3E 78 A7 C8 3B EF B1 91 3B 31 FE C1 3B C1   ~x>x..;...;1..;.
00000240  62 7A F4 53 15 78 5A 80 98 EE E8 A7 60 B1 BE 26   bz.S.xZ.....`..&
00000250  3C 0D 5B 1C 3E C6 18 63 8C 31 C6 18 BB F7 07 E3   <.[.>..c.1......
00000260  FA 49 32 07 9E E1 00 00 00 00 00 49 45 4E 44 AE   .I2........IEND.
00000270  42 60 82    


Last edited by FlierMate on 20 Feb 2022, 11:26; edited 1 time in total
Post 20 Feb 2022, 11:23
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 949
Location: Russia
macomics 20 Feb 2022, 11:23
I haven't used Windows for a long time, and I don't always have access to Win 10 to check. But as I understand it, you need to run the console as an Administrator to be able to work with data from system folders.

ADD: Since fasm does not have a directive to determine the size of a file without reading it completely, I use a fixed size of 1 sector, if not explicitly specified.
Code:
define atest OK
match =count,count { restore atest
  size = 512 } ; <<< default size
match =OK, atest { match i, count \{ size = i \}
  restore atest }    
Otherwise, it is simply impossible to work with files longer than 1 GB. And with this approach, you can view at least part of a long file by specifying the start and count.
Post 20 Feb 2022, 11:23
View user's profile Send private message Reply with quote
FlierMate



Joined: 21 Jan 2021
Posts: 219
FlierMate 20 Feb 2022, 13:25
Thanks macomics, for the response, and your feedback about my code.

I plan to publish HEXDUMPQ (Quick Edition), with faster code execution (without branches / loop) in hexadecimal conversion.


Description: Same as HEXDUMPW above, but with faster Hex conversion.
Download
Filename: HEXDUMPQ.asm
Filesize: 6.94 KB
Downloaded: 335 Time(s)

Post 20 Feb 2022, 13:25
View user's profile Send private message Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.