Copyright, 2001 Multimedia Lab., CH 3. COM object (In-process server) Eun-sung Lee twoss@mmlab.net Multimedia Lab. Dept. of Electrical and Computer Eng. University of Seoul Seoul, Korea
0. Contents 1. Main Objectives 2. Overview 3. How to create DLL 4. Let s s make a DLL 5. Reference 2004-10-15 Eun-sung Lee DirectShow -2
1. Main Objectives COM COM Class (In-Process server) 2004-10-15 Eun-sung Lee DirectShow -3
2. Overview COM Library COM Client Server Application API. CLSID,. Co Ex) CoInitialize, CoCreateInstance COMPOBJ.DLL 2004-10-15 Eun-sung Lee DirectShow -4
2. Overview COM Object 1. COM Component Regsvr32 Ex) regsvr32 addback.dll dll 2. COM Library [CoInitialize] 3. Interface COM Object CLSID 4. COM Object Instance [CoCreateInstance] 5. Interface Pointer 6. method, 7. COM Library [CoUnInitailize] 2004-10-15 Eun-sung Lee DirectShow -5
3. How to Create a DLL 1. CoCreateInstance(clsid clsid, iid, ) 2. CoGetClassObject() HKEY_CLASSES_ROOT CLSID clsid CLSID InprocServer32(in InprocServer32(in- process) COM. 3. CoLoadLibrary() dll load 4.DllGetClassObject DllGetClassObject() clsid COM Class Factory IClassFactory pointer IClassFactory iid pointer ->CoCreateInstance IClassFactory IClassFactory:: ::CreateInstance 2004-10-15 Eun-sung Lee DirectShow -6
3. How to Create a DLL CoCreateInstance( CLSID_FilterGraph FilterGraph,//** [In] Class Identifier(CLSID) of the object NULL, //** [In] Pointer to controlling IUnknown CLSCTX_INPROC,//** [In] Server Context IID_IGraphBuilder IGraphBuilder,//** [In] Reference to the identifier of the interface (void **)&pgraph );//** //** [Out] Address of pointer variable that receives the interface pointer requested in riid. 2004-10-15 Eun-sung Lee DirectShow -7
Use Case Client CoInitialize(); CoCreateInstance(CLSID, IID); CoUninitialize(); Dllmain() DllGetClassObject() DllCanUnloadNow() SetRegKeyValue() DllRegisterServer() DllUnRegisterServer() Class Factory Create Instance IClassFactory DLL (IN-process Server) New COM object CAddBack IInterface Linear 4G memory(one Process Own) COM Library CoCreateInstance(CLSID, IID); System registry CoGetClassObject CoLoadlibrary DLL (IN-process) DLL (IN-process) DLL (IN-process) DLL (IN-process) DLL (IN-process) DLL (IN-process) DLL (IN-process) 2004-10-15 Eun-sung Lee DirectShow -8
3. How to Create a DLL - Class Factory Class Factory COM Class Instance COM Class IClassFactory Interface. IClassFactory Interface COM Interface IUnKnown Interface. ClassFactory provides two methods : CreateInstance,, creates an uninitialized object of a specified CLSID LockServer,, locks the object's server in memory, allowing new objects to be created more quickly 2004-10-15 Eun-sung Lee DirectShow -9
3. How to Create a DLL - CreateInstance IClassFactory:: ::CreateInstance Creates an uninitialized object. : CLSID. UNKNOWN.H (CoGetClassObject() CLSID) HRESULT CreateInstance( IUnknown * punkouter ); punkouter, //Pointer to whether object is or isn't part of an aggregate REFIID riid, //Reference to the identifier of the void ** ppvobject ppvobject //Address of output variable that the interface pointer requested in riid interface receives 2004-10-15 Eun-sung Lee DirectShow -10
3. How to Create a DLL - LockServer IClassFactory :: LockServer( ( ) Client IClassFactory Interface Pointer Client COM Object Dll Unload. HRESULT stdcall CFAddBack:: ::LockServer(BOOL block) { if(block block) ++g_clocks clocks; else --g_ g_clocks; } return S_OK; 2004-10-15 Eun-sung Lee DirectShow -11
3. How to Create a DLL - DLL Functions DLL Functions DllMain: The DLL entry point. The name DllMain is a placeholder for the library- defined function name. DllMain DllGetClassObject: Creates a class factory instance. DllCanUnloadNow: Queries whether the DLL can safely be unloaded. DllGetClassObject DllCanUnloadNow DllRegisterServer: Creates registry entries for the DLL. Call by regsvr32.exe DllRegisterServer DllUnregisterServer: Removes registry Removes registry entries for the DLL. Call by regsvr32.exe 2004-10-15 Eun-sung Lee DirectShow -12
3. How to Create a DLL - DllGetClassObject DllGetClassObject COM class instance, COM class Class Factory COM class Instance. ICalssFactory interface pointer client. In_process server DllGetClassObject. 2004-10-15 Eun-sung Lee DirectShow -13
3. How to Create a DLL STDAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID* ppv) { HRESULT hr = CLASS_E_CLASSNOTAVAILABLE; IUnknown* punk = NULL; if(rclsid == CLSID_AddBack) { hr = E_OUTOFMEMORY; punk = new CFAddBack; } } if(punk!= NULL) { hr = punk->queryinterface(riid, ppv); //IClassFactory pointer if(failed(hr)) delete punk; } return hr; 2004-10-15 Eun-sung Lee DirectShow -14
3. How to Create a DLL DllRegisterServer/DllUnregisterServer DllUnregisterServer In_process srever REGSVR32.exe Dll DllRegisterServer export. Ex. >> regsvr32 <name.dll dll> DllUnregisterServer export. Ex. >> regsvr32 /u <name.dll dll> 2004-10-15 Eun-sung Lee DirectShow -15
3. How to Create a DLL module-definition definition (.def) file If you are not using the declspec declspec(dllexport) keyword to export the DLL s s functions, then the DLL requires a.def file : export all the DLL functions except for the entry-point function. The following is an example *.def file. Ex> LIBRARY "AddBack" AddBack.Dll" EXPORTS DllCanUnloadNow @1 PRIVATE DllGetClassObject @2 PRIVATE DllRegisterServer @3 PRIVATE DllUnregisterServer @4 PRIVATE 2004-10-15 Eun-sung Lee DirectShow -16
3. How to Create a DLL DllCanUnloadNow CoFreeUnusedLibraries COM Library API DLL Unload STDAPI DllCanUnloadNow(void) { if(g_ g_cobjects == 0 && g_clocks == 0) return S_OK; return S_FALSE; } g_cobject cobject 2004-10-15 Eun-sung Lee DirectShow -17
4. Let s s make a DLL AddBack.Dll IAddEnd IAdd CAddBack COM Class IClassFactory DllMain ( ) CFAddBack DllGetClassObject( ( ) DllCanUnloadNow ( ) DllRegisterServer ( ) Class Factory COM Class 2004-10-15 Eun-sung Lee DirectShow -18
4. Let s s make a DLL - IDL 1) Definition Interface (IAddEnd( IAddEnd,IAdd) 2004-10-15 Eun-sung Lee DirectShow -19
4. Let s s make a DLL Ex) interface IAddEnd [ uuid(c6f96d90-9fcf-11d1-b20a-0060970a3516), object ] interface IAddEnd : IUnknown { import "unknwn.idl"; HRESULT GetAddEnd([out] short* result); HRESULT SetAddEnd([in] short addend); HRESULT GetSum([out] short* result); HRESULT Clear(void); }; 2004-10-15 Eun-sung Lee DirectShow -20
4. Let s s make a DLL In Addback.h #if defined( cplusplus) &&!defined(cinterface) interface DECLSPEC_UUID("C6F96D90-9FCF-11d1-B20A- 0060970A3516") IAddEnd : public IUnknown { public: virtual HRESULT STDMETHODCALLTYPE GetAddEnd( /* [out] */ short RPC_FAR *result) = 0; virtual HRESULT STDMETHODCALLTYPE SetAddEnd( /* [in] */ short addend) = 0; virtual HRESULT STDMETHODCALLTYPE GetSum( /* [out] */ short RPC_FAR *result) = 0; virtual HRESULT STDMETHODCALLTYPE Clear( void) = 0 }; 2004-10-15 Eun-sung Lee DirectShow -21
4. Let s s make a DLL In Addback_i.c const IID IID_IAddEnd IAddEnd = {0xC6F96D90,0x9FCF,0x11d1,{0xB2,0x0A,0x 00,0x60,0x97,0x0A,0x35,0x16}}; 2004-10-15 Eun-sung Lee DirectShow -22
4. Let s s make a DLL 2. In_process_server Client Files LockServer( ) 2004-10-15 Eun-sung Lee DirectShow -23
4. Let s s make a DLL Relationship 2004-10-15 Eun-sung Lee DirectShow -24
5. Reference http://www.microsoft.com/com ATL COM Inside COM Essential COM 2004-10-15 Eun-sung Lee DirectShow -25