1

Using GCC and C++11 on both Windows and Debian for armhf.

I need to check the version string of an executable to see if my app have to update itself.

My "Version string" is made in this way:

#define VersionFileCap "-[+@+]-"
const char VersioneFile[] = { VersionFileCap MYAPP_VERSION "," __DATE__ VersionFileCap };

and i can see it in the compiled binary.

My plan is to search for the VersionFileCaps and then manage the found string. To do this I'm about to use the code in the answer to this question but it loads the whole file in memory, and this is worrying me as my executable can easily become more than 50MB.

So i wonder if there's a way to make the string be compiled at the beginning of the binary, so i could just load part of the file and find it faster.

Is it possible by just using some preprocessing/compiler magic parameters?

Parduz
  • 662
  • 5
  • 22
  • 1
    Why don't you simply add some command-line arg to make your app print it's version and quit? Something like `/path/to/yourapp -V` – Denis Sheremet Feb 06 '20 at 11:11
  • I assume the OP doesn't want to execute the app. Maybe the update is running from a more privileged user? – divinas Feb 06 '20 at 11:16
  • if the version of the file is newer then my app copies the binary in a folder and reboot the device. A script will then overwrite the old binary with the new one. Anyway, i need to display on the UI the binary version, so my question is really just about how to store that string in the beginning of the file. – Parduz Feb 06 '20 at 11:35
  • If the app has to update itself then it already knows its version. If some other updater app has to know the app version, then `On Windows GetFileVersionInfoEx or GetFileVersionInfo API functions can be used`. On Linux probably there is no such function. So `one global solution could be to calculate checksum(md5 or sha or alike) of the binary file and send that to server`, server would maintain a database of all released versions for all platforms with their checksum and version number, then server and updater can talk to fetch new version and install... – Sumit Feb 06 '20 at 11:50
  • @Sumit: how this all can answer my question? Obviously the app know his own version. That's why it needs to know the version of another binary file to see if it is newer. I don't have servers, just a Beaglebone Black on a machine. My question is specific, i don't need solutions to things i have no control or power to change (admitted i'd wanted to). – Parduz Feb 06 '20 at 11:54
  • @Parduz I think you should read the file "chunk" by "chunk" with overlapping section of the size of the string to search. This way you'll not have more than the size of a "chunk" in memory. – Fareanor Feb 06 '20 at 12:17
  • @Fareanor: sure, that's what i'm doing, in the end. But if i have a way to store that string near the beginning of the file i'll just read a small part of it. – Parduz Feb 06 '20 at 13:10
  • @Parduz Will it work if you place a text/binary/.ini file containing just the app version information on the target machine, and the updater program will read that small file to know the installed app version ? You may also include executable file's checksum in that version info file for security/reliability reasons... – Sumit Feb 06 '20 at 13:11
  • @Parduz Also instead of beginning if you write something at the end of the executable file, the executable does not get corrupted, it still executes. So by doing that if you can read just few chars from end then that could be one solution. – Sumit Feb 06 '20 at 13:30

1 Answers1

0

The correct way to handle this, at least on Windows, is to store your app's version values in the app's VERSIONINFO resource, not in a string in the app's code.

If the app wants to use the version info to populate its VersioneFile[] array, it can do so dynamically at runtime.

Microsoft has Win32 API functions specifically designed for querying data from the VERSIONINFO resource of an executable file (EXE or DLL):

GetFileVersionInfoSize()

GetFileVersionInfo()

VerQueryValue()

Your app can query its own version data. An outside updater can query your app's version data.

The Win32 API can access an executable file's resources without having to load the entire file into memory at one time.

See Version Information on MSDN for more details.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770