summaryrefslogblamecommitdiffstats
path: root/Src/Elevator/FileTypeRegistrar.cpp
blob: e56da01b437d38abb1d0e7b461fbf21d2270de4e (plain) (tree)
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
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966





































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                                                                                                                                                
#include "FileTypeRegistrar.h"
#include <windows.h>
#include <shlwapi.h>
#include <shellapi.h>
#include <shobjidl.h>
#define _STD_USING
//#include "../nu/cast64.h"
#include <strsafe.h>
#ifdef ELEVATOR_SCOPE
void Lock();
void UnLock();
#endif

#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE) && (_WIN32_WINNT >= 0x0501)
#include <VersionHelpers.h>
#endif

FileTypeRegistrar::FileTypeRegistrar()
{
	refCount = 0;
}

HRESULT FileTypeRegistrar::QueryInterface(REFIID riid, LPVOID FAR *ppvObj)
{
	if (!ppvObj)
		return E_POINTER;
	else if (IsEqualIID(riid, IID_IUnknown))
		*ppvObj = static_cast<IUnknown *>(this);
	else if (IsEqualIID(riid, __uuidof(IFileTypeRegistrar)))
		*ppvObj = static_cast<IFileTypeRegistrar *>(this);
	else
	{
		*ppvObj = NULL;
		return E_NOINTERFACE;
	}

	AddRef();
	return S_OK;
}

ULONG FileTypeRegistrar::AddRef(void)
{
#ifdef ELEVATOR_SCOPE
	Lock();
#endif
	return ++refCount;
}

ULONG FileTypeRegistrar::Release(void)
{
	ULONG retCount=--refCount;
#ifdef ELEVATOR_SCOPE
	UnLock();
#endif
	return retCount;
}

static LONG SetValue(HKEY hkey, const wchar_t *data)
{
	return RegSetValueExW(hkey, NULL,0,REG_SZ,(const BYTE *)data, (DWORD)(sizeof(wchar_t)*(wcslen(data)+1)));
}

static LONG SetValue(HKEY hkey, const wchar_t *value, const wchar_t *data)
{
	return RegSetValueExW(hkey, value,0,REG_SZ,(const BYTE *)data, (DWORD)(sizeof(wchar_t)*(wcslen(data)+1)));
}

