1

Suppose I have a program within the VC++ environment. This program needs to modify itself in order to use certain code for a different operating system.

Let's say, for example, that the program will modify the code to do A for Windows XP and for Windows 7 it will modify the code to do B. The code for the action A located on the file "a.h" and the code for the action B located on the file "b.h".

Let's say I have a function whose purpose is to detect the operating system. If I wish to load just one of the files by detecting the OS, how can I include a file by such terms like activating a function?

2
  • 1
    It is a little unclear exactly what you are looking to do - do you want two different executables, one for Windows XP and another for Windows 7? Or one executable to cope with both? What do you mean by "modify code"? Commented Dec 31, 2012 at 14:47
  • @MatsPetersson I want a single executable to cope with both. My program should work with a network adapter. As I did proceed with my program I found out that it works for the Windows XP and nothing beyond. So I came across with the information that since windows vista there are some changes with the network adapter. Well,instead of creating two executables I want to create a single one which will maintain the adapter propertly. Commented Dec 31, 2012 at 15:13

3 Answers 3

3

You can't do such a thing as you exposed it.

The #include mechanism is handled by the precompiler, that runs at the moment of the compilation, not at runtime, thus, once your binary is compiled, the all the precompiler choices are already made and made their way into the final executable.

To do a different thing depending on the version of Windows you have to include the code for both behaviors and choose the correct behavior at runtime, e.g. using the results of the GetVersionEx API.

Sign up to request clarification or add additional context in comments.

5 Comments

If the code is not small you could compile it as a library and then load the appropriate library at runtime.
@diolemo: of course, but this is again runtime behavior triggered by a runtime check, not a check done with the precompiler as he requested.
Yes the library needs to be loaded as runtime.
@diolemo: and mine was a specification of your comment :) Sorry, I didn't mean to sound rude.
@MatteoItalia Thank you very much. As a matter of fact when I think of my request I see that such thing cannot be done. diolemo Thank you too :) I'll do as you mentioned
2

You imply that you want to use the preprocessor to conditionally build a different executable rather than influence the run-time behaviour. The consequence of this is that you will have an executable for each different operating system. This may or may not be what you want and brings with it it's own pitfalls.

Generally, it is worth avoiding conditional compilation unless you are in a situation where you have code which simply will not build on any other environment. Possibly a better solution would be to derive the specific functionality at runtime. This can either be done by detecting the OS or by configuration etc.

There are ways of dynamically loading libraries specifically at runtime which you might want to consider. You can use the dlopen family of functions on Linux to do this (Not sure about Windows but I'm sure there's something similar.) One drawback is that there is little by way of checking so if you get the interface wrong, look forward to seg faults. Also, the interface is restricted to C linkage.

1 Comment

On Windows you can use LoadLibrary() to load libraries at runtime.
1

Right, so it's entirely possible to create an executable that WORKS on modern and old versions of Windows. The typical way to do that is to check the actual version, and then vary the code. MS tells you how to check the version here:

http://msdn.microsoft.com/en-gb/library/windows/desktop/ms724429(v=vs.85).aspx

Then you just make a flag, say bool isWndowsXP that is set by the checking code, and where relevant do if(isWindowsXP) do_stuff_xp_way(); else do_stuff_otherway();

If you want more specific advice, you really need to provide the functionality that is hidden behind A and B in your question.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.