main.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. #include <windows.h>
  2. #include "Winamp.h"
  3. #include "resource.h"
  4. #include "WinampFactory.h"
  5. #define INITGUID
  6. #include <guiddef.h>
  7. #include <strsafe.h>
  8. //D9C17076-9F55-49b5-8BEB-6A857931E62C
  9. DEFINE_GUID(CLSID_Winamp,0xD9C17076,0x9F55,0x49b5,0x8B,0xEB,0x6A,0x85,0x79,0x31,0xE6,0x2C);
  10. static HRESULT UnregisterComponent(const CLSID &clsid);
  11. static HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName);
  12. static const WCHAR szComponentFriendlyName[] = L"Winamp Application Detector";
  13. static HINSTANCE hMainInstance=0;
  14. static DWORD regID = 0;
  15. extern "C"
  16. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  17. {
  18. if (dwReason == DLL_PROCESS_ATTACH)
  19. {
  20. hMainInstance=hInstance;
  21. // CoRegisterClassObject(CLSID_Winamp,(IClassFactory*)&cf, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &regID);
  22. }
  23. return TRUE; // ok
  24. }
  25. STDAPI DllRegisterServer()
  26. {
  27. HRESULT hr(S_OK);
  28. hr = RegisterComponent(hMainInstance, CLSID_Winamp, szComponentFriendlyName);
  29. return hr;
  30. }
  31. STDAPI DllCanUnloadNow()
  32. {
  33. return S_OK;
  34. }
  35. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID * ppv)
  36. {
  37. HRESULT hr = E_OUTOFMEMORY;
  38. *ppv = NULL;
  39. WinampFactory *winampFactory = new WinampFactory();
  40. if (winampFactory != NULL) {
  41. hr = winampFactory->QueryInterface(riid, ppv);
  42. winampFactory->Release();
  43. }
  44. return hr;
  45. }
  46. STDAPI DllUnregisterServer()
  47. {
  48. HRESULT hr(S_OK);
  49. hr = UnregisterComponent(CLSID_Winamp);
  50. return hr;
  51. }
  52. static BOOL WriteRegKey(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszValue, HKEY *phKey = NULL)
  53. {
  54. HKEY hKey;
  55. LONG result;
  56. result = RegCreateKeyExW(hKeyParent, pszKey, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hKey, NULL);
  57. if (ERROR_SUCCESS != result || !hKey) return FALSE;
  58. if (pszValue) {
  59. result = RegSetValueExW(hKey, NULL, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
  60. }
  61. if (!phKey) RegCloseKey(hKey);
  62. else *phKey = hKey;
  63. return (ERROR_SUCCESS == result);
  64. }
  65. static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, LPCWSTR pszValue)
  66. {
  67. HKEY hKey;
  68. LONG result;
  69. result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
  70. if (ERROR_SUCCESS != result || !hKey) return FALSE;
  71. if (pszValue)
  72. {
  73. result = RegSetValueEx(hKey, pszEntry, 0, REG_SZ, (BYTE*)pszValue, (DWORD)(sizeof(wchar_t)*(1 + lstrlenW(pszValue))));
  74. }
  75. RegCloseKey(hKey);
  76. return (ERROR_SUCCESS == result);
  77. }
  78. static BOOL WriteRegValue(HKEY hKeyParent, LPCWSTR pszKey, LPCWSTR pszEntry, DWORD pszValue)
  79. {
  80. HKEY hKey;
  81. LONG result;
  82. result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE, &hKey);
  83. if (ERROR_SUCCESS != result || !hKey) return FALSE;
  84. if (pszValue)
  85. {
  86. result = RegSetValueEx(hKey, pszEntry, 0, REG_DWORD, (BYTE*)&pszValue, sizeof(DWORD));
  87. }
  88. RegCloseKey(hKey);
  89. return (ERROR_SUCCESS == result);
  90. }
  91. static LONG DeleteRegKey(HKEY hKeyParent, LPCWSTR pszKey)
  92. {
  93. HKEY hKey;
  94. LONG result;
  95. FILETIME time;
  96. WCHAR szBuffer[512];
  97. DWORD dwSize = sizeof(szBuffer)/sizeof(WCHAR);
  98. result = RegOpenKeyExW(hKeyParent, pszKey, 0, KEY_SET_VALUE | KEY_ENUMERATE_SUB_KEYS , &hKey);
  99. if (ERROR_SUCCESS != result) return result;
  100. while (ERROR_SUCCESS == RegEnumKeyExW(hKey, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time))
  101. {
  102. if (ERROR_SUCCESS != (result = DeleteRegKey(hKey, szBuffer)))
  103. {
  104. RegCloseKey(hKey);
  105. return result;
  106. }
  107. dwSize = sizeof(szBuffer)/sizeof(WCHAR);
  108. }
  109. RegCloseKey(hKey);
  110. return RegDeleteKeyW(hKeyParent, pszKey);
  111. }
  112. static HRESULT RegisterComponent(HMODULE hModule, const CLSID &clsid, LPCWSTR pszFriendlyName)
  113. {
  114. HKEY hKey, hKey2=0;
  115. BOOL br;
  116. WCHAR szBuffer[MAX_PATH], szCLSID[64];
  117. if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
  118. StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"CLSID\\%s", szCLSID);
  119. if (!WriteRegKey(HKEY_CLASSES_ROOT, szBuffer, pszFriendlyName, &hKey)) return E_ACCESSDENIED;
  120. if (!GetModuleFileNameW(hModule, szBuffer, sizeof(szBuffer)/sizeof(WCHAR))) return S_FALSE;
  121. //wchar_t localizedString[MAX_PATH+15];
  122. //StringCbPrintf(localizedString, sizeof(localizedString), L"@%s,-%u", szBuffer, IDS_WINAMP);
  123. br = (WriteRegKey(hKey, L"InProcServer32" , szBuffer, &hKey2) &&
  124. WriteRegValue(hKey2, NULL, L"ThreadingModel", L"Both"));
  125. RegCloseKey(hKey);
  126. if (hKey2) RegCloseKey(hKey2);
  127. if (!br) return E_ACCESSDENIED;
  128. return S_OK;
  129. }
  130. static HRESULT UnregisterComponent(const CLSID &clsid)
  131. {
  132. LONG result;
  133. WCHAR szBuffer[MAX_PATH], szCLSID[64];
  134. if (!StringFromGUID2(clsid, szCLSID, sizeof(szCLSID)/sizeof(WCHAR))) return E_OUTOFMEMORY;
  135. StringCchPrintfW(szBuffer, sizeof(szBuffer)/sizeof(WCHAR), L"CLSID\\%s", szCLSID);
  136. result = DeleteRegKey(HKEY_CLASSES_ROOT, szBuffer);
  137. if (ERROR_SUCCESS != result && ERROR_FILE_NOT_FOUND != result) return S_FALSE;
  138. return S_OK;
  139. }