6ea722b43725416f86fc5968819938233ff512e6
[haskell-directory.git] / cbits / directory.c
1 #if defined(__GLASGOW_HASKELL__) || defined(__HUGS__)
2 /* 
3  * (c) The University of Glasgow 2002
4  *
5  */
6
7 #define INLINE
8 #include "HsDirectory.h"
9
10 /*
11  * Function: __hscore_getFolderPath()
12  *
13  * Late-bound version of SHGetFolderPath(), coping with OS versions
14  * that have shell32's lacking that particular API.
15  *
16  */
17 #if defined(_MSC_VER) || defined(__MINGW32__) || defined(_WIN32)
18 typedef HRESULT (*HSCORE_GETAPPFOLDERFUNTY)(HWND,int,HANDLE,DWORD,char*);
19 int
20 __hscore_getFolderPath(HWND hwndOwner,
21                        int nFolder,
22                        HANDLE hToken,
23                        DWORD dwFlags,
24                        char*  pszPath)
25 {
26     static int loaded_dll = 0;
27     static HMODULE hMod = (HMODULE)NULL;
28     static HSCORE_GETAPPFOLDERFUNTY funcPtr = NULL;
29     /* The DLLs to try loading entry point from */
30     char* dlls[] = { "shell32.dll", "shfolder.dll" };
31     
32     if (loaded_dll < 0) {
33         return (-1);
34     } else if (loaded_dll == 0) {
35         int i;
36         for(i=0;i < sizeof(dlls); i++) {
37             hMod = LoadLibrary(dlls[i]);
38             if ( hMod != NULL &&
39                  (funcPtr = (HSCORE_GETAPPFOLDERFUNTY)GetProcAddress(hMod, "SHGetFolderPathA")) ) {
40                 loaded_dll = 1;
41                 break;
42             }
43         }
44         if (loaded_dll == 0) {
45             loaded_dll = (-1);
46             return (-1);
47         }
48     }
49     /* OK, if we got this far the function has been bound */
50     return (int)funcPtr(hwndOwner,nFolder,hToken,dwFlags,pszPath);
51     /* ToDo: unload the DLL on shutdown? */
52 }
53 #endif /* WIN32 */
54 #endif /* !__NHC__ */
55