]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/winapi.c
Update README.mingw32 to include gdb info
[bacula/bacula] / bacula / src / win32 / compat / winapi.c
1 /*
2    Bacula® - The Network Backup Solution
3
4    Copyright (C) 2003-2008 Free Software Foundation Europe e.V.
5
6    The main author of Bacula is Kern Sibbald, with contributions from
7    many others, a complete list can be found in the file AUTHORS.
8    This program is Free Software; you can redistribute it and/or
9    modify it under the terms of version two of the GNU General Public
10    License as published by the Free Software Foundation and included
11    in the file LICENSE.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23    Bacula® is a registered trademark of John Walker.
24    The licensor of Bacula is the Free Software Foundation Europe
25    (FSFE), Fiduciary Program, Sumatrastrasse 25, 8006 Zürich,
26    Switzerland, email:ftf@fsfeurope.org.
27 */
28 /*
29  * Windows APIs that are different for each system.
30  *   We use pointers to the entry points so that a
31  *   single binary will run on all Windows systems.
32  *
33  *     Kern Sibbald MMIII
34  */
35
36 #include "bacula.h"
37
38 // init with win9x, but maybe set to NT in InitWinAPI
39 DWORD g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
40 DWORD g_MinorVersion = 0;
41 DWORD g_MajorVersion = 0;
42
43 /* API Pointers */
44
45 t_OpenProcessToken      p_OpenProcessToken = NULL;
46 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
47 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
48
49 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
50
51 t_CreateFileA           p_CreateFileA = NULL;
52 t_CreateFileW           p_CreateFileW = NULL;
53 t_CreateDirectoryA      p_CreateDirectoryA;
54 t_CreateDirectoryW      p_CreateDirectoryW;
55
56 t_wunlink               p_wunlink = NULL;
57 t_wmkdir                p_wmkdir = NULL;
58
59 t_GetFileAttributesA    p_GetFileAttributesA = NULL;
60 t_GetFileAttributesW    p_GetFileAttributesW = NULL;
61
62 t_GetFileAttributesExA  p_GetFileAttributesExA = NULL;
63 t_GetFileAttributesExW  p_GetFileAttributesExW = NULL;
64
65 t_SetFileAttributesA    p_SetFileAttributesA = NULL;
66 t_SetFileAttributesW    p_SetFileAttributesW = NULL;
67 t_BackupRead            p_BackupRead = NULL;
68 t_BackupWrite           p_BackupWrite = NULL;
69 t_WideCharToMultiByte   p_WideCharToMultiByte = NULL;
70 t_MultiByteToWideChar   p_MultiByteToWideChar = NULL;
71
72 t_AttachConsole         p_AttachConsole = NULL;
73
74 t_FindFirstFileA        p_FindFirstFileA = NULL;
75 t_FindFirstFileW        p_FindFirstFileW = NULL;
76
77 t_FindNextFileA         p_FindNextFileA = NULL;
78 t_FindNextFileW         p_FindNextFileW = NULL;
79
80 t_SetCurrentDirectoryA  p_SetCurrentDirectoryA = NULL;
81 t_SetCurrentDirectoryW  p_SetCurrentDirectoryW = NULL;
82
83 t_GetCurrentDirectoryA  p_GetCurrentDirectoryA = NULL;
84 t_GetCurrentDirectoryW  p_GetCurrentDirectoryW = NULL;
85
86 t_GetVolumePathNameW    p_GetVolumePathNameW = NULL;
87 t_GetVolumeNameForVolumeMountPointW p_GetVolumeNameForVolumeMountPointW = NULL;
88
89 t_SHGetFolderPath       p_SHGetFolderPath = NULL;
90
91 void 
92 InitWinAPIWrapper() 
93 {
94    OSVERSIONINFO osversioninfo = { sizeof(OSVERSIONINFO) };
95
96    // Get the current OS version
97    if (!GetVersionEx(&osversioninfo)) {
98       g_platform_id = 0;
99    } else {
100       g_platform_id = osversioninfo.dwPlatformId;
101       g_MinorVersion = osversioninfo.dwMinorVersion;
102       g_MajorVersion = osversioninfo.dwMajorVersion;
103    }
104
105    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
106    if (hLib) {
107       /* create file calls */
108       p_CreateFileA = (t_CreateFileA)GetProcAddress(hLib, "CreateFileA");
109       p_CreateDirectoryA = (t_CreateDirectoryA)GetProcAddress(hLib, "CreateDirectoryA");
110
111       /* attribute calls */
112       p_GetFileAttributesA = (t_GetFileAttributesA)GetProcAddress(hLib, "GetFileAttributesA");
113       p_GetFileAttributesExA = (t_GetFileAttributesExA)GetProcAddress(hLib, "GetFileAttributesExA");
114       p_SetFileAttributesA = (t_SetFileAttributesA)GetProcAddress(hLib, "SetFileAttributesA");
115
116       /* process calls */
117       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
118           GetProcAddress(hLib, "SetProcessShutdownParameters");
119
120       /* char conversion calls */
121       p_WideCharToMultiByte = (t_WideCharToMultiByte)
122           GetProcAddress(hLib, "WideCharToMultiByte");
123       p_MultiByteToWideChar = (t_MultiByteToWideChar)
124           GetProcAddress(hLib, "MultiByteToWideChar");
125
126       /* find files */
127       p_FindFirstFileA = (t_FindFirstFileA)GetProcAddress(hLib, "FindFirstFileA"); 
128       p_FindNextFileA = (t_FindNextFileA)GetProcAddress(hLib, "FindNextFileA");
129
130       /* get and set directory */
131       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
132           GetProcAddress(hLib, "GetCurrentDirectoryA");
133       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
134           GetProcAddress(hLib, "SetCurrentDirectoryA");
135
136       if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
137          p_CreateFileW = (t_CreateFileW)
138              GetProcAddress(hLib, "CreateFileW");
139          p_CreateDirectoryW = (t_CreateDirectoryW)
140              GetProcAddress(hLib, "CreateDirectoryW");
141
142          /* backup calls */
143          p_BackupRead = (t_BackupRead)GetProcAddress(hLib, "BackupRead");
144          p_BackupWrite = (t_BackupWrite)GetProcAddress(hLib, "BackupWrite");
145
146          p_GetFileAttributesW = (t_GetFileAttributesW)
147              GetProcAddress(hLib, "GetFileAttributesW");
148          p_GetFileAttributesExW = (t_GetFileAttributesExW)
149              GetProcAddress(hLib, "GetFileAttributesExW");
150          p_SetFileAttributesW = (t_SetFileAttributesW)
151              GetProcAddress(hLib, "SetFileAttributesW");
152          p_FindFirstFileW = (t_FindFirstFileW)
153              GetProcAddress(hLib, "FindFirstFileW");
154          p_FindNextFileW = (t_FindNextFileW)
155              GetProcAddress(hLib, "FindNextFileW");
156          p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
157              GetProcAddress(hLib, "GetCurrentDirectoryW");
158          p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
159              GetProcAddress(hLib, "SetCurrentDirectoryW");
160
161          /* some special stuff we need for VSS
162             but static linkage doesn't work on Win 9x */
163          p_GetVolumePathNameW = (t_GetVolumePathNameW)
164              GetProcAddress(hLib, "GetVolumePathNameW");
165          p_GetVolumeNameForVolumeMountPointW = (t_GetVolumeNameForVolumeMountPointW)
166              GetProcAddress(hLib, "GetVolumeNameForVolumeMountPointW");
167
168          p_AttachConsole = (t_AttachConsole)
169              GetProcAddress(hLib, "AttachConsole");
170       }
171    }
172    
173    if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
174       hLib = LoadLibraryA("MSVCRT.DLL");
175       if (hLib) {
176          /* unlink */
177          p_wunlink = (t_wunlink)
178          GetProcAddress(hLib, "_wunlink");
179          /* wmkdir */
180          p_wmkdir = (t_wmkdir)
181          GetProcAddress(hLib, "_wmkdir");
182       }
183
184       hLib = LoadLibraryA("ADVAPI32.DLL");
185       if (hLib) {
186          p_OpenProcessToken = (t_OpenProcessToken)
187             GetProcAddress(hLib, "OpenProcessToken");
188          p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
189             GetProcAddress(hLib, "AdjustTokenPrivileges");
190          p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
191             GetProcAddress(hLib, "LookupPrivilegeValueA");
192       }
193    }
194
195    hLib = LoadLibraryA("SHELL32.DLL");
196    if (hLib) {
197       p_SHGetFolderPath = (t_SHGetFolderPath)
198          GetProcAddress(hLib, "SHGetFolderPathA");
199    } else {
200       /* If SHELL32 isn't found try SHFOLDER for older systems */
201       hLib = LoadLibraryA("SHFOLDER.DLL");
202       if (hLib) {
203          p_SHGetFolderPath = (t_SHGetFolderPath)
204             GetProcAddress(hLib, "SHGetFolderPathA");
205       }
206    }
207    atexit(Win32ConvCleanupCache);
208 }