Initial snarf.
[shack.git] / libmojave / cutil / lm_compat_win32.c
blob37e05184b778eb0333565f6b7e9b36a2e0286cd9
1 /*
2 * Compatibility functions for the various versions of Win32.
4 * ----------------------------------------------------------------
6 * @begin[license]
7 * Copyright (C) 2004 Mojave Group, Caltech
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation,
12 * version 2.1 of the License.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 * Additional permission is given to link this library with the
24 * OpenSSL project's "OpenSSL" library, and with the OCaml runtime,
25 * and you may distribute the linked executables. See the file
26 * LICENSE.libmojave for more details.
28 * Author: Jason Hickey
29 * @email{jyh@cs.caltech.edu}
30 * @end[license]
32 #include <stdio.h>
33 #include <caml/mlvalues.h>
34 #include <caml/alloc.h>
35 #include <caml/memory.h>
36 #include <caml/fail.h>
37 #include <caml/custom.h>
39 #ifdef WIN32
40 /* Disable some of the warnings */
41 #pragma warning( disable : 4100 4201 )
43 #include <windows.h>
44 #include <tlhelp32.h>
45 #include <shlobj.h>
47 #include "lm_compat_win32.h"
50 * Pointers to the functions.
52 static HANDLE (__stdcall *OpenThreadF)(DWORD, BOOL, DWORD);
53 static BOOL (__stdcall *GetLongPathNameF)(LPCTSTR, LPTSTR, DWORD);
54 static HANDLE (__stdcall *CreateToolhelp32SnapshotF)(DWORD, DWORD);
55 static BOOL (__stdcall *Thread32FirstF)(HANDLE, LPTHREADENTRY32);
56 static BOOL (__stdcall *Thread32NextF)(HANDLE, LPTHREADENTRY32);
57 static HRESULT (__stdcall *SHGetFolderPathF)(HWND, int, HANDLE, DWORD, LPTSTR);
58 static BOOL (__stdcall *SHGetSpecialFolderPathF)(HWND, LPTSTR, int, BOOL);
61 * Compatibility.
63 int ExistsOpenThread(void)
65 return OpenThreadF ? 1 : 0;
68 HANDLE CompatOpenThread(DWORD arg1, BOOL arg2, DWORD arg3)
70 return OpenThreadF(arg1, arg2, arg3);
73 BOOL CompatGetLongPathName(LPCTSTR arg1, LPTSTR arg2, DWORD arg3)
75 BOOL b = 0;
77 if(GetLongPathNameF)
78 b = GetLongPathNameF(arg1, arg2, arg3);
79 return b;
82 HANDLE CompatCreateToolhelp32Snapshot(DWORD arg1, DWORD arg2)
84 return CreateToolhelp32SnapshotF(arg1, arg2);
87 BOOL CompatThread32First(HANDLE arg1, LPTHREADENTRY32 arg2)
89 return Thread32FirstF(arg1, arg2);
92 BOOL CompatThread32Next(HANDLE arg1, LPTHREADENTRY32 arg2)
94 return Thread32NextF(arg1, arg2);
97 HRESULT CompatSHGetFolderPath(HWND hwndOwner, int nFolder, HANDLE hToken, DWORD dwFlags, LPTSTR pszPath)
99 if(SHGetFolderPathF)
100 return SHGetFolderPathF(hwndOwner, nFolder, hToken, dwFlags, pszPath);
101 else if(SHGetSpecialFolderPathF) {
102 BOOL fCreate = nFolder & CSIDL_FLAG_CREATE ? TRUE : FALSE;
103 nFolder &= ~CSIDL_FLAG_CREATE;
104 fCreate = SHGetSpecialFolderPathF(hwndOwner, pszPath, nFolder, fCreate);
105 return fCreate ? S_OK : E_FAIL;
107 else
108 return E_NOTIMPL;
112 * Decide whether OpenThread is available.
114 static void init(void)
116 HINSTANCE hinst;
118 hinst = LoadLibrary(TEXT("KERNEL32"));
119 if(hinst != NULL) {
120 *(FARPROC *)&OpenThreadF = GetProcAddress(hinst, "OpenThread");
121 #ifdef UNICODE
122 *(FARPROC *)&GetLongPathNameF = GetProcAddress(hinst, "GetLongPathNameW");
123 #else
124 *(FARPROC *)&GetLongPathNameF = GetProcAddress(hinst, "GetLongPathNameA");
125 #endif
126 *(FARPROC *)&CreateToolhelp32SnapshotF = GetProcAddress(hinst, "CreateToolhelp32Snapshot");
127 *(FARPROC *)&Thread32FirstF = GetProcAddress(hinst, "Thread32First");
128 *(FARPROC *)&Thread32NextF = GetProcAddress(hinst, "Thread32Next");
131 hinst = LoadLibrary(TEXT("SHELL32"));
132 if(hinst != NULL) {
133 #ifdef UNICODE
134 *(FARPROC *)&SHGetFolderPathF = GetProcAddress(hinst, "SHGetFolderPathW");
135 *(FARPROC *)&SHGetSpecialFolderPathF = GetProcAddress(hinst, "SHGetSpecialFolderPathW");
136 #else
137 *(FARPROC *)&SHGetFolderPathF = GetProcAddress(hinst, "SHGetFolderPathA");
138 *(FARPROC *)&SHGetSpecialFolderPathF = GetProcAddress(hinst, "SHGetSpecialFolderPathA");
139 #endif
142 if(SHGetFolderPathF == 0) {
143 hinst = LoadLibrary(TEXT("SHFOLDER"));
144 if(hinst != NULL) {
145 #ifdef UNICODE
146 *(FARPROC *)&SHGetFolderPathF = GetProcAddress(hinst, "SHGetFolderPathW");
147 #else
148 *(FARPROC *)&SHGetFolderPathF = GetProcAddress(hinst, "SHGetFolderPathA");
149 #endif
155 * ML interface.
157 value lm_compat_init(value v_unit)
159 init();
160 return Val_unit;
163 #else /* !WIN32 */
165 value lm_compat_init(value v_unit)
167 return Val_unit;
170 #endif /* !WIN32 */