VarSetCapacity to BufferAlloc Topic is solved

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
User avatar
F4Jonatas
Posts: 45
Joined: 22 Oct 2015, 06:35
Contact:

VarSetCapacity to BufferAlloc

23 Nov 2020, 05:48

Sometimes I can't pass VarSetCapacity to BufferAlloc, when it is a string.
Here is a small example.

Code: Select all

msgbox( fileversioninfo( a_ahkpath, "FileDescription" ))

; Original version: https://autohotkey.com/board/topic/59496-filegetversioninfo-aw/
fileversioninfo( file :="", fileinfo :="" ) {
	local char := ( a_isunicode ? "W" : "A" )
	local fsize := dllcall( "Version\GetFileVersionInfoSize" char, "str", file, "uint", 0 )

	if not fsize
		return -1

	local trans
	local infoptr := 0
	local translation := 0
	local infoptr := 0
	local fvi := bufferalloc( fsize, 0 )
	varsetcapacity( trans, ( a_isunicode ? 16 : 8 ))

	dllcall( "Version\GetFileVersionInfo" char, "str", file, "int", 0, "uint", fsize, "uint", fvi.ptr )

	if not dllcall( "Version\VerQueryValue" char, "uint", fvi.ptr, "str", "\VarFileInfo\Translation", "uint*", translation, "uint", 0 )
		return -2

	if not dllcall( "msvcrt\swprintf", "str", trans, "str", "%08X", "uint", numget( translation +0 ), "cdecl int" )
		return -3

	local subblock := "\StringFileInfo\" substr( trans, -4 ) substr( trans, 1, 4 ) "\" fileinfo
	if not dllcall( "Version\VerQueryValue" char, "uint", fvi.ptr, "str", subblock, "uint*", infoptr, "uint", 0 )
		return -4

	local info := (
		a_isunicode
		? strget( infoptr, dllcall( "kernel32\lstrlen" char, "uint", infoptr ))
		: dllcall( "Kernel32\MulDiv", "uint", infoptr, "int", 1, "int", 1, "str" )
	)


	return info
}
User avatar
lmstearn
Posts: 698
Joined: 11 Aug 2016, 02:32
Contact:

Re: VarSetCapacity to BufferAlloc

23 Nov 2020, 07:32

Hey @F4Jonatas, no BufferAlloc in v1, did you want the v2 thread?
Just a couple of things- using swprintf with a possible ANSII string argument is suspect and VerQueryValue is writing to an address of translation so you'll want something like &translation. The folks in v2 will be more reliable than this, though. :)
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
swagfag
Posts: 6222
Joined: 11 Jan 2017, 17:59

Re: VarSetCapacity to BufferAlloc

24 Nov 2020, 10:07

Sometimes I can't pass VarSetCapacity to BufferAlloc, when it is a string.
there isnt a VarSetCapacity() in v2(anymore), so ur "sometimes being unable to pass it to BufferAlloc(), when it is a string" is a sentence that makes literally no sense at all.

Code: Select all

	local char := ( a_isunicode ? "W" : "A" )
having this is redundant. v2 is Unicode only
ur dllcall types are probably also all incorrect(UInt defined instead of Ptr). half the dllcalls are probably also redundant(since the v1 script is so old), just use ahk's Format()
User avatar
F4Jonatas
Posts: 45
Joined: 22 Oct 2015, 06:35
Contact:

Re: VarSetCapacity to BufferAlloc  Topic is solved

04 Dec 2020, 06:01

Perfect! I forgot that v2 is only Unicode and format function.
So I managed to eliminate some unnecessary things.

Code: Select all

msgbox( fileversioninfo( a_ahkpath, "FileDescription" ))

; Original version: https://autohotkey.com/board/topic/59496-filegetversioninfo-aw/
fileversioninfo( file :="", fileinfo :="" ) {
	local fsize := dllcall( "Version\GetFileVersionInfoSizeW", "str", file, "uint", 0 )

	if not fsize
		return -1

	local translation := 0
	local fvi := bufferalloc( fsize, 0 )

	dllcall( "Version\GetFileVersionInfoW", "str", file, "int", 0, "uint", fsize, "uint", fvi.ptr )

	if not dllcall( "Version\VerQueryValueW", "uint", fvi.ptr, "str", "\VarFileInfo\Translation", "uintp", translation, "uint", 0 )
		return -2

	if not numget( translation )
		return -3


	local infoptr := 0
	local trans := format( "0{:X}", numget( translation ))
	local subblock := "\StringFileInfo\" substr( trans, -4 ) substr( trans, 1, 4 ) "\" fileinfo
	if not dllcall( "Version\VerQueryValueW", "uint", fvi.ptr, "str", subblock, "uintp", infoptr, "uint", 0 )
		return -4

	local info := strget( infoptr, dllcall( "kernel32\lstrlenW", "uint", infoptr ))
	return info
}

Return to “Ask for Help (v2)”

Who is online

Users browsing this forum: Lpanatt and 64 guests