Error using GetFileSizeEx API call Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
rhodian
Posts: 13
Joined: 20 Nov 2019, 23:33

Error using GetFileSizeEx API call

Post by rhodian » 03 Dec 2023, 16:27

Hello,

I am using using Autohotkey, WINAPI & DllCall GetFileSizeEx function provided by @jNizM from here https://github.com/jNizM/AHK_DllCall_WinAPI/blob/master/src/File%20Management%20Functions/GetFileSizeEx.ahk, which I converted to V2.

Code: Select all

GetFileSizeEx(File)
{
    static FileSize, static init := VarSetStrCapacity(&FileSize, 1048576)
    if ((hFile := DllCall("kernel32.dll\CreateFile", "Str", File, "UInt", 0x80000000, "UInt", 1, "Ptr", 0, "UInt", 3, "UInt", 0, "Ptr", 0)) = -1 )
        return DllCall("kernel32.dll\GetLastError")
    if !(DllCall("kernel32.dll\GetFileSizeEx", "UInt", hFile, "Uint",&FileSize))
        return DllCall("kernel32.dll\GetLastError")
    FileSize := NumGet(FileSize, 0, "Int64*")
    DllCall("kernel32.dll\CloseHandle", "UInt", hFile)
    return FileSize
}
I've obviously done/am doing something wrong because when I run the script from VSCode by passing the path of a file to the function, I get an Error: Expected a Number but got a VarRef error!

Can anyone help?
Attachments
2023-12-04_08-14-46.jpg
2023-12-04_08-14-46.jpg (36.39 KiB) Viewed 512 times


vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: Error using GetFileSizeEx API call

Post by vmech » 04 Dec 2023, 07:24

@rhodian, try:

Code: Select all

if !(DllCall("kernel32.dll\GetFileSizeEx", "UInt",hFile, "PtrP",&FileSize))
    return DllCall("kernel32.dll\GetLastError")
FileSize := NumGet(FileSize, 0, "Int64")
PS. As @teadrinker noted above, many WinAPI things are reflected internally by Autohotkey itself. Such as :!: FileGetSize function and builtin variable :!: A_LastError.
Please post your script code inside [code] ... [/code] block. Thank you.

teadrinker
Posts: 4380
Joined: 29 Mar 2015, 09:41
Contact:

Re: Error using GetFileSizeEx API call

Post by teadrinker » 04 Dec 2023, 09:30

@vmech
If the goal is to get a result, it's easier to use a built-in function. If you learn how to use DllCall(), there are enough issues in the initial code so I wouldn't use it as an example.

vmech
Posts: 361
Joined: 25 Aug 2019, 13:03

Re: Error using GetFileSizeEx API call

Post by vmech » 04 Dec 2023, 12:24

@teadrinker
There is only one problem - frankly poor code conversion.
I don't want to discuss anything. In the end I just made a better try. Although personally I don’t need it at all.
Spoiler
Please post your script code inside [code] ... [/code] block. Thank you.

teadrinker
Posts: 4380
Joined: 29 Mar 2015, 09:41
Contact:

Re: Error using GetFileSizeEx API call  Topic is solved

Post by teadrinker » 04 Dec 2023, 12:40

vmech wrote: I don't want to discuss anything
:) Ok, for those who might want to figure it out:
vmech wrote: DllCall("GetFileSizeEx", "Ptr",hFile, "PtrP",&FileSize := 0)
PtrP is not quite correct, Int64P must be here.

rhodian
Posts: 13
Joined: 20 Nov 2019, 23:33

Re: Error using GetFileSizeEx API call

Post by rhodian » 04 Dec 2023, 20:51

Thanks gentlemen...

Post Reply

Return to “Ask for Help (v2)”