Mini Tutorial: Finding the Windows Desktop in Delphi.

 

This tutorial provides step by step instructions for locating the desktop in Delphi.

 

Introduction (skip if you want)

The Delphi function ShellGetSpecialFolderPath can be used to find the file path corresponding to special folders such as the recycle bin (CSIDL_BITBUCKET), the Start menu (CSIDL_STARTMENU) and, of course, the Desktop (CSIDL_DESKTOP);

  function ShellGetSpecialFolderPath( ahwnd: HWND;  csidl: TCSIDL ): String;

 

 

ahwnd

Windows Object Handle. You can use the default handle.

 

 

csidl

A system folder constant such as CSIDL_DESKTOP. This constant tells the function what you are looking for.

 

 

You can also use ShGetSpecialFolderLocation, which is discussed later in this document.

 

Lets get started

Create a new application by choosing New from Delphi’s File menu and choosing Application.

 

Drop a Label component on the form and set the properties as follows

Name:      Label1

Autosize:  True

 

Drop a Button on the form and set the properties to:

Name:      Button1

 

Add the following to your Unit1 uses clause

UPTShellUtils

 

Your uses clause should now look something like this:

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, UPTShellUtils;

 

Add the following code to the On Click Event handler for Button1

procedure TForm1.Button1Click(Sender: TObject);

begin

  Label1.Caption := ShellGetSpecialFolderPath(Handle, CSIDLDESKTOP);

end;

 

Now run your program. The results should look something like this.

 

 

 

Alternative method: Using ShGetSpecialFolderLocation.

You can also use the ShGetSpecialFolderLocation function. Full details are available in the WIN32.HLP file. This useful Windows API function returns the location of a number of key locations such as the location of the recycle bin (CSIDL_BITBUCKET), the Start menu (CSIDL_STARTMENU) and, of course, the Desktop (CSIDL_DESKTOP);

 

The C declaration is:

  WINSHELLAPI HRESULT WINAPI SHGetSpecialFolderLocation(;HWND hwndOwner, int nFolder,  LPITEMIDLIST *ppidl);

 

The Object Paskcal (Delphi) declaration is:

  ShGetSpecialFolderLocation(Handle : HWND; Folder : Integer; Pidl : PItemIDList)

 

 

Handle

Windows Object Handle. You can use the default Handle.

 

Folder

Use the constant CSIDL_DESKTOP for the Windows desktop.

 

Pidl

Pidl is a pointer to an ItemIDList structure.

 

SHGetSpecialFolder location places the relative path from the root of the virtual namespace (i.e. the desktop) into the PItemIDList item identifier list. It is then necessary to convert this value to a file system path using the ShGetPathfromIDList API.

  function SHGetPathFromIDList( pidl: PItemIdList;  pszPath: PChar ): Bool;

 

Steps

Create a new application by choosing New from Delphi’s File menu and choosing Application.

 

Drop a Label component on the form and set the properties as follows

Name:      Label1

Autosize:  True

 

Drop a Button on the form and set the properties to:

Name:      Button1

 

Add the following to your Unit1 uses clause

ShLOBJ, ActiveX;

 

Your uses clause should now look something like this:

unit Unit1;

 

interface

 

uses

  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,

  Dialogs, StdCtrls, ShLOBJ, ActiveX;

 

Add the following code to the On Click Event handler for Button1

 

procedure TForm1.Button1Click(Sender: TObject);

var

  pidl: PItemIDList;

  Buf: array [0..MAX_PATH] of Char;

begin

  if Succeeded( ShGetSpecialFolderLocation( Handle, CSIDL_DESKTOP, pidl ))

    Then Begin

     If ShGetPathfromIDList(pidl, Buf ) Then

        Label1.Caption := Buf;

   CoTaskMemFree(pidl);

  end;

end;

 

 

A general purpose desktop locator with ShGetSpecialFolderLocation:

 

We have learned that the ShGetSpecialFolderLocation API is used to find the desktop location. We have also learned that ShellAPI and SHLOBJ must be added to your units uses clause to use this API. We can now write a general purpose function to obtain the location of the Windows desktop.

 

// Get location of the Windows desktop virtual folder at the root of the name space.

Function Dir_WinDesktop(Handle : THandle) : String;

var

  pidl: PItemIDList;

  Buf: array [0..MAX_PATH] of Char;

begin

  if Succeeded( ShGetSpecialFolderLocation( Handle, CSIDL_DESKTOP, pidl ))

    Then Begin

     If ShGetPathfromIDList(pidl, Buf ) Then

        Result := Buf;

   CoTaskMemFree(pidl);

  end;

end;

 

 

 

 

 

References

 

Win32 Programmers Reference (WIN32.HLP)

Delphi 6 online help.

The Tomes of Delphi 3: Win32 Core API, 1988, Wordware Publishing Inc. ISBN 1-55622-556-3

 

 

Author

Prepared by Paul Griffiths November 2002.

www.jpgriffiths.com