flat assembler
Message board for the users of flat assembler.

Index > Windows > [solved] GetFileSize and GetFileSizeEx issues

Author
Thread Post new topic Reply to topic
StakFallT



Joined: 19 Jan 2006
Posts: 50
StakFallT 10 May 2024, 12:11
Is there some kind of issue with the Windows GetFileSize (https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfilesize) / GetFileSizeEx (https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfilesizeex) API?

I've been trying to debug a small portion of a much larger project for several days now and it almost looks like there IS an issue. I've been primarily using x64dbg to debug the built executable and I wind up going pretty darn deep into kernel32.dll, ntdll.dll, and gdi32.dll. I've tried running a trace and it goes for hours and set an exception breakpoint on ERROR_INVALID_HANDLE (from the PDF at: https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/1bc92ddf-b79e-413c-bbaa-99a5281a6c90) but that didn't help.

It seems I'm getting a handle to the file just fine via CreateFile as the last_error shown by x64dbg on that is ERROR_SUCCESS. However, the next call I make, to GetFileSize or GetFileSizeEx, the value of last_error, shown in x64dbg, is ERROR_INVALID_HANDLE. I've checked file permissions, I've ran chkdsk, I've tried running the file as administrator, at one point I even thought it might have been an issue with SysWOW64 paths with an x86 executable (so I started converting it to a 64-bit executable to test that theory), I started to think it might have been wide-code issues with the filepath / name I was specifying. None of that turned out to be the issue.

I thought maybe it was because the system had pending updates; that turned out to not be the issue. I thought it might have been some weird failure due to a cascading / domino effect of some kind (ex. a lack of virtual memory, causing a DLL needing loading by the API failed, thus the DLL wasn't present in memory and returning a semi-erroneous error); that wasn't it as I tried it on another workstation and still got the same error. I've attached a small test-case. I've left most (but not all since) of the commented out code intact to show various things I've tried or lines of thought I had while trying to determine the issue.

I am seriously hoping someone can shed some light on this; I'm at the end of my wits having tried to debug this for so long... Thanks in advance!


Description: Sample test-case
Download
Filename: GetFileSizeEx_Test.asm
Filesize: 4.24 KB
Downloaded: 15 Time(s)

Post 10 May 2024, 12:11
View user's profile Send private message Reply with quote
macomics



Joined: 26 Jan 2021
Posts: 913
Location: Russia
macomics 10 May 2024, 13:17
Code:
invoke GetFileSizeEx, [hFile], File_Size ; instead invoke GetFileSizeEx, hFile, File_Size    


Description: Here is the correct value passed to GetFileSizeEx
Filesize: 17.1 KB
Viewed: 132 Time(s)

Screenshot_20240510_174535.png


Description: And here's what you pass to GetFileSizeEx
Filesize: 21.85 KB
Viewed: 132 Time(s)

Screenshot_20240510_174639.png


Description: This is the value that CreateFile returned to you
Filesize: 22.37 KB
Viewed: 132 Time(s)

Screenshot_20240510_174442.png




Last edited by macomics on 10 May 2024, 13:50; edited 1 time in total
Post 10 May 2024, 13:17
View user's profile Send private message Reply with quote
StakFallT



Joined: 19 Jan 2006
Posts: 50
StakFallT 10 May 2024, 13:25
Wow... THANK YOU!!! lol
Post 10 May 2024, 13:25
View user's profile Send private message Reply with quote
MatQuasar



Joined: 25 Oct 2023
Posts: 97
MatQuasar 10 May 2024, 13:36
macomics wrote:
Code:
invoke GetFileSizeEx, [hFile], File_Size ; instead invoke GetFileSizeEx, hFile, File_Size    


Brilliant! I also helped to debug for about 30 mins, but didn't find this mistake. I keep getting 0xffffffff for low order DWORD.
Post 10 May 2024, 13:36
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.