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

Asked 5 months ago, Updated 5 months ago, 12 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

    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")
        ctypes.byref(r), ctypes.byref(l))
    if not l.value:
        raise RuntimeError ("No codepage")
    codepage=array.array('H', ctypes.string_at(r.value,4))
    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")
        return ctypes.wstring_at(r.value, l.value)

name = "C:\\programs\test.exe"

The actual configured product name is TestApp, but there is an unnecessary string after TestApp.
How do I make this "TestApp" only?


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

© 2023 OneMinuteCode. All rights reserved.