Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

206 lines
5.2 KiB

  1. // PNGFilter.cpp : Implementation of DLL Exports.
  2. // You will need the NT SUR Beta 2 SDK or VC 4.2 in order to build this
  3. // project. This is because you will need MIDL 3.00.15 or higher and new
  4. // headers and libs. If you have VC 4.2 installed, then everything should
  5. // already be configured correctly.
  6. // Note: Proxy/Stub Information
  7. // To build a separate proxy/stub DLL,
  8. // run nmake -f WMFFilterps.mak in the project directory.
  9. #include "stdafx.h"
  10. #include "resource.h"
  11. #include "initguid.h"
  12. #include "pngfilt.h"
  13. #include "cpngfilt.h"
  14. #include <advpub.h>
  15. #define IID_DEFINED
  16. #ifdef UNIX
  17. # include "pngfilt.ic"
  18. #else
  19. # include "pngfilt_i.c"
  20. #endif
  21. #pragma warning( disable: 4505 )
  22. HRESULT WriteMIMEKeys(LPCTSTR lpszCLSID, LPTSTR lpszMIME, int nBytes, BYTE * pbID);
  23. CComModule _Module;
  24. BEGIN_OBJECT_MAP(ObjectMap)
  25. OBJECT_ENTRY(CLSID_CoPNGFilter, CPNGFilter)
  26. END_OBJECT_MAP()
  27. /////////////////////////////////////////////////////////////////////////////
  28. // DLL Entry Point
  29. extern "C"
  30. BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID /*lpReserved*/)
  31. {
  32. if (dwReason == DLL_PROCESS_ATTACH)
  33. {
  34. _Module.Init(ObjectMap, hInstance);
  35. DisableThreadLibraryCalls(hInstance);
  36. }
  37. else if (dwReason == DLL_PROCESS_DETACH)
  38. {
  39. _Module.Term();
  40. }
  41. return TRUE; // ok
  42. }
  43. /////////////////////////////////////////////////////////////////////////////
  44. // Used to determine whether the DLL can be unloaded by OLE
  45. STDAPI DllCanUnloadNow(void)
  46. {
  47. return (_Module.GetLockCount()==0) ? S_OK : S_FALSE;
  48. }
  49. /////////////////////////////////////////////////////////////////////////////
  50. // Returns a class factory to create an object of the requested type
  51. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv)
  52. {
  53. return _Module.GetClassObject(rclsid, riid, ppv);
  54. }
  55. /////////////////////////////////////////////////////////////////////////////
  56. // DllRegisterServer - Adds entries to the system registry
  57. BYTE byPNGID[] = { 0x08, 0x00, 0x00, 0x00, // length
  58. 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF, // mask
  59. 0x89,0x50,0x4E,0x47,0x0D,0x0A,0x1A,0x0A // data
  60. };
  61. STDAPI ie3_DllRegisterServer(void)
  62. {
  63. HRESULT hr;
  64. // registers object, typelib and all interfaces in typelib
  65. hr = _Module.RegisterServer(FALSE);
  66. if (FAILED(hr))
  67. return hr;
  68. hr = WriteMIMEKeys(_T("{A3CCEDF7-2DE2-11D0-86F4-00A0C913F750}"), _T("image/png"), sizeof(byPNGID), byPNGID);
  69. if (FAILED(hr))
  70. return hr;
  71. hr = WriteMIMEKeys(_T("{A3CCEDF7-2DE2-11D0-86F4-00A0C913F750}"), _T("image/x-png"), sizeof(byPNGID), byPNGID);
  72. return hr;
  73. }
  74. /////////////////////////////////////////////////////////////////////////////
  75. // DllUnregisterServer - Removes entries from the system registry
  76. STDAPI ie3_DllUnregisterServer(void)
  77. {
  78. _Module.UnregisterServer();
  79. return S_OK;
  80. }
  81. TCHAR szDatabase[] = _T("MIME\\Database\\Content Type\\");
  82. TCHAR szBits[] = _T("Bits");
  83. HRESULT WriteMIMEKeys(LPCTSTR lpszCLSID, LPTSTR lpszMIME, int nBytes, BYTE * pbID)
  84. {
  85. TCHAR szBuf[MAX_PATH];
  86. HKEY hkey, hkey2;
  87. DWORD dw;
  88. lstrcpy(szBuf, szDatabase);
  89. lstrcat(szBuf, lpszMIME);
  90. RegCreateKeyEx(HKEY_CLASSES_ROOT, szBuf, 0, NULL, 0, KEY_WRITE, NULL, &hkey, &dw);
  91. if (hkey)
  92. {
  93. RegSetValueEx(hkey, _T("Image Filter CLSID"), 0, REG_SZ, (LPBYTE)lpszCLSID, lstrlen(lpszCLSID)+1);
  94. RegCreateKeyEx(hkey, szBits, 0, NULL, 0, KEY_WRITE, NULL, &hkey2, &dw);
  95. if (hkey2)
  96. {
  97. RegSetValueEx(hkey2, _T("0"), 0, REG_BINARY, pbID, nBytes);
  98. RegCloseKey(hkey2);
  99. }
  100. RegCloseKey(hkey);
  101. }
  102. return S_OK;
  103. }
  104. static HINSTANCE hAdvPackLib;
  105. REGINSTALL GetRegInstallFn(void)
  106. {
  107. hAdvPackLib = LoadLibraryA("advpack.dll");
  108. if (!hAdvPackLib)
  109. return NULL;
  110. return (REGINSTALL)GetProcAddress(hAdvPackLib, achREGINSTALL);
  111. }
  112. inline void UnloadAdvPack(void)
  113. {
  114. FreeLibrary(hAdvPackLib);
  115. }
  116. STDAPI ie4_DllRegisterServer(void)
  117. {
  118. REGINSTALL pfnReg = GetRegInstallFn();
  119. HRESULT hr;
  120. if (pfnReg == NULL)
  121. return E_FAIL;
  122. // Delete any old registration entries, then add the new ones.
  123. hr = (*pfnReg)(_Module.GetResourceInstance(), "UnReg", NULL);
  124. if (SUCCEEDED(hr))
  125. hr = (*pfnReg)(_Module.GetResourceInstance(), "Reg", NULL);
  126. UnloadAdvPack();
  127. return hr;
  128. }
  129. STDAPI
  130. ie4_DllUnregisterServer(void)
  131. {
  132. REGINSTALL pfnReg = GetRegInstallFn();
  133. HRESULT hr;
  134. if (pfnReg == NULL)
  135. return E_FAIL;
  136. hr = (*pfnReg)( _Module.GetResourceInstance(), "UnReg", NULL);
  137. UnloadAdvPack();
  138. return hr;
  139. }
  140. STDAPI DllRegisterServer(void)
  141. {
  142. REGINSTALL pfnReg = GetRegInstallFn();
  143. UnloadAdvPack();
  144. if (pfnReg)
  145. return ie4_DllRegisterServer();
  146. else
  147. return ie3_DllRegisterServer();
  148. }
  149. STDAPI
  150. DllUnregisterServer(void)
  151. {
  152. REGINSTALL pfnReg = GetRegInstallFn();
  153. UnloadAdvPack();
  154. if (pfnReg)
  155. return ie4_DllUnregisterServer();
  156. else
  157. return ie3_DllUnregisterServer();
  158. }