0

I used RegisterDeviceNotification API in my code. It is not working (getting linker errror), if I'm not using #define WINVER 0x501.

1) When I look into window Winuser.h , RegisterDeviceNotificationA is defined under #if(WINVER >= 0x0500). What is the reason for this?

2) To make RegisterDeviceNotificationA work I declared all the declarations in my own .h file without #if(WINVER >= 0x0500). Is this right way?

3) My another question here is there any problem in declaring RegisterDeviceNotificationA without winver? what is the use of Winver? Please explain me in detail.

2vision2
  • 4,933
  • 16
  • 83
  • 164
  • 3
    Not exact duplicate, but this explains WINVER http://stackoverflow.com/questions/1439752/what-is-winver – tinman Jul 17 '12 at 09:50
  • It means that the function is only available on Windows 2000 and up. So your program won't start when the user has an older version. That's unlikely. Always set WINVER, use 0x501 for compatibility with XP. – Hans Passant Jul 17 '12 at 10:49
  • @HansPassant Is there any impact if am not defining it? I'm coding in XP and it works fine without WINVER? – 2vision2 Jul 17 '12 at 10:52

1 Answers1

2

The primary purpose of definitions like WINVER is to compile old code with recent compilers.

If you are writing new code, you need to define this variable and maybe several others in your cpp file or in your project file. The value is the version of Windows that you are targeting. This is how Microsoft designed it.

It is extremely bad practice to duplicate system definitions into your code.

Kirill Kobelev
  • 10,252
  • 6
  • 30
  • 51
  • It is extremely bad practice to duplicate system definitions in your code. In my case does is there any impact of doing like this? – 2vision2 Jul 17 '12 at 10:31
  • Well, in a relatively small app, a few definition can be ok. I came one over a case when whole headers from Win DDK were duplicated and modified. After a while this created real MESS because Windows team modified them also. – Kirill Kobelev Jul 17 '12 at 10:33
  • One of the reason I duplicated this definition is that if i define #define WINVER 0x501 in my app, the behavior of RASPHONE.exe is different. That is it displays a gui to start the dialup, which I don't expect. If posible have a look into this post http://stackoverflow.com/questions/11469514/difference-between-rasphone-exe-and-rasdial-exe – 2vision2 Jul 17 '12 at 10:40
  • I never used these apps. Are you using them as binaries? It is not clear how compiling your app can influence behavior of others... Explain this in your orig post. – Kirill Kobelev Jul 17 '12 at 10:43
  • This answer is pretty wrong, other than the last sentence. It has nothing to do with "old code" or "new compilers", everything to do with the version of Windows your user is running. Check the answer that tinman linked. – Hans Passant Jul 17 '12 at 10:46
  • @Hans, I worked in Microsoft. I know what I am writing. This info in not from nowhere. – Kirill Kobelev Jul 17 '12 at 10:48
  • Compiling doesnt cause any issue in that case. only the behaviour is different if am using RASPHONE.exe with Winver defined. – 2vision2 Jul 17 '12 at 10:51
  • Hmm, that explains it I guess, you are talking about old Microsoft code. – Hans Passant Jul 17 '12 at 10:51
  • @Hans, you are kind of funny. First of all, there is no fundamental difference between what I wrote and that post. Second, WINVER allows hiding definitions that were not present in older versions of these headers. If you compile old code (regardless if it is MS or not MS) with recent headers, you will have problems. Picking up the right system dll is a different story. This is done with SHIM technology (that does not always work). – Kirill Kobelev Jul 17 '12 at 10:56
  • @user1317084, most likely there is some **other** API that has different behavior in the old system and the recent one. Try to narrow it down.. – Kirill Kobelev Jul 17 '12 at 11:05