]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/lib/winapi.c
- Correct compiler complaints in wx-console and tray-monitor.
[bacula/bacula] / bacula / src / lib / winapi.c
1 /*
2  * Windows APIs that are different for each system.
3  *   We use pointers to the entry points so that a
4  *   single binary will run on all Windows systems.
5  *
6  *     Kern Sibbald MMIII
7  */
8 /*
9    Copyright (C) 2003-2005 Kern Sibbald
10
11    This program is free software; you can redistribute it and/or
12    modify it under the terms of the GNU General Public License
13    version 2 as amended with additional clauses defined in the
14    file LICENSE in the main source directory.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
19    the file LICENSE for additional details.
20
21  */
22
23 #include "bacula.h"
24
25 #if defined(HAVE_CYGWIN) || defined(HAVE_WIN32)
26
27 #include "winapi.h"
28
29 #ifdef WIN32_VSS
30 #include "vss.h"   
31 #endif
32
33 // init with win9x, but maybe set to NT in InitWinAPI
34 DWORD  g_platform_id = VER_PLATFORM_WIN32_WINDOWS;
35 #ifdef WIN32_VSS
36 /* preset VSSClient to NULL */
37 VSSClient *g_pVSSClient = NULL;
38 #endif
39
40
41 /* API Pointers */
42
43 t_OpenProcessToken      p_OpenProcessToken = NULL;
44 t_AdjustTokenPrivileges p_AdjustTokenPrivileges = NULL;
45 t_LookupPrivilegeValue  p_LookupPrivilegeValue = NULL;
46
47 t_SetProcessShutdownParameters p_SetProcessShutdownParameters = NULL;
48
49 t_CreateFileA   p_CreateFileA = NULL;
50 t_CreateFileW   p_CreateFileW = NULL;
51
52 t_wunlink p_wunlink = NULL;
53 t_wmkdir p_wmkdir = NULL;
54 t_wopen p_wopen = NULL;
55
56 t_GetFileAttributesA    p_GetFileAttributesA = NULL;
57 t_GetFileAttributesW    p_GetFileAttributesW = NULL;
58
59 t_GetFileAttributesExA  p_GetFileAttributesExA = NULL;
60 t_GetFileAttributesExW  p_GetFileAttributesExW = NULL;
61
62 t_SetFileAttributesA    p_SetFileAttributesA = NULL;
63 t_SetFileAttributesW    p_SetFileAttributesW = NULL;
64 t_BackupRead            p_BackupRead = NULL;
65 t_BackupWrite           p_BackupWrite = NULL;
66 t_WideCharToMultiByte p_WideCharToMultiByte = NULL;
67 t_MultiByteToWideChar p_MultiByteToWideChar = NULL;
68
69 t_FindFirstFileA p_FindFirstFileA = NULL;
70 t_FindFirstFileW p_FindFirstFileW = NULL;
71
72 t_FindNextFileA p_FindNextFileA = NULL;
73 t_FindNextFileW p_FindNextFileW = NULL;
74
75 t_SetCurrentDirectoryA p_SetCurrentDirectoryA = NULL;
76 t_SetCurrentDirectoryW p_SetCurrentDirectoryW = NULL;
77
78 t_GetCurrentDirectoryA p_GetCurrentDirectoryA = NULL;
79 t_GetCurrentDirectoryW p_GetCurrentDirectoryW = NULL;
80
81 #ifdef WIN32_VSS
82 void 
83 VSSCleanup()
84 {
85    if (g_pVSSClient)
86       delete (g_pVSSClient);
87 }
88 #endif
89
90 void 
91 InitWinAPIWrapper() 
92 {
93    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
94    if (hLib) {
95       /* create file calls */
96       p_CreateFileA = (t_CreateFileA)
97           GetProcAddress(hLib, "CreateFileA");
98       p_CreateFileW = (t_CreateFileW)
99           GetProcAddress(hLib, "CreateFileW");      
100
101       /* attribute calls */
102       p_GetFileAttributesA = (t_GetFileAttributesA)
103           GetProcAddress(hLib, "GetFileAttributesA");
104       p_GetFileAttributesW = (t_GetFileAttributesW)
105           GetProcAddress(hLib, "GetFileAttributesW");
106       p_GetFileAttributesExA = (t_GetFileAttributesExA)
107           GetProcAddress(hLib, "GetFileAttributesExA");
108       p_GetFileAttributesExW = (t_GetFileAttributesExW)
109           GetProcAddress(hLib, "GetFileAttributesExW");
110       p_SetFileAttributesA = (t_SetFileAttributesA)
111           GetProcAddress(hLib, "SetFileAttributesA");
112       p_SetFileAttributesW = (t_SetFileAttributesW)
113           GetProcAddress(hLib, "SetFileAttributesW");
114       /* process calls */
115       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
116           GetProcAddress(hLib, "SetProcessShutdownParameters");
117       /* backup calls */
118       p_BackupRead = (t_BackupRead)
119           GetProcAddress(hLib, "BackupRead");
120       p_BackupWrite = (t_BackupWrite)
121           GetProcAddress(hLib, "BackupWrite");
122       /* char conversion calls */
123       p_WideCharToMultiByte = (t_WideCharToMultiByte)
124           GetProcAddress(hLib, "WideCharToMultiByte");
125       p_MultiByteToWideChar = (t_MultiByteToWideChar)
126           GetProcAddress(hLib, "MultiByteToWideChar");
127
128       /* find files */
129       p_FindFirstFileA = (t_FindFirstFileA)
130           GetProcAddress(hLib, "FindFirstFileA"); 
131       p_FindFirstFileW = (t_FindFirstFileW)
132           GetProcAddress(hLib, "FindFirstFileW");       
133       p_FindNextFileA = (t_FindNextFileA)
134           GetProcAddress(hLib, "FindNextFileA");
135       p_FindNextFileW = (t_FindNextFileW)
136           GetProcAddress(hLib, "FindNextFileW");
137       /* set and get directory */
138       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
139           GetProcAddress(hLib, "SetCurrentDirectoryA");
140       p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
141           GetProcAddress(hLib, "SetCurrentDirectoryW");       
142       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
143           GetProcAddress(hLib, "GetCurrentDirectoryA");
144       p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
145           GetProcAddress(hLib, "GetCurrentDirectoryW");      
146       FreeLibrary(hLib);
147    }
148    
149    hLib = LoadLibraryA("MSVCRT.DLL");
150    if (hLib) {
151       /* unlink */
152       p_wunlink = (t_wunlink)
153       GetProcAddress(hLib, "_wunlink");
154       /* wmkdir */
155       p_wmkdir = (t_wmkdir)
156       GetProcAddress(hLib, "_wmkdir");
157       /* wopen */
158       p_wopen = (t_wopen)
159       GetProcAddress(hLib, "_wopen");
160         
161       FreeLibrary(hLib);
162    }
163    
164    hLib = LoadLibraryA("ADVAPI32.DLL");
165    if (hLib) {
166       p_OpenProcessToken = (t_OpenProcessToken)
167          GetProcAddress(hLib, "OpenProcessToken");
168       p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
169          GetProcAddress(hLib, "AdjustTokenPrivileges");
170       p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
171          GetProcAddress(hLib, "LookupPrivilegeValueA");
172       FreeLibrary(hLib);
173    }
174
175    // do we run on win 9x ???
176    OSVERSIONINFO osversioninfo;   
177    osversioninfo.dwOSVersionInfoSize = sizeof(osversioninfo);
178
179    DWORD dwMinorVersion;
180
181    // Get the current OS version
182    if (!GetVersionEx(&osversioninfo)) {
183       g_platform_id = 0;
184       dwMinorVersion = 0;
185    } else {
186       g_platform_id = osversioninfo.dwPlatformId;
187       dwMinorVersion = osversioninfo.dwMinorVersion;
188    }
189
190    /* deinitialize some routines on win95 - they're not implemented well */
191    if (g_platform_id == VER_PLATFORM_WIN32_WINDOWS) {
192       p_BackupRead = NULL;
193       p_BackupWrite = NULL;
194
195       p_CreateFileW = NULL;          
196       p_GetFileAttributesW = NULL;          
197       p_GetFileAttributesExW = NULL;
198           
199       p_SetFileAttributesW = NULL;
200                 
201       p_FindFirstFileW = NULL;
202       p_FindNextFileW = NULL;
203       p_SetCurrentDirectoryW = NULL;
204       p_GetCurrentDirectoryW = NULL;
205
206       p_wunlink = NULL;
207       p_wmkdir = NULL;
208       p_wopen = NULL;
209    }   
210
211    /* decide which vss class to initialize */
212 #ifdef WIN32_VSS
213    switch (dwMinorVersion) {
214       case 1: 
215          g_pVSSClient = new VSSClientXP();
216          atexit(VSSCleanup);
217          break;
218       case 2: 
219          g_pVSSClient = new VSSClient2003();
220          atexit(VSSCleanup);
221          break;
222    }
223 #endif /* WIN32_VSS */
224 }
225
226 #endif