1 / 56

DCOM(Distributed COM)

DCOM(Distributed COM). Presenter: 資管研一 R89725009 呂江麟 資管研一 R89725037 劉英和. 大綱. Component Object Model Distributed COM DCOM Example With MFC SAP: DCOM Component Connector. Component Object Model. COM – 軟體之間提供服務的一種標準方式 COM 是一組規格: 描述 COM 元件建立時所必須遵守的準則

anthea
Télécharger la présentation

DCOM(Distributed COM)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DCOM(Distributed COM) Presenter: 資管研一 R89725009 呂江麟 資管研一 R89725037 劉英和

  2. 大綱 • Component Object Model • Distributed COM • DCOM Example With MFC • SAP: DCOM Component Connector

  3. Component Object Model • COM –軟體之間提供服務的一種標準方式 • COM是一組規格: 描述COM元件建立時所必須遵守的準則 • COM是一組服務: COM函式庫提供支援此規格的服務或API • 物件導向:識別碼、狀態、行為 • 位置通透性 • 相同行程 (in-process server) • 不同行程 (out-of-process server, local server) • 不同機器 (remote server)

  4. COM 的基本理念 • 組裝軟體 : Componentware • COM 的承諾 • 以機器碼而非原始碼的重複使用方式 • 語言獨立性 • 位置通透性

  5. COM介面 • 定義了其他軟體或元件可以使用的公用功能 • 一個COM介面必須滿足下列要求 • 唯一的識別碼:IID(Interface ID) • 衍生自IUnknown介面 • 一旦出版之後,便不允許更改 • 介面並不是一個類別 • 介面不是一個物件

  6. IUnknown介面 • QueryInterface : 查詢物件是否有支援特定的介面 • AddRef與Release 進行物件生命週期的管理,當參考計數(Reference Count)的值為0時,物件就知道沒有任何一個客戶端使用到自己,便能夠將自己毀滅,

  7. COM元件的型態 • 視程式碼模組的結構以及元件與使用元件的 用戶端處理程序之間的關係而定,元件可分 為以下三種型態: -行程內(In Process) -本機(Local) -遠端(Remote)

  8. 行程內(In-Process) 客戶端與伺服器端存在於相同的行程中,執行在相同的電腦上

  9. 行程內(In-Process) • In-Process Server:DLL型式 • 執行時,將DLL載入記憶體區塊 • 容錯能力較差 • 執行於同一個Process,沒有安全機制

  10. 本機(Local) 本機伺服器和用戶端於相同的一部機器上,但是在另一個獨立的處理程序中執行

  11. 本機(Local) • 通常實作成 EXE 型式 • 不同行程,彼此不會互相干擾 • Proxy-> Marshaling->IPC (inter-process communication)->Un-marshaling->Stub • 執行效率較 In-Process Server 差

  12. 遠端(Remote) 客戶端與伺服器透過網路來溝通,執行於不同的電腦

  13. 遠端(Remote) • Client端與 Server 端必須包含相對應的proxy/stub 進行 marshaling • RPC(Remote Procedure Call) 溝通 • Server端可為 DLL 型式或 EXE 型式 (若為 DLL,則需要 Server 的代理行程, 通常為 dllhost.exe)

  14. COM系統函式庫(COM Library) • COM系統層級的程式碼:COM的建置是包 含在COM系統函式庫中的。這項建置透過一個DLL提供,這個DLL包含下列COM的規格中所提到的要件: -基本的API函數 -建置找尋服務 -透明化的遠端程序呼叫 -控制記憶體的配置

  15. 介面定義語言(Interface Definition Language) • 不需要任何的物件實作碼 • MIDL(Microsoft IDL編譯器) • Proxy/StubDLL: Proxy和stub會被包在一個proxy/stub DLL中,這個DLL包含客戶端proxy與伺服器stub的程式碼 。當在不同的電腦上執行客戶端與伺服器時,必須保證proxy/stub DLL同時安裝及登錄在這兩台電腦 。

  16. COM物件的重複使用機制 • 收納(containment)

  17. COM物件的重複使用機制 • 聚集(aggregation)

  18. 使用Visual Basic呼叫 Excel Dim obj Set obj = CreateObject(“Excel.Application”) obj.visible = true obj.Workbooks.Add obj.Range("B3").Select obj.ActiveCell.FormulaR1C1 = "12" obj.Range("B4").Select obj.ActiveCell.FormulaR1C1 = "34“ …

  19. COM 如何找出 Excel 的位置? • 使用 RegEdit.exe 登錄編輯器 • HKEY_CLASSES_ROOT CLSID {00024500-0000-000…} LocalServer32 = excel.exe Excel.Application CLSID = {00024500-0000-000…} • Excel.Application : ProgID

  20. COM Runtime 元件和應用程式 COM Runtime 核心服務(Monikers, Storage, Data Transfer,...) [D]COM Registry Pluggable Security(SSPI) MS-RPC Pluggable Transports NTLM DCE Kerberos ETC... TCP UDP IPX Tunneled TCP ETC...

  21. Security Lifecycle Management Type Information (Interface Repository) Monikers (Naming) Automation (Dynamic Invocation) Data Transfer Component Categories Registry Core COM Services

  22. Proxy Component COM Runtime ArchitectureFlexible and extensible Pluggable Transports Client Machine Server Machine TCP, UDP COMClient SPX,IPX COM Runtime COM Runtime Net BUI HTTP “Falcon” Other Component

  23. Proxy Component COM Runtime ArchitectureFlexible and extensible Pluggable Security Providers Client Machine Server Machine NTLM COMClient SSL, Certs. COM Runtime COM Runtime NT Kerberos DCE Other Component

  24. COM 程式設計模型 用戶端行程 用戶端程式 OLE32.DLL Service Control manager (RPCSS.EXE)

  25. 元件行程 Object OLE32.DLL COM 程式設計模型 用戶端行程 用戶端程式 OLE32.DLL Service Control manager (RPCSS.EXE)

  26. Object stub proxy COM 程式設計模型 元件行程 用戶端行程 用戶端程式 OLE32.DLL OLE32.DLL Service Control manager (RPCSS.EXE)

  27. Object stub proxy COM 程式設計模型 元件行程 用戶端行程 用戶端程式 OLE32.DLL OLE32.DLL Service Control manager (RPCSS.EXE)

  28. Object stub proxy COM 程式設計模型 元件行程 用戶端行程 用戶端程式 COM 的攔截機制 OLE32.DLL OLE32.DLL Service Control manager (RPCSS.EXE)

  29. Distributed COM • Just COM “with a longer wire” • All COM components communicate in the same way • on the same machine • In-process, Out-of-process • across a Local Area Network • across a Wide Area Network • across the Internet • Same tools, knowledge, code

  30. Distributed COM • Wire Protocol • Based on DCE RPC Specification • Interoperable with OSF DCE RPC implementations • We call it “ORPC” • 指定元件的伺服器的位置 • CreateObject(ProgID, “Jacky”) • DCOMCNFG.EXE

  31. COM如何找到啟動遠端伺服器的資訊? • 又是 RegEdit.exe! • HKEY_CLASSES_ROOT AppID {00024500-0000-000…} RemoteServerName = jacky

  32. proxy stub Object Registry Registry {CLSID} {AppID} LocalServer32 RemoteServerName DCOM 的物件啟動 元件行程 用戶端行程 用戶端程式 OLE32.DLL OLE32.DLL SCM SCM

  33. COM物件的安全模型 設定方式: • 宣告式安全設定:於元件外部的登錄 (registry)中設定 • 程式控制安全設定:由程式設計者加進元件 宣告式安全設定: • 預設安全(default security):指定所有在本機上執行的元件之安全設定值 • 元件安全(component security):提供特別的安全性給一個特定的元件

  34. To implement a class using MFC’s interface maps follows these steps: Implementation With MFC(1) 1.Use Derive a class either directly or indirectly from CCmdTarget.   2.Use the DECLARE_INTERFACE_MAP function in the derived class definition. 3.For each interface you wish to support, use the BEGIN_INTERFACE_PART、 END_INTERFACE_PART macros in the class definition.

  35. Implementation With MFC(2) 4.In the implementation file, use the BEGIN_INTERFACE_MAP and END_INTERFACE_MAP macros to define the class's interface map. 5.For each IID supported, use the INTERFACE_PART macro between the BEGIN_INTERFACE_MAP and END_INTERFACE_MAP macros to map that IID to a specific "part" of your class.

  36. Implementation With MFC(3) 6.Implement each of the nested classes that represent the interfaces you support. 7.Use the METHOD_PROLOGUE macro to access the parent, CcmdTarget-derived object. 8.AddRef, Release, and QueryInterface can delegate to the CcmdTarget implementation of these functions: ExternalAddRef, ExternalRelease, and ExternalQueryInterface.

  37. Macro of MFC Implementation(1) BEGIN_INTERFACE_PART(localClass, iface); END_INTERFACE_PART(localClass) localClass:實作 interface 的 class iface:class 實作的 interface 用來產生巢狀類別,它會把X加到第一個參數名稱前,以組成類別名稱。

  38. Macro of MFC Implementation(2) DECLARE_INTERFACE_MAP 產生一個表格的宣告,這個表格內含所有類別的介面識別碼 BEGIN_INTERFACE_MAP(theClass, baseClass) END_INTERFACE_MAP theClass :要建立 interface 的 class baseClass:該 class所繼承的類別 在implement時,實際定義 interface map。

  39. Macro of MFC Implementation(3) STDMETHOD_ 在宣告敘述(*.h file)中,宣告method。 STDMETHODIMP_ 在implement中(*.cpp),定義method。 此兩巨集將傳回值指定為第一個參數。

  40. Macro of MFC Implementation(4) STDMETHOD 在宣告敘述(*.h file)中,宣告method。 STDMETHODIMP 在implement中(*.cpp),定義method。 此兩巨集採用型態為HRESULT的傳回值。 METHOD_PROLOGUE 傳回父類別的指標,且存於pThis這個指標中。

  41. CLASS FACTORY(1) A COM object that implements the IClassFactory interface and that creates one or more instances of an object。

  42. CLASS FACTORY(2) IClassFactory介面: 衍生自IUnknow介面,主要member function 為: virtual BOOL CreateInstance(int& nlid, void** ppvObj) = 0

  43. CLASS FACTORY(3) Why Class Factory ? 因為不能直接call target class的constructor,則object的設置和啟動是透過Class Factory 來作。

  44. 介面宣告 Interface.h struct Isum : public IUnknown { STDMETHOD_(void, Sum) (int x,int y, int *revtal) = 0; };

  45. 由CcmdTarget類別derived下來的 元件類別 test.h class CTest : public CCmdTarget {  protected:  BEGIN_INTERFACE_PART(Sum, ISum) STDMETHOD_(void, Sum) (int x,int y,int *revtal); END_INTERFACE_PART(Sum) DECLARE_INTERFACE_MAP() };

  46. 類別implement test.cpp(1) #include "Interface.h" #include "test.h“  static const IID IID_ISum = { 0x692d03a4, 0xc689, 0x11ce, { 0xb3, 0x37, 0x88, 0xea, 0x36, 0xde, 0x9e, 0x4e } }; BEGIN_INTERFACE_MAP(CTest, CCmdTarget) INTERFACE_PART(CTest, IID_ISum, Sum) END_INTERFACE_MAP()

  47. 類別implement test.cpp(2) STDMETHODIMP_(ULONG) CTest::XSum::AddRef() { METHOD_PROLOGUE(CTest, Sum) return pThis->ExternalAddRef(); } STDMETHODIMP_(ULONG) CTest::XSum::Release() { METHOD_PROLOGUE(CTest, Sum) return pThis->ExternalRelease(); }

  48. 類別implement test.cpp(3) STDMETHODIMP CTest::XSum::QueryInterface( REFIID iid, LPVOID* ppvObj) { METHOD_PROLOGUE(CTest, Sum) return pThis->ExternalQueryInterface(&iid, ppvObj); } STDMETHODIMP_(void) CTest::XSum::Sum(int x,int y,int *revtal) { *revtal=x+y; return; }

  49. 用戶端程式(1): LPCLASSFACTORY pClf; LPUNKNOWN pUnk; ISum* pSum; Imultilpy *pMul HRESULT hr; CLSID clsid; if ((hr = ::CLSIDFromProgID(L"Test", &clsid)) != NOERROR) { return; }

  50. 用戶端程式(2): if((hr=::CoGetClassObject(clsid,CLSCTX_REMOTE_SERVER, NULL, IID_IClassFactory, (void **) &pClf)) != NOERROR) {; return; } pClf->CreateInstance(NULL, IID_IUnknown, (void**) &pUnk); pUnk->QueryInterface(IID_ISum, (void**) &pSum);

More Related