aboutsummaryrefslogtreecommitdiff
path: root/Src/ie_plugin/main.cpp
blob: 4a8a8f7b7d3bc62cd9fb5e374ded69ae611396fa (plain) (blame)
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
#include <windows.h>
#include "Winamp.h"
#include "resource.h"
#include "WinampFactory.h"
#define INITGUID
#include <guiddef.h>
#include <strsafe.h>

//D9C17076-9F55-49b5-8BEB-6A857931E62C
DEFINE_GUID(CLSID_Winamp,0xD9C17076,0x9F55,0x49b5,0x8B,0xEB,0x6A,0x85,0x79,0x31,0xE6,0x2C);

static HRESULT UnregisterComponent(const CLSID &clsid);
static HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName);

static const WCHAR szComponentFriendlyName[]		= L"Winamp Application Detector";

static HINSTANCE hMainInstance=0;
static DWORD regID = 0;

extern "C"
BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
{
   if (dwReason == DLL_PROCESS_ATTACH)
	 {
      hMainInstance=hInstance;
			
			
		//	CoRegisterClassObject(CLSID_Winamp,(IClassFactory*)&cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regID);
	 }
   return TRUE;    // ok
}

STDAPI DllRegisterServer()
{
	HRESULT hr(S_OK);
	hr = RegisterComponent(hMainInstance, CLSID_Winamp, szComponentFriendlyName);
	return hr;
}

STDAPI DllCanUnloadNow()
{
	return S_OK;
}

STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppv)
{
	    HRESULT hr = E_OUTOFMEMORY; 
    *ppv = NULL; 
 
    WinampFactory *winampFactory = new WinampFactory(); 
    if (winampFactory != NULL)   { 
        hr = winampFactory->QueryInterface(riid, ppv); 
        winampFactory->Release(); 
    } 
    return hr;

}

STDAPI DllUnregisterServer()
{
	HRESULT hr(S_OK);
	hr = UnregisterComponent(CLSID_Winamp);
	return hr;
}

static BOOL WriteRegKey(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszValue, HKEY *phKey = NULL)
{
	HKEY hKey;
	LONG result;
	result = RegCreateKeyExW(hKeyParent, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
	if (ERROR_SUCCESS != result || !hKey) return FALSE;
	if (pszValue) {		
		result = RegSetValueExW(hKey, NULL, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
	}
	if (!phKey) RegCloseKey(hKey);
	else *phKey = hKey;

	return (ERROR_SUCCESS == result);
}

static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, LPCWSTR pszValue)
{
	HKEY hKey;
	LONG result;
	result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
	if (ERROR_SUCCESS != result || !hKey) return FALSE;
	if (pszValue) 
	{		
		result = RegSetValueEx(hKey, pszEntry, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
	}
	RegCloseKey(hKey);
	return (ERROR_SUCCESS == result);
}

static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, DWORD pszValue)
{
	HKEY hKey;
	LONG result;
	result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
	if (ERROR_SUCCESS != result || !hKey) return FALSE;
	if (pszValue) 
	{		
		result = RegSetValueEx(hKey, pszEntry, 0, REG_DWORD, (BYTE*)&pszValue, sizeof(DWORD));
	}
	RegCloseKey(hKey);
	return (ERROR_SUCCESS == result);
}

static LONG DeleteRegKey(HKEY hKeyParent, LPCWSTR pszKey)
{	
	HKEY hKey;
	LONG result;
	FILETIME time;
	WCHAR szBuffer[512];
	DWORD dwSize = sizeof(szBuffer)/sizeof(WCHAR);

	result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE | KEY_ENUMERATE_SUB_KEYS , &hKey);
	if (ERROR_SUCCESS != result) return result;
	while (ERROR_SUCCESS == RegEnumKeyExW(hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time))
	{
		if (ERROR_SUCCESS != (result = DeleteRegKey(hKey, szBuffer)))
		{
			RegCloseKey(hKey);
			return result;
		}
		dwSize = sizeof(szBuffer)/sizeof(WCHAR);
	}
	
	RegCloseKey(hKey);
	return RegDeleteKeyW(hKeyParent, pszKey);
}

static HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName)
{
	HKEY hKey, hKey2=0;
	BOOL br;
	WCHAR szBuffer[MAX_PATH], szCLSID[64]; 
	
	if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
	StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"CLSID\\%s", szCLSID);
		
	if (!WriteRegKey(HKEY_CLASSES_ROOT, szBuffer, pszFriendlyName, &hKey)) return E_ACCESSDENIED;
			
	if (!GetModuleFileNameW(hModule, szBuffer, sizeof(szBuffer)/sizeof(WCHAR))) return S_FALSE;
	//wchar_t localizedString[MAX_PATH+15];
	//StringCbPrintf(localizedString, sizeof(localizedString), L"@%s,-%u", szBuffer, IDS_WINAMP);

	br = (WriteRegKey(hKey, L"InProcServer32" , szBuffer, &hKey2) &&
		WriteRegValue(hKey2, NULL, L"ThreadingModel", L"Both"));
	RegCloseKey(hKey);
	if (hKey2) RegCloseKey(hKey2);
	if (!br) return E_ACCESSDENIED;

	return S_OK;
}

static HRESULT UnregisterComponent(const CLSID &clsid)
{
	LONG result;
	WCHAR szBuffer[MAX_PATH], szCLSID[64]; 
	if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
	StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"CLSID\\%s", szCLSID);

	result = DeleteRegKey(HKEY_CLASSES_ROOT, szBuffer);
	if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;


	return S_OK;
}