xref: /netbsd-src/external/gpl3/gcc.old/dist/libphobos/src/std/internal/windows/advapi32.d (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
1 // Written in the D programming language.
2 
3 /**
4  * The only purpose of this module is to do the static construction for
5  * std.windows.registry, to eliminate cyclic construction errors.
6  *
7  * License:   $(HTTP www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
8  * Authors:   Kenji Hara
9  * Source:    $(PHOBOSSRC std/internal/windows/_advapi32.d)
10  */
11 module std.internal.windows.advapi32;
12 
13 version (Windows):
14 
15 import core.sys.windows.windows;
16 
17 pragma(lib, "advapi32.lib");
18 
19 immutable bool isWow64;
20 
this()21 shared static this()
22 {
23     // WOW64 is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows
24     // IsWow64Process Function - Minimum supported client - Windows Vista, Windows XP with SP2
25     alias fptr_t = extern(Windows) BOOL function(HANDLE, PBOOL);
26     auto hKernel = GetModuleHandleA("kernel32");
27     auto IsWow64Process = cast(fptr_t) GetProcAddress(hKernel, "IsWow64Process");
28     BOOL bIsWow64;
29     isWow64 = IsWow64Process && IsWow64Process(GetCurrentProcess(), &bIsWow64) && bIsWow64;
30 }
31 
32 HMODULE hAdvapi32 = null;
33 extern (Windows)
34 {
35     LONG function(in HKEY hkey, in LPCWSTR lpSubKey, in REGSAM samDesired, in DWORD reserved) pRegDeleteKeyExW;
36 }
37 
loadAdvapi32()38 void loadAdvapi32()
39 {
40     if (!hAdvapi32)
41     {
42         hAdvapi32 = LoadLibraryA("Advapi32.dll");
43         if (!hAdvapi32)
44             throw new Exception(`LoadLibraryA("Advapi32.dll")`);
45 
46         pRegDeleteKeyExW = cast(typeof(pRegDeleteKeyExW)) GetProcAddress(hAdvapi32 , "RegDeleteKeyExW");
47         if (!pRegDeleteKeyExW)
48             throw new Exception(`GetProcAddress(hAdvapi32 , "RegDeleteKeyExW")`);
49     }
50 }
51 
52 // It will free Advapi32.dll, which may be loaded for RegDeleteKeyEx function
freeAdvapi32()53 private void freeAdvapi32()
54 {
55     if (hAdvapi32)
56     {
57         if (!FreeLibrary(hAdvapi32))
58             throw new Exception(`FreeLibrary("Advapi32.dll")`);
59         hAdvapi32 = null;
60 
61         pRegDeleteKeyExW = null;
62     }
63 }
64 
~this()65 static ~this()
66 {
67     freeAdvapi32();
68 }
69