1 #include "Core.h"
2 
3 namespace Upp {
4 
5 #ifdef PLATFORM_WIN32
6 
7 static HINSTANCE app_instance;
8 
AppGetHandle()9 HINSTANCE AppGetHandle()
10 {
11 	if(!app_instance)
12 		app_instance = GetModuleHandle(NULL);
13 	return app_instance;
14 }
15 
AppSetHandle(HINSTANCE dll_instance)16 void AppSetHandle(HINSTANCE dll_instance) { app_instance = dll_instance; }
17 
18 #ifndef PLATFORM_WINCE
IsWin2K()19 bool IsWin2K()
20 {
21 	OSVERSIONINFO of;
22 	of.dwOSVersionInfoSize = sizeof(of);
23 	GetVersionEx(&of);
24 	return of.dwMajorVersion >= 5;
25 }
26 
IsWinXP()27 bool IsWinXP()
28 {
29 	OSVERSIONINFO of;
30 	of.dwOSVersionInfoSize = sizeof(of);
31 	GetVersionEx(&of);
32 	return of.dwMajorVersion > 5 || of.dwMajorVersion == 5 && of.dwMinorVersion >= 1;
33 }
34 
IsWinVista()35 bool IsWinVista()
36 {
37 	OSVERSIONINFO of;
38 	of.dwOSVersionInfoSize = sizeof(of);
39 	GetVersionEx(&of);
40 	return of.dwMajorVersion >= 6;
41 }
42 
IsWin7()43 bool IsWin7()
44 {
45 	OSVERSIONINFO of;
46 	of.dwOSVersionInfoSize = sizeof(of);
47 	GetVersionEx(&of);
48 	return of.dwMajorVersion >= 6 && of.dwMinorVersion >= 1;
49 }
50 #endif
51 
AsString(const wchar_t * buffer)52 String AsString(const wchar_t *buffer) {
53 	if(!buffer)
54 		return Null;
55 	return AsString(buffer, (int)wcslen(buffer));
56 }
57 
AsString(const wchar_t * buffer,int count)58 String AsString(const wchar_t *buffer, int count) { // Convert with code page...
59 	if(!buffer)
60 		return Null;
61 	StringBuffer temp(count);
62 	for(char *p = temp, *e = p + count; p < e;)
63 		*p++ = (char)*buffer++;
64 	return String(temp);
65 }
66 
AsString(const wchar_t * buffer,const wchar_t * end)67 String AsString(const wchar_t *buffer, const wchar_t *end) {
68 	if(!buffer)
69 		return Null;
70 	return AsString(buffer, (int)(end - buffer));
71 }
72 
GetWinRegString(const char * value,const char * path,HKEY base_key,dword wow)73 String GetWinRegString(const char *value, const char *path, HKEY base_key, dword wow) {
74 	HKEY key = 0;
75 	if(RegOpenKeyEx(base_key, path, 0, KEY_READ|wow, &key) != ERROR_SUCCESS)
76 		return String::GetVoid();
77 	dword type, len;
78 	if(RegQueryValueEx(key, value, 0, &type, NULL, &len) != ERROR_SUCCESS)
79 	{
80 		RegCloseKey(key);
81 		return String::GetVoid();
82 	}
83 	StringBuffer raw_len(len);
84 	if(RegQueryValueEx(key, value, 0, 0, (byte *)~raw_len, &len) != ERROR_SUCCESS)
85 	{
86 		RegCloseKey(key);
87 		return String::GetVoid();
88 	}
89 	if(len > 0 && (type == REG_SZ || type == REG_EXPAND_SZ))
90 		len--;
91 	raw_len.SetLength(len);
92 	RegCloseKey(key);
93 	return String(raw_len);
94 }
95 
GetWinRegInt(const char * value,const char * path,HKEY base_key,dword wow)96 int GetWinRegInt(const char *value, const char *path, HKEY base_key, dword wow) {
97 	HKEY key = 0;
98 	if(RegOpenKeyEx(base_key, path, 0, KEY_READ|wow, &key) != ERROR_SUCCESS)
99 		return Null;
100 	int data;
101 	dword type, length = sizeof(data);
102 	if(RegQueryValueEx(key, value, 0, &type, (byte *)&data, &length) != ERROR_SUCCESS)
103 		data = Null;
104 	RegCloseKey(key);
105 	return data;
106 }
107 
SetWinRegString(const String & string,const char * value,const char * path,HKEY base_key,dword wow)108 bool SetWinRegString(const String& string, const char *value, const char *path, HKEY base_key, dword wow) {
109 	HKEY key = 0;
110 	if(RegCreateKeyEx(base_key, path, 0, NULL, REG_OPTION_NON_VOLATILE,
111 		KEY_ALL_ACCESS|wow, NULL, &key, NULL) != ERROR_SUCCESS)
112 		return false;
113 	bool ok = (RegSetValueEx(key, value, 0,	REG_SZ, string, string.GetLength() + 1) == ERROR_SUCCESS);
114 	RegCloseKey(key);
115 	return ok;
116 }
117 
SetWinRegExpandString(const String & string,const char * value,const char * path,HKEY base_key,dword wow)118 bool SetWinRegExpandString(const String& string, const char *value, const char *path, HKEY base_key, dword wow) {
119 	HKEY key = 0;
120 	if(RegCreateKeyEx(base_key, path, 0, NULL, REG_OPTION_NON_VOLATILE,
121 		KEY_ALL_ACCESS|wow, NULL, &key, NULL) != ERROR_SUCCESS)
122 		return false;
123 	bool ok = (RegSetValueEx(key, value, 0,	REG_EXPAND_SZ, string, string.GetLength() + 1) == ERROR_SUCCESS);
124 	RegCloseKey(key);
125 	return ok;
126 }
127 
SetWinRegInt(int data,const char * value,const char * path,HKEY base_key,dword wow)128 bool SetWinRegInt(int data, const char *value, const char *path, HKEY base_key, dword wow)
129 {
130 	HKEY key = 0;
131 	if(RegCreateKeyEx(base_key, path, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS|wow, NULL, &key, NULL) != ERROR_SUCCESS)
132 		return false;
133 	bool ok = (RegSetValueEx(key, value, 0, REG_DWORD, (const byte *)&data, sizeof(data)) == ERROR_SUCCESS);
134 	RegCloseKey(key);
135 	return ok;
136 }
137 
DeleteWinReg(const String & key,HKEY base,dword wow)138 void DeleteWinReg(const String& key, HKEY base, dword wow) {
139 	HKEY hkey;
140 	if(RegOpenKeyEx(base, key, 0, KEY_READ|wow, &hkey) != ERROR_SUCCESS)
141 		return;
142 	Vector<String> subkeys;
143 	char temp[_MAX_PATH];
144 	dword len;
145 	for(dword dw = 0; len = sizeof(temp), RegEnumKeyEx(hkey, dw, temp, &len, 0, 0, 0, 0) == ERROR_SUCCESS; dw++)
146 		subkeys.Add(temp);
147 	RegCloseKey(hkey);
148 	while(!subkeys.IsEmpty())
149 		DeleteWinReg(key + '\\' + subkeys.Pop(), base);
150 
151 	static LONG (WINAPI *RegDeleteKeyEx)(HKEY, LPCTSTR, REGSAM, DWORD);
152 	DllFn(RegDeleteKeyEx, "Advapi32.dll", "RegDeleteKeyExA");
153 
154 	if(wow && RegDeleteKeyEx)
155 		RegDeleteKeyEx(base, key, wow, 0);
156 	else
157 		RegDeleteKey(base, key);
158 }
159 
GetSystemDirectory()160 String GetSystemDirectory() {
161 	char temp[MAX_PATH];
162 	*temp = 0;
163 	::GetSystemDirectory(temp, sizeof(temp));
164 	return FromSystemCharset(temp);
165 }
166 
GetWindowsDirectory()167 String GetWindowsDirectory() {
168 	wchar temp[MAX_PATH];
169 	*temp = 0;
170 	GetWindowsDirectoryW(temp, sizeof(temp));
171 	return FromSystemCharsetW(temp);
172 }
173 
GetDllFn(const char * dll,const char * fn)174 void *GetDllFn(const char *dll, const char *fn)
175 {
176 	if(HMODULE hDLL = LoadLibrary(dll))
177 		return (void *)GetProcAddress(hDLL, fn);
178 	return NULL;
179 }
180 
GetModuleFileName(HINSTANCE instance)181 String GetModuleFileName(HINSTANCE instance) {
182 	wchar h[_MAX_PATH];
183 	GetModuleFileNameW(instance, h, _MAX_PATH);
184 	return FromSystemCharsetW(h);
185 }
186 
187 #ifdef DEPRECATED
Wait(int ms)188 bool SyncObject::Wait(int ms)
189 {
190 	return WaitForSingleObject(handle, ms);
191 }
192 
Wait()193 bool SyncObject::Wait()
194 {
195 	return Wait(INFINITE);
196 }
197 
SyncObject()198 SyncObject::SyncObject()
199 {
200 	handle = NULL;
201 }
202 
~SyncObject()203 SyncObject::~SyncObject()
204 {
205 	if(handle) CloseHandle(handle);
206 }
207 
Win32Event()208 Win32Event::Win32Event()
209 {
210 	handle = CreateEvent(NULL, FALSE, FALSE, NULL);
211 }
212 
Set()213 void Win32Event::Set()
214 {
215 	SetEvent(handle);
216 }
217 #endif
218 
219 #endif
220 
221 }
222