HRESULT FileTypeRegistrar::RegisterMIMEType(const wchar_t *mimeType, const wchar_t *programName, const wchar_t *extension, int netscapeOnly)
{
	HKEY mp3Key = NULL;

	if (!netscapeOnly)
	{
		wchar_t s[MAX_PATH] = {0};
		if (RegCreateKeyW(HKEY_CLASSES_ROOT, extension, &mp3Key) == ERROR_SUCCESS)
		{
			SetValue(mp3Key, L"Content Type", mimeType);
			RegCloseKey(mp3Key);
		}
		StringCchPrintfW(s, MAX_PATH, L"MIME\\Database\\Content Type\\%s", mimeType);
		if (RegCreateKeyW(HKEY_CLASSES_ROOT, s, &mp3Key) == ERROR_SUCCESS)
		{
			RegDeleteValueW(mp3Key, L"CLSID");
			SetValue(mp3Key, L"Extension", extension);
			RegCloseKey(mp3Key);
		}
	}
	if (RegOpenKeyW(HKEY_CURRENT_USER, L"Software\\Netscape\\Netscape Navigator\\Viewers", &mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, mimeType, programName);
		for (int x = 0; x < 999; x ++)
		{
			wchar_t st[100] = {0};
			DWORD vt;
			DWORD s = 128;
			wchar_t b[128] = {0};
			StringCchPrintfW(st, 100, L"TYPE%d", x);
			if (RegQueryValueExW(mp3Key, st, 0, &vt, (LPBYTE)b, &s) == ERROR_SUCCESS)
			{
				if (!wcscmp(b, mimeType)) break;
			}
			else
			{
				SetValue(mp3Key, st, mimeType);
				break;
			}
		}
		RegCloseKey(mp3Key);
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::RegisterCDPlayer(const wchar_t *programName)
{
	wchar_t buf2[MAX_PATH] = {0};
	HKEY mp3Key = NULL;
	StringCchPrintfW(buf2, MAX_PATH, L"\"%s\" %%1", programName);
	if (RegOpenKeyW(HKEY_CLASSES_ROOT, L"AudioCD\\shell\\play\\command", &mp3Key) == ERROR_SUCCESS)
	{
		wchar_t b[MAX_PATH] = {0};
		DWORD s = sizeof(b)/sizeof(*b);
		if (RegQueryValueEx(mp3Key, NULL, 0, NULL, (LPBYTE)b, &s) == ERROR_SUCCESS)
		{
			if (_wcsicmp(b, buf2))
			{
				wchar_t buf3[MAX_PATH] = {0};
				DWORD st = sizeof(buf3)/sizeof(*buf3);
				if (RegQueryValueExW(mp3Key, L"Winamp_Back", 0, NULL, (LPBYTE)buf3, &st) != ERROR_SUCCESS ||
					_wcsicmp(buf3, b))
				{
					SetValue(mp3Key, L"Winamp_Back", b);
				}
				SetValue(mp3Key, buf2);
			}
		}
		else SetValue(mp3Key, buf2);

		RegCloseKey(mp3Key);
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::UnregisterCDPlayer(const wchar_t *programName)
{
	wchar_t buf2[MAX_PATH] = {0};
	HKEY mp3Key = NULL;
	StringCchPrintfW(buf2, MAX_PATH, L"\"%s\" %%1", programName);
	if (RegOpenKeyW(HKEY_CLASSES_ROOT, L"AudioCD\\shell\\play\\command", &mp3Key) == ERROR_SUCCESS)
	{
		wchar_t b[MAX_PATH] = {0};
		DWORD s = sizeof(b)/sizeof(*b);
		if (RegQueryValueEx(mp3Key, NULL, 0, NULL, (LPBYTE)b, &s) == ERROR_SUCCESS)
		{
			if (!wcscmp(b, buf2))
			{
				s = sizeof(b);
				if (RegQueryValueExW(mp3Key, L"Winamp_Back", 0, NULL, (LPBYTE)b, &s) == ERROR_SUCCESS)
				{
					if (!_wcsicmp(b, buf2)) b[0] = 0;
					if (SetValue(mp3Key, b) == ERROR_SUCCESS)
						RegDeleteValueW(mp3Key, L"Winamp_Back");
				}
				else
				{
					buf2[0] = 0;
					SetValue(mp3Key, buf2);
				}
			}
		}

		RegCloseKey(mp3Key);
	}
	return S_OK;
}

static LONG myRegDeleteKeyEx(HKEY thiskey, LPCWSTR lpSubKey)
{
	HKEY key = NULL;
	int retval = RegOpenKeyW(thiskey, lpSubKey, &key);
	if (retval == ERROR_SUCCESS)
	{
		wchar_t buffer[1024] = {0};
		while (RegEnumKeyW(key, 0, buffer, 1024) == ERROR_SUCCESS)
			if ((retval = myRegDeleteKeyEx(key, buffer)) != ERROR_SUCCESS) break;
		RegCloseKey(key);
		retval = RegDeleteKeyW(thiskey, lpSubKey);
	}
	return retval;
}

HRESULT FileTypeRegistrar::RegisterType(const wchar_t *extension, const wchar_t *which_str, const wchar_t *prog_name)
{
	HKEY mp3Key = NULL;
	LONG regResult = RegCreateKeyW(HKEY_CLASSES_ROOT, extension, &mp3Key);

	if (regResult == ERROR_SUCCESS)
	{
		wchar_t b[128] = {0};
		DWORD s = sizeof(b)/sizeof(*b);
		if (RegQueryValueEx(mp3Key, NULL, 0, NULL, (LPBYTE)b, &s) == ERROR_SUCCESS)
		{
			if (wcsncmp(b, which_str, wcslen(b)))
			{
				SetValue(mp3Key, L"Winamp_Back", b);
				SetValue(mp3Key, which_str);
			}
		}
		else SetValue(mp3Key, which_str);

		if (mp3Key) RegCloseKey(mp3Key);
	}


#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE) && (_WIN32_WINNT >= 0x0501)
	if (IsWindowsVersionOrGreater(6, 0, 0)) // Vista
#else
		OSVERSIONINFO version = {0};
		version.dwOSVersionInfoSize = sizeof(version);
		if (!GetVersionEx(&version)) ZeroMemory(&version, sizeof(OSVERSIONINFO));
		if (version.dwMajorVersion >= 6) // Vista
#endif
	{
		TCHAR szKey[256] = {0};
		StringCbPrintfW(szKey, sizeof(szKey), L"Software\\Clients\\Media\\%s\\Capabilities\\FileAssociations", prog_name);
		if (ERROR_SUCCESS == RegCreateKeyW(HKEY_LOCAL_MACHINE, szKey, &mp3Key))
		{
			SetValue(mp3Key, extension, which_str);

			// TODO: cache IApplicationAssociationRegistration
			IApplicationAssociationRegistration* pAAR = NULL;
			HRESULT hr = CoCreateInstance(CLSID_ApplicationAssociationRegistration,
										  NULL, CLSCTX_INPROC,
										  __uuidof(IApplicationAssociationRegistration),
										  (void**)&pAAR);
			if (SUCCEEDED(hr) && pAAR)
			{
				pAAR->SetAppAsDefault(prog_name, extension, AT_FILEEXTENSION);
				pAAR->Release();
			}

			RegCloseKey(mp3Key);
		}
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::UnregisterType(const wchar_t *extension, const wchar_t *which_str, const wchar_t *prog_name, int is_playlist)
{
	HKEY mp3Key = NULL;

	LONG regResult = RegOpenKeyW(HKEY_CLASSES_ROOT, extension, &mp3Key);

	if (regResult == ERROR_SUCCESS)
	{
		wchar_t b[128] = {0};
		DWORD s = sizeof(b)/sizeof(*b);
		if (RegQueryValueEx(mp3Key, NULL, 0, NULL, (LPBYTE)b, &s) == ERROR_SUCCESS)
		{
			size_t len = min(wcslen(b), wcslen(which_str));
			if (!wcsncmp(b, which_str, len))
			{
				s = sizeof(b)/sizeof(*b);
				if (RegQueryValueExW(mp3Key, L"Winamp_Back", 0, NULL, (LPBYTE)b, &s) == ERROR_SUCCESS)
				{
					if (SetValue(mp3Key, b) == ERROR_SUCCESS)
						RegDeleteValueW(mp3Key, L"Winamp_Back");
				}
				else
				{
					RegDeleteValue(mp3Key, NULL);
					RegCloseKey(mp3Key);
					mp3Key = NULL;
					myRegDeleteKeyEx(HKEY_CLASSES_ROOT, extension);
				}
			}
		}

		if (mp3Key) RegCloseKey(mp3Key);

		if(!is_playlist)
		{
			myRegDeleteKeyEx(HKEY_CLASSES_ROOT, which_str);
		}
	}
	// TODO: benski> it'd be nice to have a function to remove any capabilities for which we no longer have a plugin for ...
#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE) && (_WIN32_WINNT >= 0x0501)
	if (IsWindowsVersionOrGreater(6, 0, 0)) // Vista
#else
	OSVERSIONINFO version = { 0 };
	version.dwOSVersionInfoSize = sizeof(version);
	if (!GetVersionEx(&version)) ZeroMemory(&version, sizeof(OSVERSIONINFO));
	if (version.dwMajorVersion >= 6) // Vista
#endif
	{
		TCHAR szKey[256] = {0};
		StringCbPrintfW(szKey, sizeof(szKey), L"Software\\Clients\\Media\\%s\\Capabilities\\FileAssociations", prog_name);
		if (ERROR_SUCCESS == RegCreateKey(HKEY_LOCAL_MACHINE, szKey, &mp3Key))
		{
			RegDeleteValue(mp3Key, extension);
			RegCloseKey(mp3Key);
		}
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::AddDirectoryContext(const wchar_t *commandLine, const wchar_t *which_str, const wchar_t *description)
{
	HKEY mp3Key = NULL;
	wchar_t regStr[256] = {0};
	StringCbPrintfW(regStr, sizeof(regStr), L"SOFTWARE\\Classes\\Directory\\shell\\%s", which_str);
	if (RegCreateKeyW(HKEY_LOCAL_MACHINE, regStr,&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, description);
		RegCloseKey(mp3Key);
	}

	StringCbPrintfW(regStr, sizeof(regStr), L"SOFTWARE\\Classes\\Directory\\shell\\%s\\command", which_str);
	if (RegCreateKeyW(HKEY_LOCAL_MACHINE,regStr,&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, commandLine);
		RegCloseKey(mp3Key);
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::RemoveDirectoryContext(const wchar_t *which_str)
{
	wchar_t regStr[256] = {0};
	StringCbPrintfW(regStr, sizeof(regStr), L"SOFTWARE\\Classes\\Directory\\shell\\%s", which_str);
	myRegDeleteKeyEx(HKEY_LOCAL_MACHINE, regStr);
	return S_OK;
}

HRESULT FileTypeRegistrar::AddAgent(const wchar_t *agentFilename)
{
	HKEY key = NULL;
	STARTUPINFOW si = {sizeof(si), };
	PROCESS_INFORMATION pi = {0, };

	if (RegCreateKeyW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &key) == ERROR_SUCCESS)
	{
		SetValue(key, L"WinampAgent", agentFilename);
		RegCloseKey(key);
	}
	CreateProcessW(NULL, (LPWSTR)agentFilename, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
	CloseHandle(pi.hProcess);
	CloseHandle(pi.hThread);
	return S_OK;
}

HRESULT FileTypeRegistrar::RemoveAgent()
{
	HKEY key = NULL;

	// caller will have done this also, but we'll do it again at elevated level just in case that's where winamp agent is running
	HWND hwnd = FindWindow(L"WinampAgentMain", NULL);
	if (hwnd)
	{
		SendMessageW(hwnd, WM_CLOSE, 0, 0);
	}

	if (RegOpenKey(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", &key) == ERROR_SUCCESS)
	{
		RegDeleteValue(key, L"WinampAgent");
		RegCloseKey(key);
	}
	return S_OK;
}

static LONG RegMedia_CreateKey(LPCWSTR lpSubKey, HKEY *phkResult, LPCWSTR prog_name)
{
	WCHAR szKey[256] = {0};
	HRESULT hr = (lpSubKey && *lpSubKey) ?
					StringCbPrintfW(szKey, sizeof(szKey), L"Software\\Clients\\Media\\%s\\%s", prog_name, lpSubKey) :
					StringCbPrintfW(szKey, sizeof(szKey), L"Software\\Clients\\Media\\%s", prog_name);
	if (S_OK != hr) return ERROR_NOT_ENOUGH_MEMORY;
	return RegCreateKeyW(HKEY_LOCAL_MACHINE, szKey, phkResult);
}

HRESULT FileTypeRegistrar::RegisterMediaPlayer(DWORD accessEnabled, const wchar_t *programName, const wchar_t *prog_name, int iconNumber)
{
	HKEY mp3Key = NULL;
	wchar_t str[MAX_PATH+128] = {0};
	
	if (RegMedia_CreateKey(NULL, &mp3Key, prog_name) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, prog_name);
		RegCloseKey(mp3Key);
	}

	if (RegMedia_CreateKey(L"DefaultIcon",&mp3Key, prog_name) == ERROR_SUCCESS)
	{
		StringCchPrintfW(str,MAX_PATH+128,L"%s,%d",programName,iconNumber);
		SetValue(mp3Key, str);
		RegCloseKey(mp3Key);
	}
	if (RegMedia_CreateKey(L"InstallInfo",&mp3Key, prog_name) == ERROR_SUCCESS)
	{
		RegSetValueExW(mp3Key, L"IconsVisible", 0, REG_DWORD, (BYTE *)&accessEnabled, sizeof(accessEnabled));
		StringCchPrintfW(str,MAX_PATH+128, L"\"%s\" /REG=AVCDL", programName);
		SetValue(mp3Key,L"ReinstallCommand", str);
		SetValue(mp3Key,L"ShowIconsCommand", str);
		StringCchPrintfW(str,MAX_PATH+128,L"\"%s\" /UNREG",programName);
		SetValue(mp3Key,L"HideIconsCommand", str);
		RegCloseKey(mp3Key);
	}
	if (RegMedia_CreateKey(L"shell",&mp3Key, prog_name) == ERROR_SUCCESS)
		RegCloseKey(mp3Key);

	if (RegMedia_CreateKey(L"shell\\open",&mp3Key, prog_name) == ERROR_SUCCESS)
		RegCloseKey(mp3Key);

	if (RegMedia_CreateKey(L"shell\\open\\command",&mp3Key, prog_name) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, programName);
		RegCloseKey(mp3Key);
	}

#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE) && (_WIN32_WINNT >= 0x0501)
	if (IsWindowsVersionOrGreater(6, 0, 0)) // Vista
#else
	OSVERSIONINFO version = { 0 };
	version.dwOSVersionInfoSize = sizeof(version);
	if (!GetVersionEx(&version)) ZeroMemory(&version, sizeof(OSVERSIONINFO));
	if (version.dwMajorVersion >= 6) // Vista
#endif
	{
		if (ERROR_SUCCESS == RegMedia_CreateKey(L"Capabilities",&mp3Key, prog_name))
		{
			SetValue(mp3Key,L"ApplicationDescription", L"Winamp. The Ultimate Media Player.");
			SetValue(mp3Key,L"ApplicationName", prog_name);
			RegCloseKey(mp3Key);
		}
		if (ERROR_SUCCESS == RegCreateKeyW(HKEY_LOCAL_MACHINE, L"Software\\RegisteredApplications", &mp3Key))
		{
			TCHAR szValue[256] = {0};
			StringCbPrintfW(szValue, sizeof(szValue), L"Software\\Clients\\Media\\%s\\Capabilities", prog_name);
			SetValue(mp3Key, prog_name, szValue);
			RegCloseKey(mp3Key);
		}

		if (ERROR_SUCCESS == RegMedia_CreateKey(L"Capabilities\\FileAssociations",&mp3Key, prog_name))
		{
			RegCloseKey(mp3Key);
		}

		if (ERROR_SUCCESS == RegMedia_CreateKey(L"Capabilities\\MimeAssociations",&mp3Key, prog_name))
		{
			RegCloseKey(mp3Key);
		}
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::RegisterMediaPlayerProtocol(LPCWSTR protocol, LPCWSTR prog_name)
{
	HKEY mp3Key = NULL;
#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE) && (_WIN32_WINNT >= 0x0501)
	if (IsWindowsVersionOrGreater(6, 0, 0)) // Vista
#else
	OSVERSIONINFO version = { 0 };
	version.dwOSVersionInfoSize = sizeof(version);
	if (!GetVersionEx(&version)) ZeroMemory(&version, sizeof(OSVERSIONINFO));
	if (version.dwMajorVersion >= 6) // Vista
#endif
	{
		if (ERROR_SUCCESS == RegMedia_CreateKey(L"Capabilities\\URLAssociations",&mp3Key, prog_name))
		{
			wchar_t szBuffer[64] = {0};

			StringCbCopyW(szBuffer, sizeof(szBuffer), protocol);
			CharUpperW(szBuffer);
			SetValue(mp3Key, protocol, szBuffer);

			RegCloseKey(mp3Key);
		}
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::UnregisterMediaPlayerProtocol(LPCWSTR protocol, LPCWSTR prog_name)
{
	HKEY mp3Key = NULL;
#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE) && (_WIN32_WINNT >= 0x0501)
	if (IsWindowsVersionOrGreater(6, 0, 0)) // Vista
#else
	OSVERSIONINFO version = { 0 };
	version.dwOSVersionInfoSize = sizeof(version);
	if (!GetVersionEx(&version)) ZeroMemory(&version, sizeof(OSVERSIONINFO));
	if (version.dwMajorVersion >= 6) // Vista
#endif
	{
		if (ERROR_SUCCESS == RegMedia_CreateKey(L"Capabilities\\URLAssociations",&mp3Key, prog_name))
		{
			RegDeleteValue(mp3Key, protocol);

			RegCloseKey(mp3Key);
		}
	}
	return S_OK;
}

static LONG RegCreateKey4(HKEY hKey, LPCWSTR lpSubKey, LPCWSTR lpSubKey2, PHKEY phkResult)
{
	wchar_t temp[1024] = {0};
	StringCchPrintfW(temp, 1024, L"%s%s", lpSubKey, lpSubKey2);
	return RegCreateKeyW(hKey,temp,phkResult);
}

static LONG RegCreateKey5(HKEY hKey, LPCWSTR lpSubKey, LPCWSTR lpSubKey2, LPCWSTR lpSubKey3, PHKEY phkResult)
{
	wchar_t temp[1024] = {0};
	StringCchPrintfW(temp, 1024, L"%s%s%s", lpSubKey, lpSubKey2, lpSubKey3);
	return RegCreateKeyW(hKey,temp,phkResult);
}

static LONG RegCreateKey6(HKEY hKey, LPCWSTR lpSubKey, LPCWSTR lpSubKey2, LPCWSTR lpSubKey3, LPCWSTR lpSubKey4, PHKEY phkResult)
{
	wchar_t temp[1024] = {0};
	StringCchPrintfW(temp, 1024, L"%s%s%s%s", lpSubKey, lpSubKey2, lpSubKey3,lpSubKey4);
	return RegCreateKeyW(hKey,temp,phkResult);
}

HRESULT FileTypeRegistrar::SetupShell(const wchar_t *commandLine, const wchar_t *winamp_file, const wchar_t *description, const wchar_t *commandName, const wchar_t *dragAndDropGUID)
{
	HKEY mp3Key = NULL;

	if (RegCreateKey5(HKEY_CLASSES_ROOT,winamp_file, L"\\shell\\", commandName,&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, description);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey6(HKEY_CLASSES_ROOT,winamp_file, L"\\shell\\", commandName, L"\\command",&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, commandLine);
		RegCloseKey(mp3Key);
	}

	/* Drag and Drop stuff */
	if (dragAndDropGUID && *dragAndDropGUID)
	{
		if (RegCreateKey6(HKEY_CLASSES_ROOT,winamp_file, L"\\shell\\", commandName, L"\\DropTarget",&mp3Key) == ERROR_SUCCESS)
		{
			SetValue(mp3Key, L"Clsid", dragAndDropGUID);
			RegCloseKey(mp3Key);
		}
	}

	return S_OK;
}

HRESULT FileTypeRegistrar::RemoveShell(const wchar_t *winamp_file, const wchar_t *commandName)
{
	wchar_t temp[1024] = {0};
	StringCchPrintfW(temp, 1024, L"%s\\shell\\%s", winamp_file, commandName);
	myRegDeleteKeyEx(HKEY_CLASSES_ROOT, temp);
	return S_OK;
}

HRESULT FileTypeRegistrar::SetupFileType(const wchar_t *programName, const wchar_t *winamp_file, const wchar_t *name, int iconNumber, const wchar_t *defaultShellCommand, const wchar_t *iconPath)
{
	HKEY mp3Key = NULL;
	wchar_t str[MAX_PATH+32] = {0};

	if (RegCreateKeyW(HKEY_CLASSES_ROOT,winamp_file,&mp3Key) == ERROR_SUCCESS)
	{
		StringCchCopyW(str,MAX_PATH+32,name);
		SetValue(mp3Key, str);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey4(HKEY_CLASSES_ROOT,winamp_file, L"\\DefaultIcon",&mp3Key) == ERROR_SUCCESS)
	{
		StringCchPrintfW(str,MAX_PATH+32,(iconPath[0]?L"%s":L"%s,%d"),(iconPath[0]?iconPath:programName),iconNumber);
		SetValue(mp3Key, str);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey4(HKEY_CLASSES_ROOT,winamp_file, L"\\shell",&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, defaultShellCommand);
		RegCloseKey(mp3Key);
	}

	return S_OK;
}

HRESULT FileTypeRegistrar::SetupDefaultFileType(const wchar_t *winamp_file, const wchar_t *defaultShellCommand){

	HKEY mp3Key = NULL;

	if (RegCreateKey4(HKEY_CLASSES_ROOT,winamp_file, L"\\shell",&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, defaultShellCommand);
		RegCloseKey(mp3Key);
	}

	return S_OK;
}

HRESULT FileTypeRegistrar::RegisterTypeShell(const wchar_t *programName, const wchar_t *which_file, const wchar_t *description, int iconNumber, const wchar_t *commandName)
{
	HKEY mp3Key = NULL;

	if (RegCreateKeyW(HKEY_CLASSES_ROOT,which_file,&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, description);
		char str[4];
		str[0]=0;
		str[1]=0;
		str[2]=1;
		str[3]=0;
		RegSetValueExW(mp3Key, L"EditFlags",0,REG_BINARY,(BYTE *)str,4);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey4(HKEY_CLASSES_ROOT, which_file, L"\\DefaultIcon",&mp3Key) == ERROR_SUCCESS)
	{
		wchar_t str[MAX_PATH+32] = {0};
		StringCchPrintfW(str,MAX_PATH+32,L"%s,%d",programName,iconNumber);
		SetValue(mp3Key, str);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey4(HKEY_CLASSES_ROOT,which_file, L"\\shell",&mp3Key) == ERROR_SUCCESS)
	{
		SetValue(mp3Key, commandName);
		RegCloseKey(mp3Key);
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::RegisterGUID(const wchar_t *programName, const wchar_t *guidString)
{
	HKEY mp3Key = NULL;

	if (RegCreateKey4(HKEY_CLASSES_ROOT,L"CLSID\\", guidString, &mp3Key) == ERROR_SUCCESS)
	{
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey5(HKEY_CLASSES_ROOT,L"CLSID\\", guidString, L"\\LocalServer32",&mp3Key) == ERROR_SUCCESS)
	{
		wchar_t str[MAX_PATH+32] = {0};
		StringCchPrintfW(str,MAX_PATH+32, L"\"%s\"",programName);
		SetValue(mp3Key, str);
		RegCloseKey(mp3Key);
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::RegisterDVDPlayer(const wchar_t *programName, int iconNumber, 
																						 const wchar_t *which_file /*Winamp.DVD*/, const wchar_t *commandName /* Winamp */,
																						 const wchar_t *provider /* Nullsoft Winamp */, const wchar_t *description /* Play in Winamp */)
{
	// TODO: stop hardcoding stuff as soon as we start using this
	wchar_t winampPath[MAX_PATH+128] = {0}; 
	wchar_t winampIcon[MAX_PATH] = {0};
	HKEY mp3Key = NULL;

	// create icon path and exe-with-param path
	StringCbPrintfW(winampIcon,sizeof(winampIcon), L"\"%s\", %d", programName, iconNumber); 
	StringCbPrintfW(winampPath,sizeof(winampPath), L"\"%s\" %1", programName);

	// uncomment if we ever want to overwrite the default icon... not set because we don't have a good dvd icon in winamp.exe
	/*
	if (RegOpenKey(HKEY_CLASSES_ROOT,"DVD\\DefaultIcon",&mp3Key) == ERROR_SUCCESS)
	{
	RegSetValueExW(mp3Key,NULL,0,REG_SZ,winampIcon,iconSize);
	}
	*/
	if (RegCreateKeyW(HKEY_CLASSES_ROOT,L"DVD\\shell",&mp3Key) == ERROR_SUCCESS)
	{
		// set winamp to be the default player
		SetValue(mp3Key, commandName);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey4(HKEY_CLASSES_ROOT,L"DVD\\shell\\", commandName, &mp3Key) == ERROR_SUCCESS)
	{
		// text to appear in right-click shell menu in explorer
		SetValue(mp3Key, description);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey5(HKEY_CLASSES_ROOT,L"DVD\\shell\\", commandName, L"\\command",&mp3Key) == ERROR_SUCCESS)
	{
		// set the executable path
		SetValue(mp3Key, winampPath);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKeyW(HKEY_LOCAL_MACHINE,L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\EventHandlers\\PlayDVDMovieOnArrival",&mp3Key) == ERROR_SUCCESS)
	{
		// register winamp for dvd autoplay
		RegSetValueExW(mp3Key,which_file,0,REG_SZ,0,0);
		RegCloseKey(mp3Key);
	}

	if (RegCreateKey4(HKEY_LOCAL_MACHINE,L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer\\AutoplayHandlers\\Handlers\\", which_file, &mp3Key) == ERROR_SUCCESS)
	{
		// autoplay details
		SetValue(mp3Key,L"Action", description);
		SetValue(mp3Key,L"DefaultIcon", winampIcon);
		SetValue(mp3Key,L"InvokeProgID", L"DVD");
		SetValue(mp3Key,L"InvokeVerb", commandName);
		SetValue(mp3Key,L"Provider", provider);
		RegCloseKey(mp3Key);
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::InstallItem(LPCWSTR sourceFile, LPCWSTR destinationFolder, LPCWSTR destinationFilename)
{
	SHFILEOPSTRUCT op = {0};

	op.hwnd = 0;
	op.wFunc = FO_COPY;

	wchar_t srcFile[MAX_PATH+1] = {0};
	wchar_t *end;
	StringCchCopyExW(srcFile, MAX_PATH, sourceFile, &end, 0, 0);
	if (end)
		end[1]=0; // double null terminate
	op.pFrom = srcFile;

	wchar_t destFile[MAX_PATH+1] = {0};
	PathCombineW(destFile, destinationFolder, destinationFilename);
	destFile[wcslen(destFile)+1]=0; // double null terminate
	op.pTo = destFile;

	op.fFlags=FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_SIMPLEPROGRESS|FOF_NOERRORUI|FOF_SILENT;
	op.lpszProgressTitle = L"";
	int val = SHFileOperation(&op);

	// send back the error message so we can show on the UI
	if (val != 0 && val != 0x71/*DE_SAMEFILE*/)
	{
		StringCchPrintfW((LPWSTR)destinationFilename,MAX_PATH,L"0x%x",val);
	}

	return ((val == 0 || val == 0x71/*DE_SAMEFILE*/) ? S_OK : E_FAIL);
}

HRESULT FileTypeRegistrar::DeleteItem(LPCWSTR file)
{
	SHFILEOPSTRUCT op = {0};
	op.wFunc = FO_DELETE;

	wchar_t srcFile[MAX_PATH+1] = {0};
	wchar_t *end;
	StringCchCopyExW(srcFile, MAX_PATH, file, &end, 0, 0);
	if (end) end[1]=0; // double null terminate
	op.pFrom = srcFile;
	op.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_SIMPLEPROGRESS|FOF_NORECURSION|FOF_NOERRORUI|FOF_SILENT|FOF_ALLOWUNDO;
	op.lpszProgressTitle = L"";
	int val = SHFileOperation(&op);
	return val == 0?S_OK:E_FAIL;
}

HRESULT FileTypeRegistrar::RenameItem(LPCWSTR oldFile, LPCWSTR newFile, BOOL force)
{
	SHFILEOPSTRUCT op = {0};
	op.wFunc = force?FO_MOVE:FO_RENAME;

	wchar_t srcFile[MAX_PATH+1] = {0};
	wchar_t *end;
	StringCchCopyExW(srcFile, MAX_PATH, oldFile, &end, 0, 0);
	if (end) end[1]=0; // double null terminate
	op.pFrom = srcFile;

	wchar_t destFile[MAX_PATH+1] = {0};
	end=0;
	StringCchCopyExW(destFile, MAX_PATH, newFile, &end, 0, 0);
	if (end) end[1]=0; // double null terminate
	op.pTo = destFile;

	op.fFlags=FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_SIMPLEPROGRESS|FOF_NOERRORUI|FOF_SILENT;
	op.lpszProgressTitle = L"";
	int val = SHFileOperation(&op);

	return val == 0?S_OK:E_FAIL;

	/*
	if (!MoveFile(oldFile, newFile))
	{
	// if move failed

	if (!force) // if they don't want to force the move
	return E_FAIL;

	if (CopyFile(oldFile, newFile, FALSE) && DeleteFile(oldFile)) // copy then delete
	return E_FAIL;
	}
	return S_OK;
	*/
}

HRESULT FileTypeRegistrar::CleanupDirectory(LPCWSTR directory)
{
	wchar_t dirmask[MAX_PATH] = {0};
	WIN32_FIND_DATAW d = {0};
	PathCombineW(dirmask, directory, L"*.*");
	HANDLE h = FindFirstFileW(dirmask, &d);
	if (h != INVALID_HANDLE_VALUE)
	{
		do
		{
			wchar_t v[MAX_PATH] = {0};
			PathCombineW(v, directory, d.cFileName);
			if (d.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
			{
				if (wcscmp(d.cFileName,L".") && wcscmp(d.cFileName,L".."))
					CleanupDirectory(v);
			}
			else
			{
				if(!DeleteFileW(v))
				{
					// this handles some rogue cases where files in the wlz's aren't unloadable
					MoveFileExW(v, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
					MoveFileExW(directory, NULL, MOVEFILE_DELAY_UNTIL_REBOOT);
				}
			}
		}
		while (FindNextFileW(h, &d));
		FindClose(h);
	}
	RemoveDirectoryW(directory);
	return S_OK;
}

HRESULT FileTypeRegistrar::MoveDirectoryContents(LPCWSTR oldDirectory, LPCWSTR newDirectory)
{
	wchar_t fromDirectory[MAX_PATH+1] = {0}; // need to zero it all so it will be double-null terminated
	SHFILEOPSTRUCT sf = {0};

	StringCchPrintfW(fromDirectory,MAX_PATH,L"%s\\*",oldDirectory);

	sf.wFunc = FO_MOVE;
	sf.pFrom = fromDirectory;
	sf.pTo = newDirectory;
	sf.fFlags = FOF_NOCONFIRMATION|FOF_NOCONFIRMMKDIR|FOF_NOERRORUI|FOF_NORECURSION;

	if(SHFileOperation(&sf)){
		return E_FAIL;
	}
	return S_OK;
}

HRESULT FileTypeRegistrar::WriteProKey(LPCWSTR name, LPCWSTR key)
{
	HKEY hkey = NULL;

	if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Nullsoft\\Winamp", 0, 0, 0, KEY_READ | KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
	{
		if (*name)
			SetValue(hkey, L"regname", name);
		else
			RegDeleteValueW(hkey, L"regname");

		if (*key)
		{
		wchar_t buf[33] = {0};
		StringCchCopyW(buf + 1, 32, key);
		buf[0] = '~';
		for (int x = 1;buf[x]; x ++)
		{
			wchar_t c = towupper(buf[x]); 
			//char c = buf[x]; // benski> goes with above commented line that i'd like to uncomment 
			if (c >= 'A' && c <= 'Z')
			{
				int w = c - 'A';
				w += x;
				w %= 26;
				buf[x] = 'A' + w;
			}
			else if (c >= '0' && c <= '9')
			{
				int w = c - '0';
				w += x * 27;
				w %= 10;
				buf[x] = 'a' + w;
			}
			else if (c == '-') buf[x] = '/';
		}
		SetValue(hkey, L"regkey", buf);
		}
		else
		{
			RegDeleteValueW(hkey, L"regkey");
		}
		RegCloseKey(hkey);
		return S_OK;
	}

	return E_FAIL;
}

HRESULT FileTypeRegistrar::WriteClientUIDKey(LPCWSTR path, LPCWSTR uid_str)
{
	HKEY hkey = NULL;

	if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Nullsoft\\Winamp", 0, 0, 0, KEY_READ | KEY_WRITE, NULL, &hkey, NULL) == ERROR_SUCCESS)
	{
		SetValue(hkey, path, uid_str);
		RegCloseKey(hkey);
		return S_OK;
	}

	return E_FAIL;
}

HRESULT FileTypeRegistrar::RegisterProtocol(LPCWSTR protocol, LPCWSTR command, LPCWSTR icon)
{
	return S_OK;

	HKEY mp3Key = NULL;

	if (RegCreateKeyW(HKEY_CLASSES_ROOT, protocol, &mp3Key) == ERROR_SUCCESS)
	{
		wchar_t regStr[256] = {0};
		SetValue(mp3Key, L"URL:Winamp Command Handler");
		SetValue(mp3Key, L"URL Protocol", L"");
		RegCloseKey(mp3Key);

		StringCbPrintfW(regStr, sizeof(regStr), L"%s\\DefaultIcon", protocol);
		if (RegCreateKeyW(HKEY_CLASSES_ROOT,regStr,&mp3Key) == ERROR_SUCCESS)
		{
			SetValue(mp3Key, icon);
			RegCloseKey(mp3Key);
		}

		StringCbPrintfW(regStr, sizeof(regStr), L"%s\\shell\\open\\command", protocol);
		if (RegCreateKeyW(HKEY_CLASSES_ROOT,regStr,&mp3Key) == ERROR_SUCCESS)
		{
			SetValue(mp3Key, command);
			RegCloseKey(mp3Key);
		}
	}

	return S_OK;
}

HRESULT FileTypeRegistrar::RegisterCapability(const wchar_t *programName, const wchar_t *winamp_file, const wchar_t *extension)
{
#if defined(_SYSINFOAPI_H_) && defined(NOT_BUILD_WINDOWS_DEPRECATE) && (_WIN32_WINNT >= 0x0501)
	if (IsWindowsVersionOrGreater(6, 0, 0)) // Vista
#else
	OSVERSIONINFO version = { 0 };
	version.dwOSVersionInfoSize = sizeof(version);
	if (!GetVersionEx(&version)) ZeroMemory(&version, sizeof(OSVERSIONINFO));
	if (version.dwMajorVersion >= 6) // Vista
#endif
	{
		HKEY hkey_file_associations = NULL;
		if (ERROR_SUCCESS == RegMedia_CreateKey(L"Capabilities\\FileAssociations", &hkey_file_associations, programName))
		{
			RegDeleteValueW(hkey_file_associations, extension); // to make sure that they are all in case that we need
			SetValue(hkey_file_associations, extension, winamp_file);
			RegCloseKey(hkey_file_associations);
		}
	}
	return S_OK;
}