Search code examples
registryinno-setuppascalscript

Storing the current date to the registry in binary format with Inno Setup


Is it possible using Inno Setup to write the current date to a registry key as binary data? (if it is a string then that makes it very easy for someone to alter)

In my MFC C++ projects I serialize a DATE variable using WriteProfileBinary.

Eventually I need to be able to read this value into a DateTime variable using .NET C#. I would be using the common program data key to store the value and only if doesn't exist.


The [Registry] caters for binary data:

If binary is specified, Setup will create a binary (REG_BINARY) value ... If the data type is binary, this is a sequence of hexadecimal bytes in the form: "00 ff 12 34".

How to we take the timestamp and turn it into the required format?


I see this question (Writing binary data to registry using Inno Setup Pascal Scripting) which is useful and also in the Inno Setup forum where they responded to me:

It's possible, but you'll have to use some [Code] and the RegWriteBinaryValue function.

But it is still not clear to me:

  1. How to write the current timestamp to the registry using this function?
  2. How to write it in a binary format such that .NET C# will be able to read it as a DateTime object?

Solution

  • To write the current date to registry as a binary value, you can do something like this:

    var
      DateStr: string;
    begin
      DateStr := GetDateTimeString('yyyy/mm/dd', '-', ':');
      RegWriteBinaryValue(
        HKEY_AUTO, 'Software\My Company\My Program', 'InstallDate', DateStr);
    end;
    

    It will result in:

    C:\>reg query "HKCU\Software\My Company\My Program"
    
    HKEY_CURRENT_USER\Software\My Company\My Program
        InstallDate    REG_BINARY    323032312D31302D3039  
    

    You can do even more obfuscation if you like. No matter what you do, user will be able to modify the value with more or less effort.