Windows Programming/Programming Shell Extensions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

The Windows Shell consists primarily of explorer.exe, the graphical user interface that displays folders, icons, and the desktop. Explorer.exe is written primarily in C++, so to write extension modules is going to require OO programming. However, there are several functions, in <windows.h> that a C program can use to interact with the shell to perform some basic tasks. First, we will describe some of the basic areas of your shell.

The Shell[edit | edit source]

explorer.exe, the Windows shell program, has a number of different functions that can be used to cause your program to perform tasks like the shell would. We will run over a few of them here:

ShellExecute[edit | edit source]

The ShellExecute function takes a file and a pathname as arguments, and essentially performs whatever task the shell would perform if the file in question was double-clicked. For instance, calling a ShellExecute on "MyFile.txt" would open notepad, and would display MyFile.txt. Similarly, calling ShellExecute on a hyperlink will automatically open Internet Explorer (or your default browser) and will open the specified URL.

HINSTANCE ShellExecute(HWND hwnd,
                       LPCTSTR lpOperation,
                       LPCTSTR lpFile,
                       LPCTSTR lpParameters,
                       LPCTSTR lpDirectory,
                       INT nShowCmd);
The handle to the parent window of the operation
The text name of the operation. These are the strings that appear in the menu when you right-click an icon. Some common values are "edit", "run", or "execute".
the name of the target file
If lpFile specifies an executable file, the lpParameters field contains the commandline arguments to the executable, if any
specifies what directory to perform the operation in

The Desktop[edit | edit source]

The Taskbar[edit | edit source]

The System Tray[edit | edit source]

The system tray is the area in the lower-right hand side of the screen that contains the clock and a number of different icons. Icons can be added to the system tray by using a simple API call. The function call to be used is the Shell_NotifyIcon function, and we will explain it here.


This function takes 2 arguments. The first argument is a message, and the second argument contains more information on the message. There are 3 messages possible:

NIM_ADD    Add a new icon to the system tray
NIM_DELETE Delete an icon from the system tray
NIM_MODIFY Modify an existing icon in the system tray

We can see that the second argument is a pointer to the NOTIFYICONDATA structure. This structure contains fields as such:

typedef struct _NOTIFYICONDATA { 
 DWORD cbSize;
 HWND hWnd; 
 UINT uFlags; 
 UINT uCallbackMessage; 
 HICON hIcon; 
 WCHAR szTip[64]; 
This should reflect the size of the NOTIFYICON structure. If we have a structure called "nid", we usually assign this member as follows:
nid.cbSize = sizeof(NOTIFYICONDATA);
This field contains the handle of the parent window. When the notify icon is clicked, a corresponding message will be sent to this window.
This is a numerical identifier for the icon in question. A program can have up to 12 different icons at once.
This is the message that will be sent to your window. This message should be WM_USER or greater. The WPARAM field of the message will contain the uID of the icon in question.
This member tells the shell which fields are valid fields. If a field does not contain any information, or if you don't want to use a particular field to set or modify a value, do not list them in the uFlags member. The possible values are NIF_ICON, NIF_MESSAGE, and NIF_TIP.
a handle to the icon to be displayed in the system tray. must be a 16x16 icon.
A short string that contains a "tooltip" for the icon. When the mouse is hovered over the icon, the tooltip will be displayed. leave this blank, if you do not want to have a tool tip displayed.

The Recycle Bin[edit | edit source]