main.cpp
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:12k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * main.cpp: ActiveX control for VLC
  3.  *****************************************************************************
  4.  * Copyright (C) 2005 the VideoLAN team
  5.  *
  6.  * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
  7.  *
  8.  * This program is free software; you can redistribute it and/or modify
  9.  * it under the terms of the GNU General Public License as published by
  10.  * the Free Software Foundation; either version 2 of the License, or
  11.  * (at your option) any later version.
  12.  *
  13.  * This program is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16.  * GNU General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU General Public License
  19.  * along with this program; if not, write to the Free Software
  20.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  21.  *****************************************************************************/
  22. #include "plugin.h"
  23. #include "utils.h"
  24. #include <stdio.h>
  25. #include <comcat.h>
  26. #include <windows.h>
  27. #include <shlwapi.h>
  28. #include <tchar.h>
  29. #include <guiddef.h>
  30. using namespace std;
  31. #define COMPANY_STR "VideoLAN"
  32. #define PROGRAM_STR "VLCPlugin"
  33. #define DESCRIPTION "VideoLAN VLC ActiveX Plugin"
  34. #define THREADING_MODEL "Apartment"
  35. #define MISC_STATUS     "131473"
  36. #define PROGID_STR COMPANY_STR"."PROGRAM_STR
  37. #define GUID_STRLEN 39
  38. /*
  39. ** MingW headers & libs do not declare those
  40. */
  41. static DEFINE_GUID(_CATID_InternetAware,       0x0DE86A58, 0x2BAA, 0x11CF, 0xA2, 0x29, 0x00,0xAA,0x00,0x3D,0x73,0x52);
  42. static DEFINE_GUID(_CATID_SafeForInitializing, 0x7DD95802, 0x9882, 0x11CF, 0x9F, 0xA9, 0x00,0xAA,0x00,0x6C,0x42,0xC4);
  43. static DEFINE_GUID(_CATID_SafeForScripting,    0x7DD95801, 0x9882, 0x11CF, 0x9F, 0xA9, 0x00,0xAA,0x00,0x6C,0x42,0xC4);
  44. static LONG i_class_ref= 0;
  45. static HINSTANCE h_instance= 0;
  46. HMODULE DllGetModule()
  47. {
  48.     return h_instance;
  49. };
  50. STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
  51. {
  52.     HRESULT hr = CLASS_E_CLASSNOTAVAILABLE;
  53.     *ppv = NULL;
  54.     if( (CLSID_VLCPlugin == rclsid )
  55.      || ( CLSID_VLCPlugin2 == rclsid) )
  56.     {
  57.         VLCPluginClass *plugin = new VLCPluginClass(&i_class_ref, h_instance, rclsid);
  58.         hr = plugin->QueryInterface(riid, ppv);
  59.         plugin->Release();
  60.     }
  61.     return hr;
  62. };
  63. STDAPI DllCanUnloadNow(VOID)
  64. {
  65.     return (0 == i_class_ref) ? S_OK: S_FALSE;
  66. };
  67. static inline HKEY keyCreate(HKEY parentKey, LPCTSTR keyName)
  68. {
  69.     HKEY childKey;
  70.     if( ERROR_SUCCESS == RegCreateKeyEx(parentKey, keyName, 0, NULL,
  71.                 REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &childKey, NULL) )
  72.     {
  73.         return childKey;
  74.     }
  75.     return NULL;
  76. };
  77. static inline HKEY keySet(HKEY hKey, LPCTSTR valueName, const void *s, size_t len, DWORD dwType = REG_SZ)
  78. {
  79.     if( NULL != hKey )
  80.     {
  81.         RegSetValueEx(hKey, valueName, 0, dwType,
  82.             (const BYTE*)s, len);
  83.     }
  84.     return hKey;
  85. };
  86. static inline HKEY keySetDef(HKEY hKey, const void *s, size_t len, DWORD dwType = REG_SZ)
  87. {
  88.     return keySet(hKey, NULL, s, len, dwType);
  89. };
  90. static inline HKEY keySetDef(HKEY hKey, LPCTSTR s)
  91. {
  92.     return keySetDef(hKey, s, sizeof(TCHAR)*(_tcslen(s)+1), REG_SZ);
  93. };
  94. static inline HKEY keyClose(HKEY hKey)
  95. {
  96.     if( NULL != hKey )
  97.     {
  98.         RegCloseKey(hKey);
  99.     }
  100.     return NULL;
  101. };
  102. static void UnregisterProgID(REFCLSID rclsid, unsigned int version)
  103. {
  104.     OLECHAR szCLSID[GUID_STRLEN];
  105.     StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
  106.     TCHAR progId[sizeof(PROGID_STR)+16];
  107.     _stprintf(progId, TEXT("%s.%u"), TEXT(PROGID_STR), version);
  108.     SHDeleteKey(HKEY_CLASSES_ROOT, progId);
  109.     HKEY hClsIDKey;
  110.     if( ERROR_SUCCESS == RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"), 0, KEY_WRITE, &hClsIDKey) )
  111.     {
  112.         SHDeleteKey(hClsIDKey, szCLSID);
  113.         RegCloseKey(hClsIDKey);
  114.     }
  115. };
  116. STDAPI DllUnregisterServer(VOID)
  117. {
  118.     // unregister type lib from the registry
  119.     UnRegisterTypeLib(LIBID_AXVLC, 1, 0, LOCALE_NEUTRAL, SYS_WIN32);
  120.     // remove component categories we supports
  121.     ICatRegister *pcr;
  122.     if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  123.             NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
  124.         CATID implCategories[] = {
  125.             CATID_Control,
  126.             CATID_PersistsToPropertyBag,
  127.             _CATID_InternetAware,
  128.             _CATID_SafeForInitializing,
  129.             _CATID_SafeForScripting,
  130.         };
  131.         pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin,
  132.                 sizeof(implCategories)/sizeof(CATID), implCategories);
  133.         pcr->UnRegisterClassImplCategories(CLSID_VLCPlugin2,
  134.                 sizeof(implCategories)/sizeof(CATID), implCategories);
  135.         pcr->Release();
  136.     }
  137.     SHDeleteKey(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
  138.     UnregisterProgID(CLSID_VLCPlugin, 2);
  139.     UnregisterProgID(CLSID_VLCPlugin2, 1);
  140.     return S_OK;
  141. };
  142. static HRESULT RegisterClassID(HKEY hParent, REFCLSID rclsid, unsigned int version, BOOL isDefault, LPCTSTR path, size_t pathLen)
  143. {
  144.     TCHAR progId[sizeof(PROGID_STR)+16];
  145.     _stprintf(progId, TEXT("%s.%u"), TEXT(PROGID_STR), version);
  146.     TCHAR description[sizeof(DESCRIPTION)+16];
  147.     _stprintf(description, TEXT("%s v%u"), TEXT(DESCRIPTION), version);
  148.     HKEY hClassKey;
  149.     {
  150.         OLECHAR szCLSID[GUID_STRLEN];
  151.         StringFromGUID2(rclsid, szCLSID, GUID_STRLEN);
  152.         HKEY hProgKey = keyCreate(HKEY_CLASSES_ROOT, progId);
  153.         if( NULL != hProgKey )
  154.         {
  155.             // default key value
  156.             keySetDef(hProgKey, description);
  157.             keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
  158.                 szCLSID,
  159.                 sizeof(szCLSID)));
  160.             //hSubKey = keyClose(keyCreate(hBaseKey, "Insertable"));
  161.  
  162.             RegCloseKey(hProgKey);
  163.         }
  164.         if( isDefault )
  165.         {
  166.             hProgKey = keyCreate(HKEY_CLASSES_ROOT, TEXT(PROGID_STR));
  167.             if( NULL != hProgKey )
  168.             {
  169.                 // default key value
  170.                 keySetDef(hProgKey, description);
  171.                 keyClose(keySetDef(keyCreate(hProgKey, TEXT("CLSID")),
  172.                     szCLSID,
  173.                     sizeof(szCLSID)));
  174.                 keyClose(keySetDef(keyCreate(hProgKey, TEXT("CurVer")),
  175.                     progId));
  176.             }
  177.         }
  178.         hClassKey = keyCreate(hParent, szCLSID);
  179.     }
  180.     if( NULL != hClassKey )
  181.     {
  182.         // default key value
  183.         keySetDef(hClassKey, description);
  184.         // Control key value
  185.         keyClose(keyCreate(hClassKey, TEXT("Control")));
  186.         // Insertable key value
  187.         //keyClose(keyCreate(hClassKey, TEXT("Insertable")));
  188.         // ToolboxBitmap32 key value
  189.         {
  190.             TCHAR iconPath[pathLen+3];
  191.             memcpy(iconPath, path, sizeof(TCHAR)*pathLen);
  192.             _tcscpy(iconPath+pathLen, TEXT(",1"));
  193.             keyClose(keySetDef(keyCreate(hClassKey,
  194.                 TEXT("ToolboxBitmap32")),
  195.                 iconPath, sizeof(iconPath)));
  196.         }
  197. #ifdef BUILD_LOCALSERVER
  198.         // LocalServer32 key value
  199.         keyClose(keySetDef(keyCreate(hClassKey,
  200.             TEXT("LocalServer32"), path, sizeof(TCHAR)*(pathLen+1))));
  201. #else
  202.         // InprocServer32 key value
  203.         {
  204.             HKEY hSubKey = keySetDef(keyCreate(hClassKey,
  205.                 TEXT("InprocServer32")),
  206.                 path, sizeof(TCHAR)*(pathLen+1));
  207.             keySet(hSubKey,
  208.                 TEXT("ThreadingModel"),
  209.                 TEXT(THREADING_MODEL), sizeof(TEXT(THREADING_MODEL)));
  210.             keyClose(hSubKey);
  211.         }
  212. #endif
  213.         // MiscStatus key value
  214.         keyClose(keySetDef(keyCreate(hClassKey,
  215.             TEXT("MiscStatus\1")),
  216.             TEXT(MISC_STATUS), sizeof(TEXT(MISC_STATUS))));
  217.         // Programmable key value
  218.         keyClose(keyCreate(hClassKey, TEXT("Programmable")));
  219.         // ProgID key value
  220.         keyClose(keySetDef(keyCreate(hClassKey,
  221.             TEXT("ProgID")),
  222.             progId));
  223.         // VersionIndependentProgID key value
  224.         keyClose(keySetDef(keyCreate(hClassKey,
  225.             TEXT("VersionIndependentProgID")),
  226.             TEXT(PROGID_STR), sizeof(TEXT(PROGID_STR))));
  227.         // Version key value
  228.         keyClose(keySetDef(keyCreate(hClassKey,
  229.             TEXT("Version")),
  230.             TEXT("1.0")));
  231.         // TypeLib key value
  232.         OLECHAR szLIBID[GUID_STRLEN];
  233.         StringFromGUID2(LIBID_AXVLC, szLIBID, GUID_STRLEN);
  234.         keyClose(keySetDef(keyCreate(hClassKey,
  235.                 TEXT("TypeLib")),
  236.                 szLIBID, sizeof(szLIBID)));
  237.  
  238.         RegCloseKey(hClassKey);
  239.     }
  240.     return S_OK;
  241. }
  242. STDAPI DllRegisterServer(VOID)
  243. {
  244.     DllUnregisterServer();
  245.     TCHAR DllPath[MAX_PATH];
  246.     DWORD DllPathLen=GetModuleFileName(h_instance, DllPath, MAX_PATH) ;
  247.     if( 0 == DllPathLen )
  248.         return E_UNEXPECTED;
  249.     HKEY hBaseKey;
  250.     if( ERROR_SUCCESS != RegOpenKeyEx(HKEY_CLASSES_ROOT, TEXT("CLSID"), 0, KEY_CREATE_SUB_KEY, &hBaseKey) )
  251.         return SELFREG_E_CLASS;
  252.     RegisterClassID(hBaseKey, CLSID_VLCPlugin, 1, FALSE, DllPath, DllPathLen);
  253.     RegisterClassID(hBaseKey, CLSID_VLCPlugin2, 2, TRUE, DllPath, DllPathLen);
  254.     RegCloseKey(hBaseKey);
  255.     // indicate which component categories we support
  256.     ICatRegister *pcr;
  257.     if( SUCCEEDED(CoCreateInstance(CLSID_StdComponentCategoriesMgr,
  258.             NULL, CLSCTX_INPROC_SERVER, IID_ICatRegister, (void**)&pcr)) ) {
  259.         CATID implCategories[] = {
  260.             CATID_Control,
  261.             CATID_PersistsToPropertyBag,
  262.             _CATID_InternetAware,
  263.             _CATID_SafeForInitializing,
  264.             _CATID_SafeForScripting,
  265.         };
  266.         pcr->RegisterClassImplCategories(CLSID_VLCPlugin,
  267.                 sizeof(implCategories)/sizeof(CATID), implCategories);
  268.         pcr->RegisterClassImplCategories(CLSID_VLCPlugin2,
  269.                 sizeof(implCategories)/sizeof(CATID), implCategories);
  270.         pcr->Release();
  271.     }
  272. #ifdef BUILD_LOCALSERVER
  273.     // replace .exe by .tlb
  274.     _tcscpy(DllPath+DllPathLen-4, TEXT(".tlb"));
  275. #endif
  276.     // register type lib into the registry
  277.     ITypeLib *typeLib;
  278.     HRESULT result = LoadTypeLibEx(DllPath, REGKIND_REGISTER, &typeLib);
  279.     if( SUCCEEDED(result) )
  280.         typeLib->Release();
  281.     return result;
  282. };
  283. #ifdef BUILD_LOCALSERVER
  284. /*
  285. ** easier to debug an application than a DLL on cygwin GDB :)
  286. */
  287. #include <iostream>
  288. STDAPI_(int) WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
  289. {
  290.     MSG msg;
  291.     if( FAILED(OleInitialize(NULL)) )
  292.     {
  293.         cerr << "cannot initialize OLE" << endl;
  294.         return 1;
  295.     }
  296.     h_instance = hInst;
  297.     if( FAILED(DllRegisterServer()) )
  298.     {
  299.         cerr << "cannot register Local Server" << endl;
  300.         return 1;
  301.     }
  302.     IUnknown *classProc = NULL;
  303.     if( FAILED(DllGetClassObject(CLSID_VLCPlugin, IID_IUnknown, (LPVOID *)&classProc)) )
  304.         return 0;
  305.  
  306.     DWORD dwRegisterClassObject;
  307.     DWORD dwRegisterClassObject2;
  308.     if( FAILED(CoRegisterClassObject(CLSID_VLCPlugin, classProc,
  309.         CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegisterClassObject)) )
  310.         return 0;
  311.     DWORD dwRegisterActiveObject;
  312.     if( FAILED(RegisterActiveObject(classProc, CLSID_VLCPlugin,
  313.                     ACTIVEOBJECT_WEAK, &dwRegisterActiveObject)) )
  314.         return 0;
  315.     if( FAILED(RegisterActiveObject(classProc, CLSID_VLCPlugin2,
  316.                     ACTIVEOBJECT_WEAK, &dwRegisterActiveObject2)) )
  317.         return 0;
  318.     classProc->Release();
  319.     /*
  320.     * Polling messages from event queue
  321.     */
  322.     while( S_FALSE == DllCanUnloadNow() )
  323.     {
  324.         while( PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) )
  325.         {
  326.             if( msg.message == WM_QUIT )
  327.                 break;  // break out PeekMessage loop
  328.             /*if(TranslateAccelerator(ghwndApp, ghAccel, &msg))
  329.                 continue;*/
  330.             TranslateMessage(&msg);
  331.             DispatchMessage(&msg);
  332.         }
  333.         if(msg.message == WM_QUIT)
  334.             break;  // break out main loop
  335.         WaitMessage();
  336.     }
  337.     if( SUCCEEDED(RevokeActiveObject(dwRegisterActiveObject, NULL)) )
  338.         CoRevokeClassObject(dwRegisterClassObject);
  339.     if( SUCCEEDED(RevokeActiveObject(dwRegisterActiveObject2, NULL)) )
  340.         CoRevokeClassObject(dwRegisterClassObject2);
  341.     // Reached on WM_QUIT message
  342.     OleUninitialize();
  343.     return ((int) msg.wParam);
  344. };
  345. #else
  346. STDAPI_(BOOL) DllMain(HANDLE hModule, DWORD fdwReason, LPVOID lpReserved )
  347. {
  348.     switch( fdwReason )
  349.     {
  350.         case DLL_PROCESS_ATTACH:
  351.             h_instance = (HINSTANCE)hModule;
  352.             break;
  353.         default:
  354.             break;
  355.     }
  356.     return TRUE;
  357. };
  358. #endif