GetFolderLocation() - Windows default directories

About 

This PRG source code implements a diagnostic tool displaying all folders provided as defaults by the Windows operating system. Default folders are of interest for application programmers who need to organize files of their applications.

The code uses the GetFolderLocation() API and passes to it numerous CSIDL_* #define constants. The latter is obtained via the WinDefines() function of The YUKON Project.

YUKON knows more than 25500 #define constants of the Windows platform SDK.

GetFolderLocation.prg 
// FILE: GetFolderLocation.prg
// Copyright (c) Hannes Ziegler, 2008 
// This file is published as Open Source for The YUKON Project (www.knowleXbase.com)
//
// This is a diagnostic tool identifying the system directories
// from #define constants of the Windows platform SDK. The program 
// displays a #define constant and the full path name of its
// corresponding system directory

#include "YukonRes.ch"

PROCEDURE Main
   LOCAL oCSIDL  := WinDefines( "CSIDL_*" ) // Hashtable of CSIDL_* #define constants
   LOCAL i, imax := HLen( oCSIDL )          // number of hashtable entries
   LOCAL cPath, cIDL, nIDL, pIdl, nErr      // Variables needed for this program

   // Open a Log file
   SET ALTERNATE ON
   SET ALTERNATE TO Csidl.txt

   // iterate all CSIDL_* constants
   FOR i:=1 TO imax
      cIDL := HGetKeyAt  ( oCSIDL, i ) // symbolic name of the WinSDK #define constant
      nIDL := HGetValueAt( oCSIDL, i ) // numeric value of the WinSDK #define constant
      pIDL := 0                        // prepare variable to receive pointer value

      // get the system directory corresponding with #define constant
      cPath:= GetFolderLocation( nIDL )

      IF cPath == NIL
         // not a system directory
         LOOP
      ENDIF

      // display name of #define constant and the path resulting from ITEMIDLIST
      ? cIDL, "=", cPath 
   NEXT

RETURN


FUNCTION GetFolderLocation( nIDL )
   LOCAL cPath := Space(260)
   LOCAL pIDL  := 0
   LOCAL nErr  := 0

    // obtain pointer to an ITEMIDLIST structure from the operating system
    nErr := SHELL32.SHGetFolderLocation( 0, nIDL, 0, 0, @pIDL )

    IF nErr <> 0
       // not a system directory
       RETURN NIL
    ENDIF

    // translate the ITEMIDLIST pointer to human readable form
    nErr := SHELL32.SHGetPathFromIDList( pIDL, @cPath )

    // free ITEMIDLIST structure allocated by the operating system
    nErr := SHELL32.ILFree( pIDL )
       
    // return path name as regular Xbase++ character string
RETURN TrimZero( cPath )