Passing preprocessor variable to nmake build environment - c++

I am having issues with building driver using nmake on Win7 x64 build environment. I am defining a preprocessor variable and passing it over the command line using -
build /nmake "USER_C_FLAGS=/DMyVersion=3"
And the build log is -
...
/DMyVersion=3
/typedil-
/wd4603
/wd4627
....
So, I clearly see the variable as part of the compiler options. Now in a header fie, I do
#define otherVersion 10
#ifdef MyVersion
#undef otherVersion
#define otherVersion MyVersion
#endif
#define FileVersion otherVersion
The problem is FileVersion is always being 10 irrespective of the MyVersion define passed and exist in the environment. To test, what is going on, I did -
#ifdef MyVersion
#error MyVersion is present in the environment.
#endif
I see the statement being printed. But why is otherVersion is always 10 despite the preprocessor directive is present in the environment ? Why it is not taking the value 3 passed via command line options?

I'm not sure, if this works for you, but some people did try to achieve quite the same using msbuild. They had to adapt the project-file to pipe their definitions "into" the build-process. Have a look at MSBuild.exe not accepting either /p:DefineConstants nor /p:PreprocessorDefinitions

Related

'CheckTokenMembership' to check for Admin Rights C++

i'm trying to use the CheckTokenMembership function, i copied the example piece of code on Microsofts website ( https://msdn.microsoft.com/pt-br/library/windows/desktop/aa376389(v=vs.85).aspx ), but i just can't get it to work!
I get "error: 'CheckTokenMembership' was not declared in this scope" (on CodeBlocks) no matter what i do.
I am including Windows.h and even Winbase.h as the website says. Does anyone have any idea on how to fix this?
Before including windows.h you need to specify the minimum version of Windows your application will support. The idea here is that the build will fail if you use an API function that isn't available in the specified Windows version.
You can do this by defining the _WIN32_WINNT macro, e.g., for Windows 7:
#define _WIN32_WINNT _WIN32_WINNT_WIN7
If you need to be more specific you can also define the NTDDI_VERSION macro, e.g., for Windows 10 version 1607:
#define _WIN32_WINNT _WIN32_WINNT_WIN10
#define NTDDI_VERSION NTDDI_WIN10_RS1
The documentation tends to lag behind, but you can find the definitions in sdkddkver.h in the SDK.
Addendum:
In some cases, e.g., if using an old or third-party version of the SDK, the named constants may not work and you will have to resort to magic values, e.g., the examples above would become
#define _WIN32_WINNT 0x0601
and
#define _WIN32_WINNT 0x0A00
#define NTDDI_VERSION 0x0A000002
You can look these up by obtaining a recent version of sdkddkver.h from Microsoft or you could try the search engine of your choice. :-)

What is _WIN32_WINNT and how does it work?

EDIT 2: Ok so I changed to Orwell DevC++ which contains the "winnt.h" that contains #define KEY_WOW64_64KEY 0x0100 but it still is not working. (Refer to EDIT 1:)
EDIT 1: I looked into the "winnt.h" which came along the CodeBlock and DevC++ and the DevC++'s is missing the following lines:
#if (_WIN32_WINNT >= 0x0502)
#define KEY_WOW64_64KEY 0x0100
#define KEY_WOW64_32KEY 0x0200
#endif
And putting the above code in the wint.h of DevC++ doesn't work.
Original Post:
I have a 32bit application (developing in DevC++ and Windows 7 64bit) which reads a 64bit app's registry as one of its task, so I am trying to use "KEY_WOW64_64KEY" flag in RegOpenKeyEx, and found few posts regarding how to use it with _WIN32_WINNT : this and this
It worked like charm when I used it in a CodeBlock Project(a test project) but the same code is not working with DevC++, I can't port it to codeblock now since codeblock presents other problems.
How do I make it work with DevC++ ?
Thanks
It defines the version of the windows header files to use. It must be declared before you #include <Windows.h>.
There are a few other similar variables you should probably set if you're going to modify it:
MSDN Using Windows Headers
_WIN32_WINNT is a preprocessor token, which is replaced by (0x0601) wherever _WIN32_WINNT is used. The preprocessor just scans the whole file and replaces _WIN32_WINNT with (0x0601) everywhere it is found.
Chances are, there could be ifdef preprocessor guards that will enable/disable a preprocessor constant. Like:
#ifdef _WIN32_WINNT
#define KEY32 32
#endif
There, KEY32 will only be defined IF _WIN32_WINNT is defined.
It already works with DevC++.

