Debian Codegraph @

mingw-w64-tools/genidl/src/fsredir.c

raw
1 /*
2 genidl - Generate interface defintion language listing from a
3 Portable Executable.
4 Copyright (C) 2009-2016 mingw-w64 project
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "fsredir.h"
20
21 #ifdef REDIRECTOR
22 #define WIN32_LEAN_AND_MEAN
23 #include <windows.h>
24 #include <stdlib.h>
25 #include <stdio.h>
26
27 static PVOID revert; /*revert pointer*/
28 static HMODULE kernel32handle;
29 typedef WINBOOL (__stdcall (*redirector))(PVOID *);
30 typedef WINBOOL (__stdcall (*revertor))(PVOID);
31 static redirector redirectorfunction; /*Wow64DisableWow64FsRedirection*/
32 static revertor revertorfunction; /*Wow64RevertWow64FsRedirection*/
33
34 static void undoredirect(void) {
35 revertorfunction(revert);
36 }
37
38 void doredirect(const int redir) {
39 if (redir) {
40 kernel32handle = GetModuleHandleW(L"kernel32.dll");
41 if (!kernel32handle) {
42 fprintf(stderr, "kernel32.dll failed to load, failed to disable FS redirection.\n");
43 return;
44 }
45 redirectorfunction = (redirector)GetProcAddress(kernel32handle, "Wow64DisableWow64FsRedirection");
46 revertorfunction = (revertor)GetProcAddress(kernel32handle, "Wow64RevertWow64FsRedirection");
47 if (!redirectorfunction || ! revertorfunction) {
48 FreeLibrary(kernel32handle);
49 fprintf(stderr, "Wow64DisableWow64FsRedirection or Wow64RevertWow64FsRedirection functions missing.\n");
50 return;
51 }
52 if (!redirectorfunction(&revert)) {
53 fprintf(stderr, "Wow64DisableWow64FsRedirection failed.\n");
54 return;
55 } else {
56 atexit(undoredirect);
57 }
58 }
59 }
60 #endif