標籤:blog multi one lin 傳回值 ror 不同 when active
C++ 開發OCX 的方法和注意事項
前言
ActiveX控制項是一種實現了一系列特定介面而使其在使用和外觀上更象一個控制項的COM組件。ActiveX控制項這種技術涉及到了幾乎所有的COM和OLE的技術精華,如可連結化物件、統一資料轉送、OLE文檔、屬性頁面、永久儲存以及OLE自動化等。
ActiveX控制項作為基本的介面單元,必須擁有自己的屬性和方法以適合不同特點的程式和向包容器程式提供功能服務,其屬性和方法均由自動化服務的 IDispatch介面來支援。除了屬性和方法外,ActiveX控制項還具有區別於自動化服務的一種特性--事件。事件指的是從控制項發送給其包容程式的一 種通知。與視窗控制項通過發送訊息通知其擁有者類似,ActiveX控制項是通過觸發事件來通知其包容器的。事件的觸發通常是通過控制項包容器提供的 IDispatch介面來調用Automation 物件的方法來實現的。在設計ActiveX控制項時就應當考慮控制項可能會發生哪些事件以及包容器程式將會對其中的哪些事 件感興趣並將這些事件包含進來。與自動化服務不同,ActiveX控制項的方法、屬性和事件均有自訂(custom)和庫存(stock)兩種不同的類 型。自訂的方法和屬性也就是是普通的自動化方法和屬性,自訂事件則是自己選取名字和Dispatch ID的事件。而所謂的庫存方法、屬性和事件則是使用了ActiveX控制項規定了名字和Dispatch ID的"標準"方法、屬性和事件。
ActiveX控制項可以使COM組件從外觀和使用上能與普通的視窗控制項一樣,而且還提供了類似於設定Windows標準控制項屬性的屬性頁面,使其能夠在包 容器程式的設計階段對ActiveX控制項的屬性進行可視化設定。ActiveX控制項提供的這些功能使得對其的使用將是非常方便的。本文下面即以MFC為工 具對ActiveX控制項的開發進行介紹。
(上面的簡介純屬百度,看看瞭解瞭解就行不重要,但是我們需要知道的是,這種控制項能做什麼,這才是我們關心的。)
OCX 控制項通過註冊,可以像普通控制項一樣被拖拉使用,相當於我們自訂了一個控制項。但是不同的是,該控制項C++ 可以調用,C#可以調用,甚至可以用於WEB頁面。後面我們一個一個編寫測試案例。
開發OCX 控制項
我們使用vs2010 開發OCX控制項,建立一個 MFC ACTIVEX CONTROL 解決方案。然
後下一步,下一步走就好。
這時候我們嘗試編譯一下該工程,可能會有錯誤
錯誤:
error MSB8011: Failed to register output. Please try enabling Per-user Redirection or register the component from a command prompt with elevated permissions. C:\Program Files\MSBuild\Microsoft.Cpp\v4.0\Microsoft.CppCommon.targets 744
解決方案:
This error happens when Visual Studio (2010) can‘t find the dependent dll files that are required by the program for creating the OCX. The OCX is created in the Debug directory of the project (for Debug builds), and specifying the "Working directory" (in project settings) as the folder where the dependent DLL‘s are, won‘t help Visual Studio in locating the DLL‘s.
Now the catch is (this is what makes it a headache to solve the problem) that sometimes, without Visual Studio being able to create the OCX, you won‘t be able to run Dependency Walker on the OCX. So you‘ll never figure out that the missing DLL‘s are the problem.
If you know which DLL‘s need to be placed in the Debug folder, just place them there, and the error will disappear. If you don‘t know, and if your OCX didn‘t get created, then go to project settings > Linker > General > Register Output and set the value to "No". This will create your OCX for you, but won‘t go through the process of registering it, which is when it needs the dependent DLL‘s.
Once you double-click the OCX and Dependency Walker shows you the missing DLL‘s with yellow circle icons, just place those DLL‘s in the same folder as the OCX, and your program will work fine. It‘s that simple.
(參考 https://stackoverflow.com/questions/10477117/solving-error-msb8011-failed-to-register-output)
錯誤:
Please use the /MD switch for _AFXDLL builds
解決方案:
工程->設定->C/C++,在分類中選中“Code Generation”,然後在Use run-time library下選中Multithreaded DLL或Debug Multithreaded DLL
(參考http://blog.sina.com.cn/s/blog_7d1dc9de01012yge.html)
然後我們給控制項添加一個方法叫Hello1
傳回值設定字串BSTR 一個參數 類型 BSTR 參數名 str
然後 編碼Hello1方法的實現,該方法在mfcActiveCtrl.cpp中。該方法原樣返回參數值。
第二步:我們給控制項添加一個事件DbClick
好了,這個時候,我們的這個OCX 控制項已經有了一個Hello1的方法,和一個DbClick事件。
其他的就和MFC 的操作差不多了。這裡只是入門方法。
C++ 開發OCX 的方法和注意事項