How can I conditionally include a nonexistent file in Visual C++?

I'm using Visual C++ to build and test a project, but I'll also be using it on other platforms. I started the code initially on a different platform.
In some of my class headers I need to include a file which is specific to the other platform, however, this file doesn't exist in the Visual C++ workspace.
I've tried using code like this:
#if TARGET == OTHERPLATFORM
#include "themissingfile.h"
#endif
where TARGET is defined elsewhere as WINDOWS
#define TARGET WINDOWS
However, I still get a compiler error stating that "themissingfile.h" cannot be found. It appears like the precompiler is processing the include before the if. What's the best way to get around this? I suppose I could just create a blank "themissingfile.h" in the Visual C++ source folder, but it seems to me like there should be a better solution.
#define TARGET WINDOWS does not set TARGET to the string WINDOWS: it sets it to whatever WINDOWS is defined to. Most likely this happens:
#define WINDOWS // Sets WINDOWS to 0
#define OTHERPLATFORM // Sets OTHERPLATFORM to 0
#define TARGET WINDOWS // Sets TARGET to 0
#if TARGET == OTHERPLATFORM // Evaluates to 0==0, so true
Use _WIN32 macro instead:
#ifndef _WIN32
#include "themissingfile.h"
#endif
_WIN32 is defined for both 32-bit and 64-bit builds. See C/C++ Predefined Macros.

problems in migrating 32bit application on 64 bit

I am trying to migrate existing c++ 32 code to 64 code on windows7 with visual studio 2010.i never did 64bit compilation before. with the help of internet references i did the setup for 64 bit compilation. like VS2010 with 64 bit compiler etc and other configuration changes.
In the preprocessor i removed WIN32 and added WIN64. i have some other pre-processors like OS_WIN_32 and some other which are specific in my code.
In the code wherever WIN32 was getting used i added extra condition as || WIN64 this is just to ensure that application should get compiled with win32 as well as win64.
When i am trying to compile the code i am getting the compilation error saying
fatal error C1189: #error : Only one of the WIN32 and WIN64 symbols should be defined
this error is coming from the local code where we have a check whether both WIN32 and WIN64 are defined. that code is as shown below.
#if defined WIN32 && defined WIN64
# error Only one of the WIN32 and WIN64 symbols should be defined
#endif
in VS2010 if macros are not enabled then the code inside the macro gets greyed out. in my code also the above error is greyed out. but still i am getting that error.
The code where i added WIN64 is including windows.h. for reference givine it below.
#if defined WIN32 || defined WIN64
#include <windows.h>
#include <process.h>
#endif
So my question is why i am getting this error? shouldnt we add windows.h for 64bit compilation.? i tried by commenting this inclusion but i am getting other errors wrt HANDLE which are used in the code.
If i go to WIN32 definition VS2010 is pointing to a definition in windef.h file. This file is present in Microsoft SDKs\windows\v7.0A\include folder i.e. not my local code.
for referance that definition is given below.
#ifndef WIN32
#define WIN32
#endif
So i want to know why compiler is getting both pre-processors WIN32 and WIN64.
Thanks in advance for your help.
You shouldn't define either yourself. The macro's that should be used to check this are
_WIN32 // always defined for Windows apps
_WIN64 // only defined for x64 compilation
These are defined by the compiler (see here).
Often, the IDE adds the unprefixed macros to the commandline to not let legacy projects which use the non-documented unprefixed versions fail to build. The fact that they work is not a reason to use them, when documented alternatives are present.
It boils down to this:
#ifdef _WIN32
// We're on Windows, yay!
#ifdef _WIN64
// We're on x64! Yay!
#else // _WIN64
// We're on x86 (or perhaps IA64, but that one doesn't matter anymore). Yay!
#endif // _WIN64
#else // _WIN32
// We're not on Windows, maybe WindowsCE or WindowsPhone stuff, otherwise some other platform
#endif

