LanguagesHottest Forum Q&A on CodeGuru.com

Hottest Forum Q&A on CodeGuru.com

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

Introduction:

Lots of hot topics are covered in the Discussion Forums on CodeGuru. If you missed the forums this week, you missed some interesting ways to solve a problem. Some of the hot topics this week include:


What is the usage of WM_NULL?
(top)  

Thread:

Alex Rest found the WM_NULL message and was curious where somebody would need to use that.

I have found a strange Windows message.
It is described in MSDN so:

WM_NULL:
The WM_NULL message performs no operation. An application sends
the WM_NULL message if it wants to post a message that the recipient
window will ignore.

I propose a competition for the the best idea how to use it.

This message really isn’t strange at all. It can be used to verify whether or not an application is still responding. A famous window that does that is the Windows Task Manager by using SendMessageTimeout with WM_NULL. There are a few more methods were you have to use WM_NULL. JohnCz described them very well:

“WM_NULL also is used to filter messages in the WH_GETMESSAGE hook. Returning 0 from a hook does not prevent you from processing a message. Replacing a message with WM_NULL will filter the message properly.”

Another use is an ActiveX control when you decide that it has a toolbar and tooltips. Normally, an application message pump calls PreTranslateMessage; because the ActiveX control does not have one, a message hook can call PreTranslateMessage and, if handled, replace the original message with WM_NULL.


Does a Machine ID for copy protection purposes exist?
(top)  

Thread:

Jim1981 has asked an intersting question. He needs to get a unique ID from the system, so that saved files from his application can’t be opened on an another system.

Is there something like a unique machine ID that I can retrieve
so I can save a file and, when opened, know whether it was saved on
the same machine?

I don't want people using my saved files on any other machine apart
from the one on which it was saved on originally.

What is the best thing to grab that is unique to each machine?

First of all, you should know that nothing is guaranteed. Several hardware components contain a unique ID, but be advised that the components can be removed and implemented on another system. Another disadvantage is that when the customer’s hardware crashes, he won’t be able to open the saved files anymore. If you still want to use the unique ID, here are a few components that contain a unique ID and can be used for that purpose.

  • Serial number of the primary hard disk.
    Use GetVolumeInformation(); to retrive the information.
  • MAC address of the network card.
  • Serial number of the CPU
    Use GetSystemInfo to retrive the information.
  • Write to the Registry.

The best idea would be to use a combination of the above described methods. Also, take a look at the following articles:


Is there any range for WM_XXX messages that I could use without having any troubles with the Windows message handlers?
(top)  

Thread:

Zaph-0 wants to know whether there is any certain range to define WM_XXX messages for his own purposes.

Is there a certain range where I can define my WM_xxx messages
without causing any problems to Windows message handlers?

How about > 1000000?

In general, you can use both ‘WM_USER’ or ‘WM_APP‘ for user-defined messages. However, you rather should use ‘WM_APP’ as a base for your user-defined messages because it provides fewer problems. Windows itself uses the range ‘WM_USER + x’ for several messages; therefore, you can run into conflict problems pretty easily. Besides that, take a look at the following MSDN entry: WM_USER Notification


How can I validate a auto_ptr?
(top)  

Thread:

kvpreeth is using a auto_ptr, but unfortunately, he doesn’t know how to validate it. Do you?

I've a basic question regarding auto_ptr.

Assume this:

auto_ptr<T> pt( new T(1) );

Now, how do I validate pt before using pt.get()? In other words,
how can I make sure that pt has been allocated?

Is there any way to validate pt? I am using this for the first time.

Well, you answered your own question. The ‘get’ function will return 0 in case of a failure and ‘new’ is supposed to throw a ‘bad_alloc’ exception in case of a failure. Also, take a look at the following article: Using auto_ptr Effectively


Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories