ACDUAL 範例:將雙重介面加入至 Automation 應用程式

更新:2007 年 11 月

ACDual 會示範如何將雙重介面支援加入至 MFC 架構的 Automation (先前稱為 OLE Automation) 應用程式。這個方案包含下列專案:

  • ACDualDriv,含有一個 Automation 用戶端 AUTODRIV 專案的版本,可以讓您選擇是要使用分派介面 (Dispatch Interface) 或使用 Vtable 繫結來控制伺服器應用程式。

  • ACDual,含有一個 Automation 伺服器 AUTOCLIK 專案的實際版本,支援雙重介面 (Dual Interface)。

安全性注意事項:

這個程式碼範例僅供概念說明之用,同時因為範例中的程式碼撰寫並沒有周全的顧及所有安全性問題,所以本範例不適用於實際執行的應用程式或網站中。如果使用者擅自將範例程式碼用於其他意圖而導致連帶事件或重大損害時,Microsoft 將不承擔任何責任。

若要取得範例和指示以便進行安裝:

  • 按一下 Visual Studio [說明] 功能表上的 [範例]。

    如需詳細資訊,請參閱尋找範例檔案

  • 最新版的完整範例清單可從 Visual Studio 2008 範例網頁 (英文) 取得。

  • 您也可以將範例置於電腦硬碟中。根據預設,範例和讀我檔案都會複製到 \Program Files\Visual Studio 9.0\Samples\ 下的資料夾中。對於所有的 Visual Studio Express 版,所有範例都可在線上找到。

建置範例

若要建置 ACDual 範例

  1. 開啟位於 acdual 目錄的 acdual.sln 方案。

  2. 在 [建置] 功能表上,按一下 [建置]。

  3. 將 ACDual 當做獨立的應用程式執行一次,使其將自己登錄到系統中。

執行範例

在登錄 ACDual 應用程式之後,您就可以開始執行 ACDualDriv 應用程式。建置來自 IDE 的方案將會在執行 ACDualDriv 之前自動地執行 ACDualDriv 登錄。ACDualDriv 會啟動 ACDual 應用程式並建立一個 Document 物件,您可以接著透過 ACDualDriv 使用者介面使用 Automation 來管理該物件。它也包含另一個核取方塊,可以讓您選擇是否要使用 VTBL 繫結來與 ACDual Document 物件進行通訊。

ACDual 伺服程式會將 AUTOCLIK 當做啟動點使用。產生的新全域唯一識別項 (GUID) 可以防止與原始的 AUTOCLIK 範例產生混淆,而且有些資源字串會變更以便釐清是否正在執行 ACDual 伺服程式。其他所有對來源所做的變更都會以標記註解區塊,就像:

      // DUAL_SUPPORT_START
      ... modified code goes here
      // DUAL_SUPPORT_END

如需雙重介面、物件描述語言 (ODL) 指令碼和 Automation 錯誤介面的詳細資訊,請參閱技術提示 65

雙重介面

雙重介面允許您實作 IDispatch 介面或 VTBL 介面。建議您對所有公開的 Automation 物件使用雙重介面。如需實作雙重介面時需考量的問題討論,請參閱技術提示 65 中的說明內容,其中包括:

  • 實作 CCmdTarget 架構類別的雙重介面支援

  • 傳遞雙重介面指標

  • 啟用 typesafe 繫結,包括:

    • 登錄應用程式的程式庫類型

    • 修改專案建置設定,以配合型別程式庫的變更

    • 指定型別程式庫中正確的物件類別名稱

  • 處理例外狀況和 Automation 錯誤介面

如需詳細資訊,請參閱下列 ActiveX 主題:Overview of Automation雙重介面Automation Type Description Interfacesdual 屬性的 ODL 參考項目。

注意事項:

部分範例 (包括本範例) 尚未經過改寫,無法反映出目前在 Visual C++ 精靈、程式庫以及編譯器方面的變更,不過還是可以示範如何完成您要進行的工作。

關鍵字

ACDual 範例將為您示範下列關鍵字:

AfxMessageBox; AfxOleInit; AfxOleLockApp; AfxOleUnlockApp; CCmdTarget::EnableAutomation; CCmdTarget::FromIDispatch; CCmdTarget::GetIDispatch; CCmdTarget::OnFinalRelease; CControlBar::EnableDocking; CControlBar::GetBarStyle; CControlBar::SetBarStyle; CDialog::DoModal; CDocument::GetFirstViewPosition; CDocument::GetNextView; CDocument::OnNewDocument; CDocument::SetModifiedFlag; CDocument::UpdateAllViews; CFrameWnd::ActivateFrame; CFrameWnd::DockControlBar; CFrameWnd::EnableDocking; CFrameWnd::LoadFrame; CObject::AssertValid; CObject::Dump; CObject::IsKindOf; CObject::Serialize; COleTemplateServer::ConnectTemplate; COleTemplateServer::UpdateRegistry; CStatusBar::Create; CStatusBar::SetIndicators; CToolBar::Create; CView::DoPreparePrinting; CView::GetDocument; CView::OnBeginPrinting; CView::OnDraw; CView::OnEndPrinting; CView::OnPreparePrinting; CWinApp::AddDocTemplate; CWinApp::EnableShellOpen; CWinApp::InitInstance; CWinApp::LoadStdProfileSettings; CWinApp::RegisterShellFileTypes; CWnd::DoDataExchange; CWnd::GetParentFrame; CWnd::OnCreate; CWnd::OnLButtonDown; CWnd::PreCreateWindow; CWnd::ShowWindow; CWnd::UpdateWindow; DragAcceptFiles; ShowWindow; TextOut

ACDualDriv 範例將為您示範下列關鍵字:

AfxGetApp; AfxMessageBox; AfxOleInit; CDC::DrawIcon; CDC::GetSafeHdc; CDialog::DoModal; CDialog::EndDialog; COleDispatchDriver::AttachDispatch; COleDispatchDriver::GetProperty; COleDispatchDriver::InvokeHelper; COleDispatchDriver::SetProperty; CRect::Height; CRect::Width; CWinApp::InitInstance; CWinApp::LoadStdProfileSettings; CWnd::DoDataExchange; CWnd::GetClientRect; CWnd::IsIconic; CWnd::OnClose; CWnd::OnCreate; CWnd::OnPaint; CWnd::OnQueryDragIcon; CWnd::SendMessage; CWnd::ShowWindow; CWnd::UpdateData; GetSystemMetrics; LoadIcon; ShowWindow

請參閱

其他資源

MFC 範例