Python Contains Unnecessary Strings When Retrieving Version Information from an exe File

Asked 2 years ago, Updated 2 years ago, 36 views

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

2022-09-30 14:26

1 Answers

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.


2022-09-30 14:26

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.