Hottest Forum Q&A on CodeGuru for the week of April 25th, 2004

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



About the Author

Sonu Kapoor

Sonu Kapoor is an ASP.NET MVP and MCAD. He is the owner of the popular .net website http://dotnetslackers.com. DotNetSlackers publishs the latest .net news and articles - it contains forums and blogs as well. His blog can be seen at: http://dotnetslackers.com/community/blogs/sonukapoor/

Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 22, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Database professionals — whether developers or DBAs — can often save valuable time by learning to get the most from their new or existing productivity tools. Whether you're responsible for managing database projects, performing database health checks and reporting, analyzing code, or measuring software engineering metrics, it's likely you're not taking advantage of some of the lesser-known features of Toad from Dell. Attend this live …

  • Confused by all the agile advice? Relax! With the Agile for Dummies eBook by your side you'll learn the fundamentals of agile and how to increase the productivity of your software teams while enabling them to produce higher-quality solutions that better fulfill customer needs much faster.

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds