]> git.sur5r.net Git - bacula/bacula/blob - bacula/src/win32/compat/winapi.c
ebl typo fix
[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 Kern Sibbald.
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 t_CreateProcessA        p_CreateProcessA = NULL;
92 t_CreateProcessW        p_CreateProcessW = NULL;
93
94 void 
95 InitWinAPIWrapper() 
96 {
97    OSVERSIONINFO osversioninfo = { sizeof(OSVERSIONINFO) };
98
99    // Get the current OS version
100    if (!GetVersionEx(&osversioninfo)) {
101       g_platform_id = 0;
102    } else {
103       g_platform_id = osversioninfo.dwPlatformId;
104       g_MinorVersion = osversioninfo.dwMinorVersion;
105       g_MajorVersion = osversioninfo.dwMajorVersion;
106    }
107
108    HMODULE hLib = LoadLibraryA("KERNEL32.DLL");
109    if (hLib) {
110       /* create process calls */
111       p_CreateProcessA = (t_CreateProcessA)
112          GetProcAddress(hLib, "CreateProcessA");
113       p_CreateProcessW = (t_CreateProcessW)
114          GetProcAddress(hLib, "CreateProcessW");
115
116       /* create file calls */
117       p_CreateFileA = (t_CreateFileA)GetProcAddress(hLib, "CreateFileA");
118       p_CreateDirectoryA = (t_CreateDirectoryA)GetProcAddress(hLib, "CreateDirectoryA");
119
120       /* attribute calls */
121       p_GetFileAttributesA = (t_GetFileAttributesA)GetProcAddress(hLib, "GetFileAttributesA");
122       p_GetFileAttributesExA = (t_GetFileAttributesExA)GetProcAddress(hLib, "GetFileAttributesExA");
123       p_SetFileAttributesA = (t_SetFileAttributesA)GetProcAddress(hLib, "SetFileAttributesA");
124
125       /* process calls */
126       p_SetProcessShutdownParameters = (t_SetProcessShutdownParameters)
127           GetProcAddress(hLib, "SetProcessShutdownParameters");
128
129       /* char conversion calls */
130       p_WideCharToMultiByte = (t_WideCharToMultiByte)
131           GetProcAddress(hLib, "WideCharToMultiByte");
132       p_MultiByteToWideChar = (t_MultiByteToWideChar)
133           GetProcAddress(hLib, "MultiByteToWideChar");
134
135       /* find files */
136       p_FindFirstFileA = (t_FindFirstFileA)GetProcAddress(hLib, "FindFirstFileA"); 
137       p_FindNextFileA = (t_FindNextFileA)GetProcAddress(hLib, "FindNextFileA");
138
139       /* get and set directory */
140       p_GetCurrentDirectoryA = (t_GetCurrentDirectoryA)
141           GetProcAddress(hLib, "GetCurrentDirectoryA");
142       p_SetCurrentDirectoryA = (t_SetCurrentDirectoryA)
143           GetProcAddress(hLib, "SetCurrentDirectoryA");
144
145       if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
146          p_CreateFileW = (t_CreateFileW)
147              GetProcAddress(hLib, "CreateFileW");
148          p_CreateDirectoryW = (t_CreateDirectoryW)
149              GetProcAddress(hLib, "CreateDirectoryW");
150
151          /* backup calls */
152          p_BackupRead = (t_BackupRead)GetProcAddress(hLib, "BackupRead");
153          p_BackupWrite = (t_BackupWrite)GetProcAddress(hLib, "BackupWrite");
154
155          p_GetFileAttributesW = (t_GetFileAttributesW)
156              GetProcAddress(hLib, "GetFileAttributesW");
157          p_GetFileAttributesExW = (t_GetFileAttributesExW)
158              GetProcAddress(hLib, "GetFileAttributesExW");
159          p_SetFileAttributesW = (t_SetFileAttributesW)
160              GetProcAddress(hLib, "SetFileAttributesW");
161          p_FindFirstFileW = (t_FindFirstFileW)
162              GetProcAddress(hLib, "FindFirstFileW");
163          p_FindNextFileW = (t_FindNextFileW)
164              GetProcAddress(hLib, "FindNextFileW");
165          p_GetCurrentDirectoryW = (t_GetCurrentDirectoryW)
166              GetProcAddress(hLib, "GetCurrentDirectoryW");
167          p_SetCurrentDirectoryW = (t_SetCurrentDirectoryW)
168              GetProcAddress(hLib, "SetCurrentDirectoryW");
169
170          /* some special stuff we need for VSS
171             but static linkage doesn't work on Win 9x */
172          p_GetVolumePathNameW = (t_GetVolumePathNameW)
173              GetProcAddress(hLib, "GetVolumePathNameW");
174          p_GetVolumeNameForVolumeMountPointW = (t_GetVolumeNameForVolumeMountPointW)
175              GetProcAddress(hLib, "GetVolumeNameForVolumeMountPointW");
176
177          p_AttachConsole = (t_AttachConsole)
178              GetProcAddress(hLib, "AttachConsole");
179       }
180    }
181    
182    if (g_platform_id != VER_PLATFORM_WIN32_WINDOWS) {
183       hLib = LoadLibraryA("MSVCRT.DLL");
184       if (hLib) {
185          /* unlink */
186          p_wunlink = (t_wunlink)
187          GetProcAddress(hLib, "_wunlink");
188          /* wmkdir */
189          p_wmkdir = (t_wmkdir)
190          GetProcAddress(hLib, "_wmkdir");
191       }
192
193       hLib = LoadLibraryA("ADVAPI32.DLL");
194       if (hLib) {
195          p_OpenProcessToken = (t_OpenProcessToken)
196             GetProcAddress(hLib, "OpenProcessToken");
197          p_AdjustTokenPrivileges = (t_AdjustTokenPrivileges)
198             GetProcAddress(hLib, "AdjustTokenPrivileges");
199          p_LookupPrivilegeValue = (t_LookupPrivilegeValue)
200             GetProcAddress(hLib, "LookupPrivilegeValueA");
201       }
202    }
203
204    hLib = LoadLibraryA("SHELL32.DLL");
205    if (hLib) {
206       p_SHGetFolderPath = (t_SHGetFolderPath)
207          GetProcAddress(hLib, "SHGetFolderPathA");
208    } else {
209       /* If SHELL32 isn't found try SHFOLDER for older systems */
210       hLib = LoadLibraryA("SHFOLDER.DLL");
211       if (hLib) {
212          p_SHGetFolderPath = (t_SHGetFolderPath)
213             GetProcAddress(hLib, "SHGetFolderPathA");
214       }
215    }
216    atexit(Win32ConvCleanupCache);
217 }