xref: /netbsd-src/external/gpl3/gcc/dist/libphobos/src/std/internal/windows/advapi32.d (revision b1e838363e3c6fc78a55519254d99869742dd33c)
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.winbase, core.sys.windows.winnt, core.sys.windows.winreg;
16 
17 pragma(lib, "advapi32.lib");
18 
isWow64()19 @property bool isWow64()
20 {
21     // WOW64 is the x86 emulator that allows 32-bit Windows-based applications to run seamlessly on 64-bit Windows
22     // IsWow64Process Function - Minimum supported client - Windows Vista, Windows XP with SP2
23     static int result = -1; // <0 if uninitialized, >0 if yes, ==0 if no
24     if (result >= 0)
25         return result > 0;  // short path
26     // Will do this work once per thread to avoid importing std.concurrency
27     // or doing gnarly initonce work.
28     alias fptr_t = extern(Windows) BOOL function(HANDLE, PBOOL);
29     auto hKernel = GetModuleHandleA("kernel32");
30     auto IsWow64Process = cast(fptr_t) GetProcAddress(hKernel, "IsWow64Process");
31     BOOL bIsWow64;
32     result = IsWow64Process && IsWow64Process(GetCurrentProcess(), &bIsWow64) && bIsWow64;
33     return result > 0;
34 }
35 
36 HMODULE hAdvapi32 = null;
37 extern (Windows)
38 {
39     LONG function(in HKEY hkey, in LPCWSTR lpSubKey, in REGSAM samDesired, in DWORD reserved) pRegDeleteKeyExW;
40 }
41 
loadAdvapi32()42 void loadAdvapi32()
43 {
44     if (!hAdvapi32)
45     {
46         hAdvapi32 = LoadLibraryA("Advapi32.dll");
47         if (!hAdvapi32)
48             throw new Exception(`LoadLibraryA("Advapi32.dll")`);
49 
50         pRegDeleteKeyExW = cast(typeof(pRegDeleteKeyExW)) GetProcAddress(hAdvapi32 , "RegDeleteKeyExW");
51         if (!pRegDeleteKeyExW)
52             throw new Exception(`GetProcAddress(hAdvapi32 , "RegDeleteKeyExW")`);
53     }
54 }
55 
56 // It will free Advapi32.dll, which may be loaded for RegDeleteKeyEx function
freeAdvapi32()57 private void freeAdvapi32()
58 {
59     if (hAdvapi32)
60     {
61         if (!FreeLibrary(hAdvapi32))
62             throw new Exception(`FreeLibrary("Advapi32.dll")`);
63         hAdvapi32 = null;
64 
65         pRegDeleteKeyExW = null;
66     }
67 }
68 
~this()69 static ~this()
70 {
71     freeAdvapi32();
72 }
73