Process Hacker
apiimport.c
Go to the documentation of this file.
1 /*
2  * Process Hacker -
3  * procedure import module
4  *
5  * Copyright (C) 2015 wj32
6  *
7  * This file is part of Process Hacker.
8  *
9  * Process Hacker is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation, either version 3 of the License, or
12  * (at your option) any later version.
13  *
14  * Process Hacker 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
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with Process Hacker. If not, see <http://www.gnu.org/licenses/>.
21  */
22 
23 #include <ph.h>
24 #include <apiimport.h>
25 
27  _Inout_ PVOID *Cache,
28  _Inout_ PBOOLEAN CacheValid,
29  _In_ PWSTR ModuleName,
30  _In_ PSTR ProcedureName
31  )
32 {
33  HMODULE module;
34  PVOID procedure;
35 
36  if (*CacheValid)
37  return *Cache;
38 
39  module = GetModuleHandle(ModuleName);
40 
41  if (!module)
42  return NULL;
43 
44  procedure = GetProcAddress(module, ProcedureName);
45  *Cache = procedure;
46  MemoryBarrier();
47  *CacheValid = TRUE;
48 
49  return procedure;
50 }
51 
52 #define PH_DEFINE_IMPORT(Module, Name) \
53 _##Name Name##_Import(VOID) \
54 { \
55  static PVOID cache = NULL; \
56  static BOOLEAN cacheValid = FALSE; \
57 \
58  return (_##Name)PhpImportProcedure(&cache, &cacheValid, Module, #Name); \
59 }
60