Defining a preprocessor directive for windows 7 - c++

I am making use of SetDllDirectory() and wants to know how can i define a directive for windows 7 HP 64 bit i am thinking something like this :
In preprocessor directives :
Add WIN7
In the .cpp i was thinking to add something like
#ifndef WIN7<- where the function is used
SetDllDirectory();
#endif
but as soon as i add the statements the SetDllDirectory becomes commented.
And this is something i tried with , i added WIN7 in the preprocessor definations and added :
#if defined(__WIN7__)
if (regkeyExists) {
if (regkey->HasValue("LibPath")) {
regkey->QueryValue("LibPath", value);
if (!value.empty()) {
wxSetEnv("ABCLib", value);
SetDllDirectory(value.c_str());
}
}
}
SetDllDirectory("C:\\Program Files\\ABC\\ABCProject\\lib");
#endif
Is this the right to declare windows 7
Thanks

You don't want to define your own macro to detect Windows 7, use the ones provided by Microsoft - wrap your Win7 only code in:
#ifdef _WIN32_WINNT_WIN7
xyz()
#endif
If you do create your own, you must define it when you want to compile for Windows 7, it seems that you're hiding the SelDllDirectory() function - #ifndef means "if not defined" so if WIN7 macro is not defined anywhere, then you get that function.

How about NTDDI_VERSION or _WIN32_WINNT?

Related

Class name that is already a macro

I have the following code :
class MessageBox
{
public:
MessageBox();
};
But the problem is that MessageBox is a os-specific macro in my case and it causes problems.I tried #define MyMacro MessageBox, then #undef MessageBox and re-define it later, but that only works inside the header, and so when i try to create an object of type MessageBox in a source file, it does not work.
If the name of you class matches a macro that you really can't change, then you'll have to change the name of your class.
One possible solution is to create a header which would be added by rest of headers or sources of your project which need it instead of system header and create a thin wrapper function:
#include <system_header.h>
inline void myMessageBox( ... )
{
MessageBox( ... );
}
#undef MessageBox
it is difficult to say how to pass parameters without further details of that macro.

I do not have lvm_GetSelectionMark in my commctrl.h

I needed to get the id of the focused item from a listview using a handle:
int selected = (int)SendMessage(hWnd, LVM_GETSELECTIONMARK, 0, 0);
However, I get an error that LVM_GETSELECTIONMARK was not declared in this scope? Is there something wrong with my library because the macro ListView_GetSelectionMark(hWnd) does not exist either.
Also, I did include commctrl.h in the header.
Thank you.
Add these lines to your project. For some reason these are missing from the Commcrtl.h included with Codeblocks IDE.
#define LVM_GETSELECTIONMARK (LVM_FIRST+66)
#define ListView_GetSelectionMark(w) (INT)SNDMSG((w),LVM_GETSELECTIONMARK,0,0)

QMessageBox Compatibility

I'm looking through some Qt code and see that rather than just using QMessageBox, the program checks whether QAPPLICATION_H is defined. If it isn't, then it uses some default system message box. Here's what it looks like:
bool Connect()
{
...
{
#ifdef QAPPLICATION_H
QMessageBox::critical(0,QString("Error!"),QString("Cannot Connect To PS3"));
#else
MessageBoxA(0,"Error!","Cannot Connect To PS3",MB_ICONINFORMATION);
#endif
return false;
}
else
{
...
#ifdef QAPPLICATION_H
QMessageBox::information(0,QString("Sucess!"),QString("Connected To PS3!"));
#else
MessageBoxA(0,"Sucess!", "Connected To PS3", MB_ICONINFORMATION);
#endif
return true;
}
}
Basically, my question is: what's the compatibility of QMessageBox? If I released a program that only uses QMessageBox, will people without Qt not be able to see the message pop up? I just don't want to have to check for this every time in my own code, and also the standard non-Qt box looks worse.
Qt is cross platform QMessageBox will be available on any platform you compile your code. I don't know why in the listed code is that define and the call to MessageBoxA, maybe the developer wanted to be able to display a more windows look and feel message box, in case the target platform is windows.

Visual Studio not building correctly

I've been having two different problems when trying to compile a C++ project today.
Sometimes it won't reflect any of my changes in the new build: If I change some wording on the output or change around actual functionality and compile and hit Start Debugging, it will behave exactly as it did before I made the changes. Hitting Clean or Rebuild Solution fixes this, but it takes about a full minute to compile this. I guess it's not detecting any changes, but in the output window I see it list the file names of files I made changes to...
I'm also getting a lot of "...already defined in main.obj" errors (one for every function and variable) whenever I try to use a header file or define a function outside of a class. Renaming the functions lets it compile once, but then the second compile will bring up the errors again. It kinda works if I just define the class in a .cpp file, don't use a header file, and don't use any functions outside of the class.
The project is an open-source program I downloaded the other day to mess with (I'm building a bot to control it). I didn't have the first problem until today, but the second one's always been happening. All I've done is add a file (two if you count both Bot.cpp and Bot.h); Bot.cpp includes Bot.h and some files from the program, and the program's main.cpp includes Bot.cpp.
I'll post some code I guess, but I can't find anything wrong with what I'm doing. I'm wondering if there's something I need to do to the existing files? (there were VS solution files included with the project that I used to open it, since VS Express doesn't help you create projects from existing code.)
//Bot.h
#ifndef _Bot_h_
#define _Bot_h_ 1
#include <string>
class Bot{
private:
uint32 inputs = 0;
bool active = false;
public:
Bot(){};
~Bot(){};
void Start();
void Stop();
void Run();
void Wait(int seconds);
void Press(int buttons);
};
#endif
//Bot.cpp
#ifndef _Bot_cpp_
#define _Bot_cpp_ 1
#include "main.h"
//Some other project files included between these
#include "Bot.h"
using namespace std;
void Bot::Start(){
if (active == false){
active = true;
Run();
}
}
void Bot::Stop(){
active = false;
}
void Bot::Run(){
while (active == true){
printf("Has my code updated?\n");
Wait(2);
}
}
//There are more functions defined here
#endif
All I've really done in the original source code is include Bot.cpp at the bottom of the list of includes in the main.cpp file, and then add a call to create the class. I'm a little out of practice with C/C++ so maybe it's something simple. I'm also bad at writing posts like this so if anything needs clarified I'll try to help.

Header guard deactivates IntelliSense within it

Well, let's say I have simple code like that:
#ifndef SYSTEM_CLASS_H
#define SYSTEM_CLASS_H
class SystemClass {
public:
SystemClass();
bool Initialize();
}
#endif
My issue is that everything within the header guard is ommited by IntelliSense. Is it intentional? Is there any way to disable it? I tried to look for solution on SO and Google, but I found nothing. I'm using VS 2012 Express, which recently got 3rd update.