summaryrefslogtreecommitdiff
path: root/plugins/CrashDumper/src/utils.cpp
blob: f7d1e5d8a10413f364b7e33095498376af2d9148 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
/*
Miranda Crash Dumper Plugin
Copyright (C) 2008 - 2012 Boris Krasnovskiy All Rights Reserved

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation version 2
of the License.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "utils.h"

static HMODULE hKernel = GetModuleHandle(TEXT("kernel32.dll")); 

tGetNativeSystemInfo pGetNativeSystemInfo = (tGetNativeSystemInfo)GetProcAddress(hKernel, "GetNativeSystemInfo");
tGetProductInfo pGetProductInfo = (tGetProductInfo) GetProcAddress(hKernel, "GetProductInfo");
tGlobalMemoryStatusEx pGlobalMemoryStatusEx = (tGlobalMemoryStatusEx) GetProcAddress(hKernel, "GlobalMemoryStatusEx");
tGetUserDefaultUILanguage pGetUserDefaultUILanguage = (tGetUserDefaultUILanguage) GetProcAddress(hKernel, "GetUserDefaultUILanguage");
tGetSystemDefaultUILanguage pGetSystemDefaultUILanguage = (tGetSystemDefaultUILanguage) GetProcAddress(hKernel, "GetSystemDefaultUILanguage");
tIsWow64Process pIsWow64Process = (tIsWow64Process) GetProcAddress(hKernel, "IsWow64Process");
tIsProcessorFeaturePresent pIsProcessorFeaturePresent = (tIsProcessorFeaturePresent) GetProcAddress(hKernel, "IsProcessorFeaturePresent");


tGetDiskFreeSpaceEx pGetDiskFreeSpaceEx = (tGetDiskFreeSpaceEx) GetProcAddress(hKernel, "GetDiskFreeSpaceExW");



void CheckForOtherCrashReportingPlugins(void)
{
	HMODULE hModule = GetModuleHandle(TEXT("attache.dll"));
	if (hModule == NULL)
		hModule = GetModuleHandle(TEXT("crashrpt.dll"));
	if (hModule == NULL)
		hModule = GetModuleHandle(TEXT("crashdmp.dll"));

	if (hModule == NULL) return;

	MessageBox(NULL, TranslateT("More then one crash reporting plugin installed. This will result in inability of creating crash reports"),
		TEXT("Miranda Crash Dumper"), MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_TOPMOST);
}

void GetOSDisplayString(bkstring& buffer)
{
	OSVERSIONINFOEX osvi = {0};
	SYSTEM_INFO si = {0};
	BOOL bOsVersionInfoEx;
	DWORD dwType = 0;

	osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);

	bOsVersionInfoEx = GetVersionEx((OSVERSIONINFO*)&osvi);
	if (!bOsVersionInfoEx)
	{
		osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
		if (!GetVersionEx((OSVERSIONINFO*)&osvi)) 
			return;
	}

	// Call GetNativeSystemInfo if supported or GetSystemInfo otherwise.
	pGetNativeSystemInfo = (tGetNativeSystemInfo)GetProcAddress(hKernel, "GetNativeSystemInfo");
	if (NULL != pGetNativeSystemInfo) pGetNativeSystemInfo(&si);
	else GetSystemInfo(&si);

	if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId && osvi.dwMajorVersion > 4)
	{
		buffer.append(TEXT("Operating System: Microsoft "));

		// Test for the specific product.
		if (osvi.dwMajorVersion == 6)
		{
			switch (osvi.dwMinorVersion)
			{
			case 0:
				if (osvi.wProductType == VER_NT_WORKSTATION)
					buffer.append(TEXT("Windows Vista "));
				else 
					buffer.append(TEXT("Windows Server 2008 "));
				break;

			case 1:
				if (osvi.wProductType == VER_NT_WORKSTATION)
					buffer.append(TEXT("Windows 7 "));
				else 
					buffer.append(TEXT("Windows Server 2008 R2 "));
				break;

			default:
				if (osvi.wProductType == VER_NT_WORKSTATION)
					buffer.append(TEXT("Windows 8 "));
				else 
					buffer.append(TEXT("Windows Server 2012 "));
				break;
			}

			pGetProductInfo = (tGetProductInfo) GetProcAddress(hKernel, "GetProductInfo");
			if (pGetProductInfo != NULL) pGetProductInfo(6, 0, 0, 0, &dwType);

			switch(dwType)
			{
			case PRODUCT_ULTIMATE:
				buffer.append(TEXT("Ultimate Edition"));
				break;
			case PRODUCT_HOME_PREMIUM:
				buffer.append(TEXT("Home Premium Edition"));
				break;
			case PRODUCT_HOME_BASIC:
				buffer.append(TEXT("Home Basic Edition"));
				break;
			case PRODUCT_ENTERPRISE:
				buffer.append(TEXT("Enterprise Edition"));
				break;
			case PRODUCT_BUSINESS:
				buffer.append(TEXT("Business Edition"));
				break;
			case PRODUCT_STARTER:
				buffer.append(TEXT("Starter Edition"));
				break;
			case PRODUCT_CLUSTER_SERVER:
				buffer.append(TEXT("Cluster Server Edition"));
				break;
			case PRODUCT_DATACENTER_SERVER:
				buffer.append(TEXT("Datacenter Edition"));
				break;
			case PRODUCT_DATACENTER_SERVER_CORE:
				buffer.append(TEXT("Datacenter Edition (core installation)"));
				break;
			case PRODUCT_ENTERPRISE_SERVER:
				buffer.append(TEXT("Enterprise Edition"));
				break;
			case PRODUCT_ENTERPRISE_SERVER_CORE:
				buffer.append(TEXT("Enterprise Edition (core installation)"));
				break;
			case PRODUCT_ENTERPRISE_SERVER_IA64:
				buffer.append(TEXT("Enterprise Edition for Itanium-based Systems"));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER:
				buffer.append(TEXT("Small Business Server"));
				break;
			case PRODUCT_SMALLBUSINESS_SERVER_PREMIUM:
				buffer.append(TEXT("Small Business Server Premium Edition"));
				break;
			case PRODUCT_STANDARD_SERVER:
				buffer.append(TEXT("Standard Edition"));
				break;
			case PRODUCT_STANDARD_SERVER_CORE:
				buffer.append(TEXT("Standard Edition (core installation)"));
				break;
			case PRODUCT_WEB_SERVER:
				buffer.append(TEXT("Web Server Edition"));
				break;
			}

			if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
				buffer.append(TEXT(", 64-bit"));
			else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_INTEL)
				buffer.append(TEXT(", 32-bit"));
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 2)
		{
			if (GetSystemMetrics(SM_SERVERR2))
				buffer.append(TEXT("Windows Server 2003 R2, "));
			else if (osvi.wSuiteMask==VER_SUITE_STORAGE_SERVER)
				buffer.append(TEXT("Windows Storage Server 2003"));
			else if (osvi.wProductType == VER_NT_WORKSTATION && 
				si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64)
				buffer.append(TEXT("Windows XP Professional x64 Edition"));
			else buffer.append(TEXT("Windows Server 2003, "));

			// Test for the server type.
			if (osvi.wProductType != VER_NT_WORKSTATION)
			{
				if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_IA64)
				{
					if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
						buffer.append(TEXT("Datacenter Edition for Itanium-based Systems"));
					else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						buffer.append(TEXT("Enterprise Edition for Itanium-based Systems"));
				}

				else if (si.wProcessorArchitecture==PROCESSOR_ARCHITECTURE_AMD64)
				{
					if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
						buffer.append(TEXT("Datacenter x64 Edition"));
					else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						buffer.append(TEXT("Enterprise x64 Edition"));
					else buffer.append(TEXT("Standard x64 Edition"));
				}

				else
				{
					if (osvi.wSuiteMask & VER_SUITE_COMPUTE_SERVER)
						buffer.append(TEXT("Compute Cluster Edition"));
					else if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
						buffer.append(TEXT("Datacenter Edition"));
					else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
						buffer.append(TEXT("Enterprise Edition"));
					else if (osvi.wSuiteMask & VER_SUITE_BLADE)
						buffer.append(TEXT("Web Edition"));
					else buffer.append(TEXT("Standard Edition"));
				}
			}
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 1)
		{
			buffer.append(TEXT("Windows XP "));
			if (osvi.wSuiteMask & VER_SUITE_PERSONAL)
				buffer.append(TEXT("Home Edition"));
			else 
				buffer.append(TEXT("Professional"));
		}

		if (osvi.dwMajorVersion == 5 && osvi.dwMinorVersion == 0)
		{
			buffer.append(TEXT("Windows 2000 "));

			if (osvi.wProductType == VER_NT_WORKSTATION)
			{
				buffer.append(TEXT("Professional"));
			}
			else 
			{
				if(osvi.wSuiteMask & VER_SUITE_DATACENTER)
					buffer.append(TEXT("Datacenter Server"));
				else if(osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
					buffer.append(TEXT("Advanced Server"));
				else buffer.append(TEXT("Server"));
			}
		}
		if (_tcslen(osvi.szCSDVersion) > 0)
		{
			buffer.append(TEXT(" "));
			buffer.append(osvi.szCSDVersion);
		}

		buffer.appendfmt(TEXT(" (build %d)"), osvi.dwBuildNumber);
	}
	else
	{
		if (VER_PLATFORM_WIN32_NT == osvi.dwPlatformId)
		{
			buffer.append(TEXT("Microsoft Windows NT "));
			if (osvi.wProductType == VER_NT_WORKSTATION)
				buffer.append(TEXT("Workstation 4.0 "));
			else if (osvi.wSuiteMask & VER_SUITE_ENTERPRISE)
				buffer.append(TEXT("Server 4.0, Enterprise Edition "));
			else 
				buffer.append(TEXT("Server 4.0 "));
		}

		if (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS && osvi.dwMajorVersion == 4)
		{
			if (osvi.dwMinorVersion == 0)
			{
				buffer.append(TEXT("Microsoft Windows 95 "));
				if (osvi.szCSDVersion[1]==TEXT('C') || osvi.szCSDVersion[1]==TEXT('B'))
					buffer.append(TEXT("OSR2 "));
			} 

			if (osvi.dwMinorVersion == 10)
			{
				buffer.append(TEXT("Microsoft Windows 98 "));
				if (osvi.szCSDVersion[1]==TEXT('A') || osvi.szCSDVersion[1]==TEXT('B'))
					buffer.append(TEXT("SE "));
			} 

			if (osvi.dwMinorVersion == 90)
			{
				buffer.append(TEXT("Microsoft Windows Millennium Edition"));
			} 
			buffer.appendfmt(TEXT("(build %d)"), LOWORD(osvi.dwBuildNumber));
		}
		else if (osvi.dwPlatformId == VER_PLATFORM_WIN32s)
		{
			buffer.append(TEXT("Microsoft Win32s"));
		}
	}
}

int GetTZOffset(void)
{
	TIME_ZONE_INFORMATION tzInfo = {0};
	DWORD type = GetTimeZoneInformation(&tzInfo);

	int offset = 0;
	switch (type)
	{
	case TIME_ZONE_ID_DAYLIGHT:
		offset = -(tzInfo.Bias + tzInfo.DaylightBias);
		break;

	case TIME_ZONE_ID_STANDARD:
		offset = -(tzInfo.Bias + tzInfo.StandardBias);
		break;

	case TIME_ZONE_ID_UNKNOWN:
		offset = -tzInfo.Bias;
		break;
	}
	return offset;
}

void GetISO8061Time(SYSTEMTIME* stLocal, LPTSTR lpszString, DWORD dwSize)
{
	SYSTEMTIME loctime;
	if (stLocal == NULL) 
	{
		stLocal = &loctime;
		GetLocalTime(stLocal);
	}

	if (clsdates)
	{
		GetDateFormat(LOCALE_INVARIANT, 0, stLocal, TEXT("d MMM yyyy"), lpszString, dwSize);
		int dlen = (int)_tcslen(lpszString);
		GetTimeFormat(LOCALE_INVARIANT, 0, stLocal, TEXT(" H:mm:ss"), lpszString+dlen, dwSize-dlen);
	}
	else
	{
		int offset = GetTZOffset();

		// Build a string showing the date and time.
		mir_sntprintf(lpszString, dwSize, TEXT("%d-%02d-%02d %02d:%02d:%02d%+03d%02d"), 
			stLocal->wYear, stLocal->wMonth, stLocal->wDay, 
			stLocal->wHour, stLocal->wMinute, stLocal->wSecond,
			offset / 60, offset % 60);
	}
}

void GetLastWriteTime(FILETIME* ftime, LPTSTR lpszString, DWORD dwSize)
{
	FILETIME ftLocal;
	SYSTEMTIME stLocal;

	// Convert the last-write time to local time.
	FileTimeToLocalFileTime(ftime, &ftLocal);
	FileTimeToSystemTime(&ftLocal, &stLocal);

	GetISO8061Time(&stLocal, lpszString, dwSize);
}

void GetLastWriteTime(LPCTSTR fileName, LPTSTR lpszString, DWORD dwSize)
{
	WIN32_FIND_DATA FindFileData;

	HANDLE hFind = FindFirstFile(fileName, &FindFileData);
	if (hFind == INVALID_HANDLE_VALUE) return;
	FindClose(hFind);

	GetLastWriteTime(&FindFileData.ftLastWriteTime, lpszString, dwSize);
}

typedef PLUGININFOEX * (__cdecl * Miranda_Plugin_Info) (DWORD mirandaVersion);

PLUGININFOEX* GetMirInfo(HMODULE hModule)
{
	Miranda_Plugin_Info bpi = (Miranda_Plugin_Info)GetProcAddress(hModule, "MirandaPluginInfoEx");
	if (bpi == NULL)
		return NULL;

	return bpi(mirandaVersion);
}


void GetInternetExplorerVersion(bkstring& buffer)
{
	HKEY hKey;
	DWORD size;

	TCHAR ieVersion[1024] = {0};
	TCHAR ieBuild[512] = {0};
	TCHAR iVer[64] = {0};

	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\Microsoft\\Internet Explorer"), 0, 
		KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
	{
		size = sizeof(ieBuild)/sizeof(ieBuild[0]);
		if (RegQueryValueEx(hKey, TEXT("Build"), NULL, NULL, (LPBYTE) ieBuild, &size) != ERROR_SUCCESS)
			ieBuild[0] = 0;

		size = sizeof(ieVersion)/sizeof(ieVersion[0]);
		if (RegQueryValueEx(hKey, TEXT("Version"), NULL, NULL, (LPBYTE) ieVersion, &size) != ERROR_SUCCESS)
			ieVersion[0] = 0;

		size = sizeof(iVer)/sizeof(iVer[0]);
		if (RegQueryValueEx(hKey, TEXT("IVer"), NULL, NULL, (LPBYTE) iVer, &size) != ERROR_SUCCESS)
			iVer[0] = 0;

		RegCloseKey(hKey);
	}

	buffer.append(TEXT("Internet Explorer: "));
	if (ieVersion[0] == 0)
	{
		if (iVer[0] == 0)
			buffer.append(TEXT("<not installed>"));
		else if (_tcscmp(iVer, TEXT("100")) == 0)
			buffer.append(TEXT("1.0"));
		else if (_tcscmp(iVer, TEXT("101")) == 0)
			buffer.append(TEXT("NT"));
		else if (_tcscmp(iVer, TEXT("102")) == 0)
			buffer.append(TEXT("2.0"));
		else if (_tcscmp(iVer, TEXT("103")) == 0)
			buffer.append(TEXT("3.0"));
	}
	else
	{
		buffer.append(ieVersion);
	}
	if (ieBuild[0] != 0)
	{
		buffer.appendfmt(TEXT(" (build %s)"), ieBuild);
	}
}


void TrimMultiSpaces(TCHAR* str)
{
	TCHAR *src = str, *dest = str;
	bool trimst = false;

	for (;;)
	{
		if (*src == TEXT(' '))
		{
			if (!trimst)
			{
				trimst = true;
				*dest++ = *src;
			}
		}
		else
		{
			trimst = false;
			*dest++ = *src;
		}
		if (*src++ == 0) break; 
	}
}				

void GetProcessorString(bkstring& buffer)
{
	HKEY hKey;
	DWORD size;

	TCHAR cpuIdent[512] = {0};
	TCHAR cpuName[512] = {0};

	if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, TEXT("Hardware\\Description\\System\\CentralProcessor\\0"), 0, 
		KEY_QUERY_VALUE, &hKey) == ERROR_SUCCESS)
	{
		size = sizeof(cpuName)/sizeof(cpuName[0]);
		if (RegQueryValueEx(hKey, TEXT("ProcessorNameString"), NULL, NULL, (LPBYTE) cpuName, &size) != ERROR_SUCCESS)
			_tcscpy(cpuName, TEXT("Unknown"));

		size = sizeof(cpuIdent)/sizeof(cpuIdent[0]);
		if (RegQueryValueEx(hKey, TEXT("Identifier"), NULL, NULL, (LPBYTE) cpuIdent, &size) != ERROR_SUCCESS)
			if (RegQueryValueEx(hKey, TEXT("VendorIdentifier"), NULL, NULL, (LPBYTE) cpuIdent, &size) != ERROR_SUCCESS)
				_tcscpy(cpuIdent, TEXT("Unknown"));

		RegCloseKey(hKey);
	}
	TrimMultiSpaces(cpuName);
	buffer.appendfmt(TEXT("CPU: %s [%s]"), cpuName, cpuIdent);

	if (pIsProcessorFeaturePresent && pIsProcessorFeaturePresent(PF_NX_ENABLED))
		buffer.append(TEXT(" [DEP Enabled]"));

	SYSTEM_INFO si = {0};
	GetSystemInfo(&si);

	if (si.dwNumberOfProcessors > 1)
		buffer.appendfmt(TEXT(" [%u CPUs]"), si.dwNumberOfProcessors);
}

void GetFreeMemoryString(bkstring& buffer)
{
	unsigned ram;
	if (pGlobalMemoryStatusEx)
	{
		MEMORYSTATUSEX ms = {0};
		ms.dwLength = sizeof(ms);
		pGlobalMemoryStatusEx(&ms);
		ram = (unsigned int) ((ms.ullTotalPhys / (1024 * 1024)) + 1);
	}
	else
	{
		MEMORYSTATUS ms = {0};
		ZeroMemory(&ms, sizeof(ms));
		ms.dwLength = sizeof(ms);
		GlobalMemoryStatus(&ms);
		ram = (unsigned int)(ms.dwTotalPhys/(1024*1024))+1;
	}
	buffer.appendfmt(TEXT("Installed RAM: %u MBytes"), ram);
}

void GetFreeDiskString(LPCTSTR dirname, bkstring& buffer)
{
	ULARGE_INTEGER tnb, tfb, fs = {0};
	if (pGetDiskFreeSpaceEx)
		pGetDiskFreeSpaceEx(dirname, &fs, &tnb, &tfb);
	else
	{
		DWORD SectorsPerCluster, BytesPerSector;
		DWORD NumberOfFreeClusters, TotalNumberOfClusters;

		GetDiskFreeSpace(dirname, &SectorsPerCluster, &BytesPerSector, 
			&NumberOfFreeClusters, &TotalNumberOfClusters);

		fs.QuadPart = BytesPerSector * SectorsPerCluster;
		fs.QuadPart *= NumberOfFreeClusters;
	}
	fs.QuadPart /= (1024*1024);

	buffer.appendfmt(TEXT("Free disk space on Miranda partition: %u MBytes"), fs.LowPart);
}

void ReadableExceptionInfo(PEXCEPTION_RECORD excrec, bkstring& buffer)
{
	buffer.append(TEXT("Exception: "));

	switch (excrec->ExceptionCode)
	{
	case EXCEPTION_BREAKPOINT:
		buffer.append(TEXT("User Defined Breakpoint"));
		break;

	case EXCEPTION_ACCESS_VIOLATION:
		buffer.append(TEXT("Access Violation"));
		break;

	case EXCEPTION_ARRAY_BOUNDS_EXCEEDED:
		buffer.append(TEXT("Array Bounds Exceeded"));
		break;

	case EXCEPTION_DATATYPE_MISALIGNMENT:
		buffer.append(TEXT("Datatype Misalignment"));
		break;

	case EXCEPTION_FLT_DENORMAL_OPERAND:
		buffer.append(TEXT("Floating Point denormlized operand"));
		break;

	case EXCEPTION_FLT_DIVIDE_BY_ZERO:
		buffer.append(TEXT("Floating Point divide by 0"));
		break;

	case EXCEPTION_FLT_INEXACT_RESULT:
		buffer.append(TEXT("Floating Point inexact result"));
		break;

	case EXCEPTION_FLT_INVALID_OPERATION:
		buffer.append(TEXT("Floating Point invalid operation"));
		break;

	case EXCEPTION_FLT_OVERFLOW:
		buffer.append(TEXT("Floating Point overflow"));
		break;

	case EXCEPTION_FLT_STACK_CHECK:
		buffer.append(TEXT("Floating Point stack overflow/underflow"));
		break;

	case EXCEPTION_FLT_UNDERFLOW:
		buffer.append(TEXT("Floating Point underflow"));
		break;

	case EXCEPTION_ILLEGAL_INSTRUCTION:
		buffer.append(TEXT("Invalid instruction executed"));
		break;

	case EXCEPTION_IN_PAGE_ERROR:
		buffer.append(TEXT("Access to the not present page"));
		break;

	case EXCEPTION_INT_DIVIDE_BY_ZERO:
		buffer.append(TEXT("Integer divide by zero"));
		break;

	case EXCEPTION_INT_OVERFLOW:
		buffer.append(TEXT("Integer overflow"));
		break;

	case EXCEPTION_PRIV_INSTRUCTION:
		buffer.append(TEXT("Priveleged instruction executed"));
		break;

	case EXCEPTION_STACK_OVERFLOW:
		buffer.append(TEXT("Stack overflow"));
		break;

	case 0xe06d7363:
		buffer.append(TEXT("Unhandled C++ software exception"));
		break;

	default:
		buffer.appendfmt(TEXT("%x"), excrec->ExceptionCode); 
		break;
	}

	buffer.appendfmt(TEXT(" at address %p."), excrec->ExceptionAddress); 

	if (excrec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION || 
		excrec->ExceptionCode == EXCEPTION_IN_PAGE_ERROR)
	{
		switch(excrec->ExceptionInformation[0])
		{
		case 0:
			buffer.appendfmt(TEXT(" Reading from address %p."), (LPVOID)excrec->ExceptionInformation[1]);
			break;

		case 1:
			buffer.appendfmt(TEXT(" Writing to address %p."), (LPVOID)excrec->ExceptionInformation[1]);
			break;

		case 8:
			buffer.appendfmt(TEXT(" DEP at address %p."), (LPVOID)excrec->ExceptionInformation[1]);
			break;
		}
	}
}

void GetAdminString(bkstring& buffer)
{
	BOOL b;
	__try
	{
		SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY;
		PSID AdministratorsGroup; 

		b = AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID,
			DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, &AdministratorsGroup); 
		if (b) 
		{
			if (!CheckTokenMembership(NULL, AdministratorsGroup, &b))
				b = FALSE;
			FreeSid(AdministratorsGroup); 
		}
		else
			b = GetLastError() == ERROR_CALL_NOT_IMPLEMENTED;
	}
	__except(EXCEPTION_EXECUTE_HANDLER) 
	{
		b = TRUE;
	}

	buffer.appendfmt(TEXT("Administrator privileges: %s"), b ? TEXT("Yes") : TEXT ("No"));
}

void GetLanguageString(bkstring& buffer)
{
	TCHAR name1[256], name2[256], name3[256], name4[256];

	GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SENGLANGUAGE, name1, 256);
	GetLocaleInfo(LOCALE_SYSTEM_DEFAULT, LOCALE_SENGLANGUAGE, name2, 256);

	if (pGetUserDefaultUILanguage && pGetSystemDefaultUILanguage)
	{
		GetLocaleInfo(MAKELCID(pGetUserDefaultUILanguage(), SORT_DEFAULT), LOCALE_SENGLANGUAGE, name3, 256);
		GetLocaleInfo(MAKELCID(pGetSystemDefaultUILanguage(), SORT_DEFAULT), LOCALE_SENGLANGUAGE, name4, 256);
	}
	else
	{
		_tcscpy(name3, name1);
		_tcscpy(name4, name2);
	}

	buffer.appendfmt(TEXT("OS Languages: (UI | Locale (User/System)) : %s/%s | %s/%s"), name3, name4, name1, name2);
}

void GetLanguagePackString(bkstring& buffer)
{
	buffer.append(TEXT("Language pack: ")); 
	if (packlcid == LOCALE_USER_DEFAULT)
		buffer.append(TEXT("No language pack installed"));
	else
	{
		TCHAR path[MAX_PATH] = TEXT("Locale id invalid");
		GetLocaleInfo(packlcid, LOCALE_SENGLANGUAGE, path, MAX_PATH);
		buffer.append(path);

		GetLocaleInfo(packlcid, LOCALE_SISO3166CTRYNAME, path, MAX_PATH);
		buffer.appendfmt(TEXT(" (%s) [%04x]"), path, packlcid);

		GetModuleFileName(NULL, path, MAX_PATH);

		LPTSTR fname = _tcsrchr(path, TEXT('\\'));
		if (fname == NULL) fname = path;
		mir_sntprintf(fname, MAX_PATH-(fname-path), TEXT("\\langpack_*.txt"));

		WIN32_FIND_DATA FindFileData;
		HANDLE hFind = FindFirstFile(path, &FindFileData);
		if (hFind == INVALID_HANDLE_VALUE) return;
		FindClose(hFind);

		mir_sntprintf(fname, MAX_PATH-(fname-path), TEXT("\\%s"), FindFileData.cFileName);
		HANDLE hDumpFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL,
			OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

		if (hDumpFile != INVALID_HANDLE_VALUE) 
		{
			char buf[8192];

			DWORD bytes = 0;
			ReadFile(hDumpFile, buf, 8190, &bytes, NULL);
			buf[bytes] = 0;

			char *id = strstr(buf, "FLID:");
			if (id != NULL)
			{
				char *endid = strchr(id, '\r');
				if (endid != NULL) *endid = 0;

				endid = strchr(id, '\n');
				if (endid != NULL) *endid = 0;

				TCHAR mirtime[30];
				GetLastWriteTime(path, mirtime, 30);

				TCHAR* tid;
				crsi_a2t(tid, id+5);
				buffer.appendfmt(TEXT(", %s, modified: %s"), tid, mirtime);
			}
			CloseHandle(hDumpFile);
		}
	}
}

void GetWow64String(bkstring& buffer)
{
	BOOL wow64 = 0;
	if (pIsWow64Process)
	{
		if (!pIsWow64Process(GetCurrentProcess(), &wow64))
		{
			wow64 = 0;
		}
	}
	if (wow64) buffer.append(TEXT(" [running inside WOW64]")); 
}


bool CreateDirectoryTree(LPTSTR szDir)
{
	DWORD dwAttr = GetFileAttributes(szDir);
	if (dwAttr != INVALID_FILE_ATTRIBUTES && (dwAttr & FILE_ATTRIBUTE_DIRECTORY))
		return true;

	TCHAR* pszSlash = _tcsrchr(szDir, TEXT('\\'));
	if (pszSlash == NULL)
		return false;

	*pszSlash = 0;
	bool res = CreateDirectoryTree(szDir);
	*pszSlash = TEXT('\\');

	if (res) res = CreateDirectory(szDir, NULL) != 0;

	return res;
}

void GetVersionInfo(HMODULE hLib, bkstring& buffer)
{
	HRSRC hVersion = FindResource(hLib, MAKEINTRESOURCE(VS_VERSION_INFO), RT_VERSION);
	if (hVersion != NULL)
	{
		HGLOBAL hGlobal = LoadResource(hLib, hVersion); 
		if (hGlobal != NULL)  
		{  
			LPVOID versionInfo  = LockResource(hGlobal);  
			if (versionInfo != NULL)
			{
				int vl = *(unsigned short*)versionInfo;
				unsigned *res = (unsigned*)versionInfo;
				while (*res != 0xfeef04bd && ((char*)res - (char*)versionInfo) < vl) ++res;

				if (((char*)res - (char*)versionInfo) < vl)
				{
					VS_FIXEDFILEINFO *vsInfo = (VS_FIXEDFILEINFO*)res;
					buffer.appendfmt(TEXT(" v.%u.%u.%u.%u"), 
						HIWORD(vsInfo->dwFileVersionMS), LOWORD(vsInfo->dwFileVersionMS),
						HIWORD(vsInfo->dwFileVersionLS), LOWORD(vsInfo->dwFileVersionLS));
				}
			}
			FreeResource(hGlobal);  
		}
	}
}

void StoreStringToClip(bkstring& buffer)
{
	HANDLE hData = GlobalAlloc(GMEM_MOVEABLE, buffer.sizebytes() + sizeof(TCHAR));
	LPSTR buf = (LPSTR)GlobalLock(hData);

	memcpy(buf, buffer.c_str(), buffer.sizebytes() + sizeof(TCHAR));

	GlobalUnlock(hData);

	OpenClipboard(NULL);
	EmptyClipboard();


	SetClipboardData(CF_UNICODETEXT, hData);
	CloseClipboard();
}

bool IsPluginEnabled(TCHAR* filename)
{
	char* fname;
	crsi_t2a(fname, filename);
	char* ext = strstr(_strlwr(fname), ".dll");
	bool res = ext && ext[4] == '\0' && db_get_b(NULL, "PluginDisable", fname, 0) == 0;
	return res;
}