ActiveXObject in Firefox or Chrome (not IE!)

ActiveX is only supported by IE - the other browsers use a plugin architecture called NPAPI. html

 However, there's a cross-browser plugin framework called Firebreath that you might find useful. git

http://stackoverflow.com/questions/7022568/activexobject-in-firefox-or-chrome-not-ie github

可是chrome和firefox均宣佈之後再也不支持NPAPI,替代方案是Native Client:https://developer.chrome.com/native-client web

項目地址:https://code.google.com/p/nativeclient/可是NACL目前還不支持IEchrome

在Windows下搭建NaCl開發平臺

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// 瀏覽器

Firebreath解決方法:https://groups.google.com/forum/#!topic/firebreath-dev/QzKD_56U07A app

I finally found a solution to make it working ! ide

I tried the axWrapper project but I didn't get to make it working.

I firstly generated and included the .h file by importing the ActiveX type Lib (.tlb) and compiling it in VS (I also got a .c, a .tli and a .tlh file)

I put this in a function:
I have to call this function in a thread be avoid to stuck the web browser (Tested on IE, Firefox, Chrome, Opéra and Safari)

DWORD WINAPI myActiveXInAThread( LPVOID lpParam ) 
{
     // Loading the DLL in prog
     HINSTANCE hDLL = LoadLibrary("C:\\PathTo\\myDll.dll");
     // Define the pointer type for the function
     typedef HRESULT (WINAPI *PAttachControl)(IUnknown*, HWND,IUnknown**);
     // Get the function address
     PAttachControl ISVR = (PAttachControl) GetProcAddress(hDLL, "MyProcName");

     // Init the COM library
     CoInitialize(0);

     // Pointer to the interface
     interfaceName *ptr;

     // Create the instance of the Object and the interface
     CoCreateInstance(CLSID_ofTheObject,0,CLSCTX_ALL,IID_ofTheInterface,(void**)&ptr);

     // Calling a method from dll
     ptr->aFunctionFromMyDll();

     // Loop Message
     MSG Msg;
     while( GetMessage(&Msg, NULL, 0, 0))
     {
          TranslateMessage( &Msg );
          DispatchMessage( &Msg );
     }

     // Release the interface
     ptr->Release();

     // Closing the COM library
     CoUninitialize();

     // Closing the DLL
     FreeLibrary(hDLL);
}

Then I call this next function from JavaScript to launch the ActiveX

std::string MyFbPluginAPI::launchActiveX(){

DWORD   dwThreadIdArray;
HANDLE  hThreadArray;

  hThreadArray = CreateThread( 
            NULL,                        // default security attributes
            0,                              // use default stack size  
            myActiveXInAThread,  // thread function name
            NULL,                        // argument to thread function 
            0,                              // use default creation flags 
            &dwThreadIdArray);     // returns the thread identifier 

return "ActiveX Launched !;
}

youtube視頻:https://www.youtube.com/watch?v=am6wA7MSztc 函數

Firebreath瀏覽器開發指南:http://itindex.net/detail/47019-firebreath-%E6%B5%8F%E8%A7%88%E5%99%A8-%E6%8F%92%E4%BB%B6 工具

                                      http://blog.csdn.net/z6482/article/details/7486921

Firebreath activex DEMO:https://github.com/firebreath/FBAXExample


How is an ActiveX wrapper created with FireBreath?



NPAPI開發詳解,Windows版  http://mozilla.com.cn/thread-21666-1-1.html


本文經過多圖組合,詳細引導初學者開發NPAPI的瀏覽器插件。
如需測試開發完成的插件請參考http://mozilla.com.cn/kb/dev/A.88/
1. 準備工做
開發工具
本例使用的是visual studio 2008 英文版,下圖是關於信息
Windows SDK
本例使用Windows7操做系統 這裏下載SDK
NPAPISDK
本例使用的是Firefox4.0.1提供的SDK。
首先,從這裏下載mozilla源碼。而後,解壓firefox-4.0.1.source.tar.bz2文件。
將 \firefox-4.0.1.source\mozilla-2.0\modules\plugin 目錄解壓縮出來,裏面有咱們開發NPAPI插件所需的全部資源。
爲了方便你們使用,--這裏--提供plugin.rar的下載。
本例將plugin目標解壓到D:\code\下(後面統一使用絕對路徑,以免異意)
2. 建立Plugin
本着「有圖有真相」的原則,下面將連續多圖並配文字一步步建立、調試Plugin。圖中畫紅圈的表明須要填寫或者須要選擇的地方。
建立項目

