Best winapi questions in December 2011

What's the purpose of registering window classes?

7 votes

What's the purpose of registering a window class via WNDCLASSEX and RegisterClassEx() when creating a window in a Windows API application?

The separation of window attributes into CreateWindow() stuff and RegisterClass() stuff was done early on to enable the creation of uniformly-behaving windows. Dialog controls (buttons, listboxes, etc.) are a prime example - they all share a class. That means - they share a window procedure, that means - they share painting logic, input reactions, custom messages, notifications, etc.

On the app level, the most typical case when you have many windows of the same class is documents within a multiple-document interface. Sometimes people introduce app-specific controls. So the distinction serves its purpose.

How to follow a .lnk file programmatically

6 votes

We have a network drive full of shortcuts (.lnk files) that point to folders and I need to traverse them programmatically in a C# Winforms app.

What practical options do I have?

Add IWshRuntimeLibrary as a reference to your project. Add Reference, COM tab, Windows Scripting Host Object Model.

Here is how I get the properties of a shortcut:

IWshRuntimeLibrary.IWshShell wsh = new IWshRuntimeLibrary.WshShellClass();
IWshRuntimeLibrary.IWshShortcut sc = (IWshRuntimeLibrary.IWshShortcut)wsh.CreateShortcut(filename);

The shortcut object "sc" has a TargetPath property.