Use # definition to easily control the macro of the version number

xiaoxiao2021-03-06  102

In a VC Project, there may be many places to use the version number, such as the About dialog, version resource, etc. If each version change will change these values, it is not only very troublesome, but also the only principle.

Smartily use macro definitions, you can solve this problem well.

Let's take a look at several predecessor operators:

OperatorActionStringizing operator (#) Causes the corresponding actual argument to be enclosed in double quotation marksCharizing operator (# @) Causes the corresponding argument to be enclosed in single quotation marks and to be treated as a character (Microsoft Specific) Token-pasting operator (# #) Allows tokens used as actual arguments to be concatened to form Other tokens

Simply put, # 就 使 参 参 号 号 号;;;; 包 包 包 包 包 包 包 包 包 包 包 包 包 包 包 包 包 包 号 包 包 包 包 包 包 包 包These operators are more embarrassed from the language, take a closer look at the MSDN example, and understand.

Below is an example says how to define a unified version macro.

Assuming that there is a resource script file in the project resource.rc, the unique version number is stored in Define.h. Other CPP files that need to be used to version number include this file.

Let's take a look at Define.h:

#define VER_Major 2 # define VER_Minor 2 # define VER_Release 137 #define VER_Build 1762 # define VER_Version MAKELONG (MAKEWORD (VER_Major, VER_Minor), VER_Release) #define _Stringizing (v) # v # define _VerJoin (a, b, c, d) _Stringizing (abcd) #define STR_BuildDate TEXT (__ DATE __) # define STR_BuildTime TEXT (__ TIME __) # define STR_BuilDateTime TEXT (__ DATE__) TEXT ( "") TEXT (__ TIME __) # define STR_Version TEXT (_VerJoin (VER_Major, VER_Minor, VER_Release, VER_Build))

It is assumed that after the above definition, Ver_major, Ver_MINOR, VER_RELEASE, VER_MINOR, VER_RELEASE, VER_BUILD are used as the version number; STR_VERSION is used as version string (Str_Version, the Str_Version executes the macro in the example, is Text ("2.2.137.1763") TCHAR string). These macros can be used directly in any CPP file.

Resource script resource.rc, suppose we need version resources, you can first define some macros in Define.h, such as:

#define STR_AppName TEXT ( "CuteFoto") # define STR_Author TEXT ( "Yonsm") # define STR_Corporation TEXT ( "Yonsm.NET") # define STR_Web TEXT ( "Yonsm.reg365.com") # define STR_Email TEXT ( "Yonsm @ 163.com ") # define STR_WebUrl TEXT (" http: // ") STR_Web # define STR_EmailUrl TEXT (" mailto:? ") STR_Email TEXT (" Subject = ") STR_AppName # ifdef _CHS # define STR_Description TEXT (" smart batch Scanning photo cutting tool. ") # Define str_copyright text (" Copyright (C) 2002-2004 ") STR_CORPORATION TEXT (", keep all right. ") # Else // _chs # define str_description text (" Picture Cutting Tool for Batch -scanned photo. ") # define str_copyright text (" Copyright (C) 2002-2004 ") STR_CORPORATION TEXT (". All Rights Reserved. ") # Endif //_chs This time, we add version resources in Resouurc.RC - - Note, don't add it directly, otherwise it will be changed to the actual value by VS, so you can't achieve our goal. A good solution is:

Double-click Resource.rc to go to the resource view in VS ID, right-click Resource.rc, select "Resources include", contain define.h in "Read-only symbol instruction":

#define apstudio_hidden_symbols // ... # include "define.h" #include "windows.h" // ... # undef apstudio_hidden_symbols

Enter: "Compile time instruction":

LANGUAGE LANG_NEUTRAL, SUBLANG_DEFAULT # pragma code_page (DEFAULT) VS_VERSION_INFO VERSIONINFOFILEVERSION VER_Major, VER_Minor, VER_Release, VER_BuildPRODUCTVERSION VER_Major, VER_Minor, VER_Release, VER_BuildFILEFLAGSMASK VS_FFI_FILEFLAGSMASKFILEFLAGS 0FILEOS VOS__WINDOWS32FILETYPE 0FILESUBTYPE VFT2_UNKNOWNBEGIN BLOCK "StringFileInfo" BEGIN BLOCK "040004e4" BEGIN VALUE "FileVersion", STR_Version VALUE "FileDescription ", STR_Description VALUE" ProductName ", STR_AppName VALUE" ProductVersion ", STR_Version VALUE" CompanyName ", STR_Corporation VALUE" LegalCopyright ", STR_Copyright VALUE" Comments "," Powered by "STR_Author". "VALUE" OriginalFilename ", STR_AppName EXT_Exe END END BLOCK "VARFILEINFO" Begin Value "Translation", 0x0400, 0x04e4 Endendok, then change the version number or program information, as long as define.h is modified.

carry on……

Define.h assumes VER_RELEASE / VER_BUILD to refer to Release number and build number, if the two values ​​indicate the number of times and the number of times compiled with the release method, how good :) I wrote a gadget BPP ( BUILD Means ":

First put bpp.exe in the engineering directory, then in the VC Project property, demug mode: Enter "BPP Define.h" in "Generation Event" -> "Presentation" -> Command Line, Release mode : "BPP -RELEASE DEFINE.H". In this way, each VC compile will call bpp.exe to increase the Ver_build or Ver_reely from one. Tool cool ...

转载请注明原文地址:https://www.9cbs.com/read-100629.html

New Post(0)