新建項目 
Name項必定要以 np開頭,爲了未來適應不一樣操做系統,最好全小寫,不要太長,儘可能控制在8字符內。
本例定義爲 npdemo
Location項定義到 plugin\sdk\samples以便項目屬性中用相對路徑引用NPAPI的SDK
本例定義爲 d:\code\plugin\sdk\samples
嚮導
選擇 Application typeDLL
選擇 Empty project

添加文件
首先,添加NPAPI SDK中的Common文件
一共3個文件
而後,添加def文件
命名最好與項目一致
編輯npdemo.def爲
  1. LIBRARY "npdemo"
  2.      
  3. EXPORTS
  4.     NP_GetEntryPoints   @1
  5.     NP_Initialize       @2
  6.     NP_Shutdown         @3
複製代碼
複製代碼
如今,添加資源
選擇 Version
自動生成了 resource.hnpdemo.rc。因爲要在版本信息中加項,因此手工 npdemo.rc
選擇「Y」
在圖中的BLOCK中添加。注意! BLOCK 必定要是" 040904e4"
  1. VALUE "MIMEType", "application/demo-plugin"
複製代碼
這裏順便說一下,MIMEType是plugin的惟一標示,須要本身定義
一般的格式是"application/「+ [plugin name]
本例中定義爲"application/demo-plugin"
下圖是rc文件數據項與plugin數據項(about:plugins 中)的對應關係 
下面添加最關鍵的部分:Plugin實現類

類名能夠隨便起,本例命名爲CPlugin
可是必定要繼承自nsPluginInstanceBace
修改Plugin.h
  1. #pragma once
  2. #include "pluginbase.h"
  3.      
  4. class CPlugin : public nsPluginInstanceBase
  5. {
  6. private:
  7.   NPP m_pNPInstance;
  8.   NPBool m_bInitialized;
  9. public:
  10.   CPlugin(NPP pNPInstance);
  11.   ~CPlugin();
  12.      
  13.   NPBool init(NPWindow* pNPWindow)  {  m_bInitialized = TRUE;  return TRUE;}
  14.   void shut()  {  m_bInitialized = FALSE;  }
  15.   NPBool isInitialized()  {  return m_bInitialized;  }
  16. };
複製代碼
複製代碼
修改Plugin.cpp
其中實現了4個全局函數
  1. #include "plugin.h"
  2.      
  3.      
  4. ////// functions /////////
  5. NPError NS_PluginInitialize()
  6. {
  7.   return NPERR_NO_ERROR;
  8. }
  9.      
  10. void NS_PluginShutdown()
  11. {
  12. }
  13.      
  14. nsPluginInstanceBase * NS_NewPluginInstance(nsPluginCreateData * aCreateDataStruct)
  15. {
  16.   if(!aCreateDataStruct)
  17.     return NULL;
  18.      
  19.   CPlugin * plugin = new CPlugin(aCreateDataStruct->instance);
  20.   return plugin;
  21. }
  22.      
  23. void NS_DestroyPluginInstance(nsPluginInstanceBase * aPlugin)
  24. {
  25.   if(aPlugin)
  26.     delete (CPlugin *)aPlugin;
  27. }
  28. ////// CPlugin /////////
  29. CPlugin::CPlugin(NPP pNPInstance) : nsPluginInstanceBase(),
  30.   m_pNPInstance(pNPInstance),
  31.   m_bInitialized(FALSE)
  32. {
  33. }
  34.      
  35. CPlugin::~CPlugin()
  36. {
  37. }
複製代碼
複製代碼
修改項目屬性
打開項目屬性 
修改字符集設置爲「 Use Multi-Byte Character Set
添加搜索目錄 「 ....\include」和「 ........\base\public
添加預編譯宏  X86
如今能夠編譯了!


三、註冊、測試
本例編譯後,在D:\code\plugin\sdk\samples\npdemo\Debug生成npdemo.dll
打開註冊表,在 HKEY_CURRENT_USER\SOFTWARE\MozillaPlugins下新建子項 @mozilla.com.cn/demo
並新建字符串數據「 Path」設值爲 D:\code\plugin\sdk\samples\npdemo\Debug\npdemo.dll

打開火狐瀏覽器 在地址欄輸入「about:plugins」 若是在plugin列表中有本例的npdemo.dll及說明咱們的plugin示例已經成功完成


簡單的測試頁面:
  1. <HTML>
  2.     <HEAD>
  3.     </HEAD>
  4.     <BODY>
  5.         <embed type="application/demo-plugin">
  6.     </BODY>
  7. </HTML>
複製代碼
特別注意
若是在實際部署中使用安裝文件安裝plugin,並用註冊表的方式註冊。那麼就 不須要重啓火狐 ,只要在頁面中執行  navigator.plugins.refresh(false);  而後刷新頁面便可使用剛安裝的plugin
相關文章
相關標籤/搜索