天天看點

MFC Activex OCX Javascript 互相通路問題,線程回調javascript

//比較好的教程 

ocx 在 win7 系統會出現注冊需要管理者權限的問題,這時候需要用管理者身份運作 cmd,然後運作 regsvr32注冊。

很麻煩

嘗試使用 nsis 做成安裝包, 采用 regdll 注冊 ocx, 成功。

ocx和外面的程式互動主要通過提供方法屬性 + 事件

方法屬性可以提供給js調用,

事件可以給js 通過下面的方式進行回調注入

<object id="xxx"></object>

<script language="JavaScript" for="xx" Event="eventFunction(x)"> 

alert(x);

</script> 

或者

document.getElementByIdx_x(xx).attachEvent("eventFunction",function(x,y){

});

這兩種功能都可以在類視圖裡面選擇  XXXCtrl,右鍵選擇 add ,會出現 方法屬性事件

按照wizard進行添加就好。

主要記錄一下如果ocx建立了線程,想通過事件回調js的話,會出現問題。

這時候解決方法就是通過 PostMessage(WM_THREADFIREEVENT,(WPARAM)NULL,(LPARAM)NULL); 下面的看看應該懂了

//-------------------------

SAMPLE:   Firing   Events   From   a   Second   Thread   

  ---------------------------------------------------------------------   

  The   information   in   this   article   applies   to:   

    -   Microsoft   Visual   C++,   32-bit   Edition   versions   4.0,   4.1,   4.2   

  SUMMARY   

  =======   

  MFC   based   ActiveX   controls   typically   fire   their   events   from   the   same   thread   

  that   implements   the   sink   interface   of   the   container   that   the   events   are     

  being   fired   to.   

  Sometimes,   it   is   desirable   to   start   a   second   thread   in   an   ActiveX   control   

  which   will   fire   events   to   the   container.   Since   MFC   ActiveX   controls   

  use   the   Apartment   threading   model,   special   consideration   must   be   taken   

  into   account   when   firing   events   from   a   secondary   thread.   

  MORE   INFORMATION   

  ================   

  An   MFC   based   ActiveX   control   supports   events   by   implementing   the   

  IConnectionPointContainer   and   IConnectionPoint   interfaces,   as   well   as   

  supplying   information   about   it's   event   interface   in   it's   type   information.   

  When   an   MFC   based   ActiveX   control   is   embedded   in   a   container   that   supports   

  events,   that   container   will   dynamically   construct   a   sink   interface   that   has   

  all   of   the   methods   specified   in   the   control's   type   information   for   it's   

  event   interface.   Once   the   container   constructs   it's   sink   interface,   it   

  will   pass   a   pointer   to   that   interface   to   the   ActiveX   control.   The   ActiveX   

  control   will   use   it's   implementation   of   IConnectionPoint   to   communicate   

  through   the   now   hooked   up   sink   interface   that   was   constructed   by   the   

  container.   This   sample   will   demonstrate   how   to   call   methods   of   the   

  container's   sink   interface   from   a   second   thread.   

  The   two   most   important   things   to   consider   when   starting   a   new   thread   to   

  fire   events   from   in   an   ActiveX   control   are:   

  1.   MFC   based   ActiveX   controls   are   in-process   objects   (implemented   in   a     

        DLL).     

  2.   MFC   based   ActiveX   controls   use   the   Apartment   threading   model.   

  The   Apartment   threading   model   specifies   that   all   threads   that   want   to   use   

  OLE   services   must   initialize   OLE   in   their   thread   prior   to   using   OLE   

  services.   Also,   if   a   thread   wants   to   use   a   pointer   to   an   interface   that   is   

  either   implemented   by   a   different   thread   of   the   same   process   or   has   been   

  previously   marshaled   to   a   different   thread   of   the   same   process,   that   

  pointer   must   be   marshaled   to   the   requesting   thread.   In   the   Apartment   

  threading   model,   hidden   windows   are   created   to   synchronize   requests   from   

  other   threads   to   the   thread   being   called.   This   means   that   all   

  communication   between   threads   will   be   done   by   using   hidden   windows   and   

  Windows   messages   in   the   Apartment   model.   

  There   are   two   possible   ways   to   fire   events   from   a   second   thread   in   an   

  ActiveX   control   (or   any   other   in-proc   server   that   implements   connection   

  points)   under   the   Apartment   threading   model.   The   first   is   to   make   the   

  interface   call   from   the   second   thread   by   calling   the   event   sink's   method   

  from   the   second 

繼續閱讀