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.
355 Unity Virtual Stick Does Not Return to Center When You Release Your Finger
366 Is there any other way to save the Python database?
369 Update Flask User Information
353 I want to create an array of sequences from "1" to a specified number.
362 To Limit Column Values to Strings in a String List Using sqlalchemy
© 2023 OneMinuteCode. All rights reserved.