How to Increment Visual Studio build number using C++?

I have a Visual Studio 2008 project that produces a file called: "Game-Release.exe".
This was configured under Project Properties -> C/C++ -> Linker -> General:
$(OutDir)\$(ProjectName)-Release.exe
I would like to take this a bit further by have an incrementing build number so I would have something which says:
Game-Release-Build-1002.exe
The number on the end should be an incrementing integer. I will be storing the build exe's on subversion so I think i would find this useful (although not necessary).
Perhaps there is a built in macro in Visual Studio that could handle this. Quite possibly I was thinking I could have a text file with the build number in it and have the compiler read, use and increment the number in the file each time the project is built. My goal is however to make the process as automated as possible. What is the best way to accomplish this?
If you offer an opinion, please also provide the code we can all share. Thnx.
The Versioning Controlled Build add-in seems like it would do the job.
Update: Your question specifically mentions using Visual Studio to increment the version, but there is nothing automated about that. Have you considered using Nant and a CI server? That way, it is easy to inject the SVN revision number into AssemblyInfo.cs equivalent for C++. Automatically, on the build server.
If you are using svn to version your project you could follow the instructions in this link it works perfectly for me because I can track bugs in a release application using its version information and comparing the source code.
All information below is not included in the link:
setup your rc file like this version.rc
#include "resource.h"
#include "version.h" //<-----------Don't forget the include
/////////////////////////////////////////////////////////////////////////////
//
// Version.rc
//
VS_VERSION_INFO VERSIONINFO
FILEVERSION VER_FILE_VERSION
PRODUCTVERSION VER_PRODUCT_VERSION
FILEFLAGSMASK 0x17L
#ifdef _DEBUG
FILEFLAGS 0x1L
#else
FILEFLAGS 0x0L
#endif
FILEOS 0x4L
FILETYPE 0x1L
FILESUBTYPE 0x0L
BEGIN
BLOCK "StringFileInfo"
BEGIN
BLOCK "040704b0"
BEGIN
VALUE "CompanyName", "Your Company Name"
VALUE "FileDescription", "Your Application Description"
VALUE "FileVersion", VER_FILE_VERSION_STR "\0"
VALUE "InternalName", "Internal Name"
VALUE "LegalCopyright", "CopyRight stuff - Copyright (C) 2015"
VALUE "OriginalFilename", "yourapp.exe"
VALUE "ProductName", "Your Application"
VALUE "ProductVersion", VER_PRODUCT_VERSION_STR "\0"
END
END
BLOCK "VarFileInfo"
BEGIN
VALUE "Translation", 0x407, 1200
END
END
Next add version.h file to your project which defines everything, setup like this:
#include "svn_version.h"
#define STRINGIZE2(s) #s
#define STRINGIZE(s) STRINGIZE2(s)
#define VERSION_MAJOR 1
#define VERSION_MINOR 0
#define VERSION_REVISION SVN_REVISION
#define VERSION_BUILD 0
#if SVN_LOCAL_MODIFICATIONS
#define VERSION_MODIFIER "M"
#else
#define VERSION_MODIFIER
#endif
#define VER_FILE_VERSION VERSION_MAJOR, VERSION_MINOR, VERSION_REVISION, VERSION_BUILD
#define VER_FILE_VERSION_STR STRINGIZE(VERSION_MAJOR) \
"." STRINGIZE(VERSION_MINOR) \
"." STRINGIZE(VERSION_REVISION) \
"." STRINGIZE(VERSION_BUILD) \
#define VER_PRODUCT_VERSION VER_FILE_VERSION
#define VER_PRODUCT_VERSION_STR VER_FILE_VERSION_STR
after this you can follow the link above.
I'm not sure VS2008 has that feature but I think you can do it with a post-linker event that runs a little script that make the task for you.
I use a pre-build script (written in JavaScript and executed using the cscript.exe engine) that defines the major/minor release, gets the SVN revision number and generates a magic build number based on the current date. The script then creates a version.h file that is used by the main application (and by the main apps resource file to create a VERSION resource).
My own approach, including binary file stamping:
http://indiocolifax86.wordpress.com/2010/05/22/a-scheme-for-automatic-build-numbers-in-cc-projects/