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 
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(
40         scope const HKEY hkey, scope const LPCWSTR lpSubKey,
41         scope const REGSAM samDesired, scope const DWORD reserved) pRegDeleteKeyExW;
42 }
43 
44 void loadAdvapi32()
45 {
46     if (!hAdvapi32)
47     {
48         hAdvapi32 = LoadLibraryA("Advapi32.dll");
49         if (!hAdvapi32)
50             throw new Exception(`LoadLibraryA("Advapi32.dll")`);
51 
52         pRegDeleteKeyExW = cast(typeof(pRegDeleteKeyExW)) GetProcAddress(hAdvapi32 , "RegDeleteKeyExW");
53         if (!pRegDeleteKeyExW)
54             throw new Exception(`GetProcAddress(hAdvapi32 , "RegDeleteKeyExW")`);
55     }
56 }
57 
58 // It will free Advapi32.dll, which may be loaded for RegDeleteKeyEx function
59 private void freeAdvapi32()
60 {
61     if (hAdvapi32)
62     {
63         if (!FreeLibrary(hAdvapi32))
64             throw new Exception(`FreeLibrary("Advapi32.dll")`);
65         hAdvapi32 = null;
66 
67         pRegDeleteKeyExW = null;
68     }
69 }
70 
71 static ~this()
72 {
73     freeAdvapi32();
74 }