1 /**
2  * Miscellaneous Windows utility code.
3  *
4  * License:
5  *   This Source Code Form is subject to the terms of
6  *   the Mozilla Public License, v. 2.0. If a copy of
7  *   the MPL was not distributed with this file, You
8  *   can obtain one at http://mozilla.org/MPL/2.0/.
9  *
10  * Authors:
11  *   Vladimir Panteleev <vladimir@thecybershadow.net>
12  */
13 
14 module ae.sys.windows.misc;
15 version (Windows):
16 
17 import std..string;
18 
19 import ae.sys.windows.imports;
20 mixin(importWin32!q{winbase});
21 mixin(importWin32!q{wincon});
22 mixin(importWin32!q{winnt});
23 mixin(importWin32!q{winuser});
24 
25 import ae.sys.windows.exception;
26 import ae.sys.windows.text;
27 
28 LARGE_INTEGER largeInteger(long n) pure nothrow @nogc
29 {
30 	LARGE_INTEGER li; li.QuadPart = n; return li;
31 }
32 
33 ULARGE_INTEGER ulargeInteger(ulong n) pure nothrow @nogc
34 {
35 	ULARGE_INTEGER li; li.QuadPart = n; return li;
36 }
37 
38 ulong makeUlong(DWORD dwLow, DWORD dwHigh) pure nothrow @nogc
39 {
40 	ULARGE_INTEGER li;
41 	li.LowPart  = dwLow;
42 	li.HighPart = dwHigh;
43 	return li.QuadPart;
44 }
45 
46 // ***************************************************************************
47 
48 // Messages
49 
50 void processWindowsMessages()()
51 {
52 	MSG m;
53 	while (PeekMessageW(&m, null, 0, 0, PM_REMOVE))
54 	{
55 		TranslateMessage(&m);
56 		DispatchMessageW(&m);
57 	}
58 }
59 
60 void messageLoop()()
61 {
62 	MSG m;
63 	while (GetMessageW(&m, null, 0, 0))
64 	{
65 		TranslateMessage(&m);
66 		DispatchMessageW(&m);
67 	}
68 }
69 
70 // ***************************************************************************
71 
72 int messageBox()(string message, string title, int style=0)
73 {
74 	return MessageBoxW(null, toWStringz(message), toWStringz(title), style);
75 }
76 
77 uint getLastInputInfo()()
78 {
79 	LASTINPUTINFO lii = { LASTINPUTINFO.sizeof };
80 	wenforce(GetLastInputInfo(&lii), "GetLastInputInfo");
81 	return lii.dwTime;
82 }
83 
84 // ***************************************************************************
85 
86 /// Hides the console window, but only if we are the owner.
87 void hideOwnConsoleWindow()()
88 {
89 	HWND w = GetConsoleWindow();
90 	if (!w)
91 		return;
92 	DWORD pid;
93 	GetWindowThreadProcessId(w, &pid);
94 	if (pid == GetCurrentProcessId())
95 		ShowWindow(w, SW_HIDE);
96 }
97 
98 // ***************************************************************************
99 
100 /// Returns true if the host Windows OS is 64-bit.
101 bool isWin64()
102 {
103 	version (D_LP64)
104 		return true; // host must be 64-bit if this compiles
105 	else
106 	{
107 		import core.sys.windows.winbase : IsWow64Process, GetCurrentProcess;
108 		int is64;
109 		IsWow64Process(GetCurrentProcess(), &is64).wenforce("IsWow64Process");
110 		return !!is64;
111 	}
112 }
113 
114 /// Returns Wine version, or null if not running under Wine.
115 string getWineVersion()
116 {
117 	auto ntdll = GetModuleHandle("ntdll.dll");
118 	if (!ntdll)
119 		return null;
120 	alias wine_get_version_t = extern(C) const(char*) function();
121 	auto wine_get_version = cast(wine_get_version_t)GetProcAddress(ntdll, "wine_get_version");
122 	if (!wine_get_version)
123 		return null;
124 	import std.conv : to;
125 	return wine_get_version().to!string();
126 }