VC USES the compile time as a method for version number identification

  • 2020-05-12 03:00:10
  • OfStack

Common methods are divided into two steps:

1. Get the compile time;

2. Set the base time to be 2 times of the total number of days between the compilation time and the base time as the version number, and add the initial value if appropriate;

The first step is implemented in two ways:

1. Use system macros directly: CString OcxTime = __DATE__;
2. Call the batch file in the Pre-link Step compiler option.


@echo off
echo #pragma once>"Ver.h"
echo #define APP_VER_NUM  "%data%">>"Ver.h"

Other netizens added

Use macro s s 20en__ and s 21en__


CString strVersion,strBuildTime; 
strBuildTime = __DATE__; 
strBuildTime.Append(" "); 
strBuildTime.Append(__TIME__); 
COleVariant vtime(strBuildTime); 
vtime.ChangeType(VT_DATE); 
COleDateTime dateTime=vtime; 
SYSTEMTIME systime; 
VariantTimeToSystemTime(dateTime, &systime); 
CTime buildTime(systime); 
strVersion = buildTime.Format(" Builded %Y-%m-%d %H:%M:%S "); 

it1 dream students supplement

Generally speaking, the official software will contain the svn version number of the program, the compiled version of the software, the compiled time and other information, but these things do not exist by default, we need to carry out some steps to deal with. There are several ways to do this, but the most common one is to get the version number and the current time with SVN's built-in instructions, fill in the placeholder in the template, and finally overwrite the resource file with the replaced template. Here is the simplest one:
a. First you need to add a resource file to the VC program. If the program is named Test, the resource file will be Test.rc
b. Add a new resource item to the resource file -- version. Open the resource file with notepad and add the following code:


VS_VERSION_INFO VERSIONINFO
 FILEVERSION 1,0,0,1
 PRODUCTVERSION 1,0,0,1
 FILEFLAGSMASK 0x17L
#ifdef _DEBUG
 FILEFLAGS 0x1L
#else
 FILEFLAGS 0x0L
#endif
 FILEOS 0x4L
 FILETYPE 0x1L
 FILESUBTYPE 0x0L
BEGIN
  BLOCK "StringFileInfo"
  BEGIN
    BLOCK "080404b0"
    BEGIN
      VALUE "FileDescription", "Test"
      VALUE "FileVersion", "1, 1, 1, 0"
      VALUE "InternalName", "Test"
      VALUE "LegalCopyright", "Copyright (C) 2015"
      VALUE "OriginalFilename", "Test.exe"
      VALUE "ProductName", "Microsoft"
      VALUE "ProductVersion", "V1.0"
    END
  END
  BLOCK "VarFileInfo"
  BEGIN
    VALUE "Translation", 0x804, 1200
  END
END

c. Next, create a template and use the placeholder WCREV provided by SVN to get the version number and WCNOW to get the compile time. Templates can directly copy the Test.rc file, renamed Test.rc2. Modify the value of ProductVersion in the above code:


VALUE "ProductVersion", "V2.1.1.$WCREV$.(Build$WCNOW$)"

d. When do placeholder substitutions, followed by the replacement of the program's resource file Test.rc, accurately compile the version number and compile time into the program? VS provides a configuration of Build Events. We select Pre-Build Event and add 1 line of code as follows:


SubWCRev $(ProjectDir) $(ProjectDir)\Test.rc2 $(ProjectDir)\Test.rc

The code means that before the program is compiled, use SVN's scripting tool SubWCRev to get the SVN number of the project directory and the current compile time, replace the placeholder in the rc2 file, and overwrite the rc file.


Related articles: