ctypes is trying to get the product name of the version information from the exe file using Windows API.
However, retrieval results may contain unnecessary strings.
Is there a good solution?
import array, ctypes, os
defaultFileVersionInfo(name):
if notos.path.exists(name):
raise RuntimeError("The file%s does not exist"%name)
size = ctypes.windll.version.GetFileVersionInfoSizeW(name,None)
if not size:
raise RuntimeError ("No version information")
res=ctypes.create_string_buffer(size)
ctypes.windll.version.GetFileVersionInfoW(name,None,size,res)
r=ctypes.c_uint()
l=ctypes.c_uint()
ctypes.windll.version.VerQueryValueW(res,u'\\VarFileInfo\\Translation',
ctypes.byref(r), ctypes.byref(l))
if not l.value:
raise RuntimeError ("No codepage")
codepage=array.array('H', ctypes.string_at(r.value,4))
codepage="%04x%04x"%tuple(codepage)
if not ctypes.windll.version.VerQueryValueW(res, u'\\StringFileInfo\\%s\\%s'%(codepage, "ProductName"), ctypes.byref(r), ctypes.byref(l)):
raise RuntimeError ("Invalid or unavailable version info")
else:
return ctypes.wstring_at(r.value, l.value)
name = "C:\\programs\test.exe"
verInfo=getFileVersionInfo(name)
print(repr(verInfo))
The actual configured product name is TestApp, but there is an unnecessary string after TestApp.
How do I make this "TestApp" only?
'TestApp\x004\x0e\x01Pro'
The environment is Windows 10 64-bit, Python 3.7
.Thank you for your cooperation.
python python3 windows
VerQueryValueW
may return characters or bytes depending on the situation.In this case, the number of bytes in the Unicode string is returned, so the number of characters must be halved.
In fact, TestApp\x004\x0e\x01Pro
takes out 14 characters from 7 characters and 14 bytes.
627 GDB gets version error when attempting to debug with the Presense SDK (IDE)
579 Understanding How to Configure Google API Key
576 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
586 PHP ssh2_scp_send fails to send files as intended
933 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.