国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

COM---類廠

2019-11-08 20:10:51
字體:
來源:轉載
供稿:網友

問題:為什么要使用類廠? 答案:http://www.cppblog.com/ivenher/articles/16460.html

使用類廠的流程: 客戶調用CoCreateInstance,而CoCreateInstance調用CoGetClassObejct,CoGetClassObejct的作用是調用LoadLibrary尋找指定的COM組件(dll),然后使用GetPRocAddress尋找組件的入口函數,其中DllGetClassObject被調用,這幾步不可見,因為微軟已經封裝好了。 問題:CoGetClassObejct要加載dll,但我們并沒有傳dll路徑,如何加載dll的? 答案:是通過第一個參數CLSID(組件id,從CreateInstance傳過去)去注冊表中找到這個組件的DLL路徑,然后將DLL加載起來,就可以使用DLL的導出函數DllGetClassObject了,并把CLSID,類廠接口ID傳給它,去創建所需的組件,并返回所需的接口。

注意:組件并不等于dll,一個dll可以提供多個組件

接口

//// Iface.h - // Declarations of interfaces, IIDs, and CLSID// shared by the client and the component.//interface IX : IUnknown{ virtual void pascal Fx() = 0 ;};interface IY : IUnknown{ virtual void pascal Fy() = 0 ;};interface IZ : IUnknown{ virtual void pascal Fz() = 0 ;};//// Declaration of GUIDs for interfaces and component.// These constants are defined in GUIDs.cpp.//extern "C" const IID IID_IX ;extern "C" const IID IID_IY ;extern "C" const IID IID_IZ ;extern "C" const CLSID CLSID_Component1 ;

連接客戶和組件—GUID

//// GUIDs.cpp// - Defines all IIDs and CLSIDs for the client and the component.// The declaration of these GUIDs is in Iface.h//#include <objbase.h>// {32bb8320-b41b-11cf-a6bb-0080c7b2d682}extern "C" const IID IID_IX = {0x32bb8320, 0xb41b, 0x11cf, {0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}} ;// {32bb8321-b41b-11cf-a6bb-0080c7b2d682}extern "C" const IID IID_IY = {0x32bb8321, 0xb41b, 0x11cf, {0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}} ;// {32bb8322-b41b-11cf-a6bb-0080c7b2d682}extern "C" const IID IID_IZ = {0x32bb8322, 0xb41b, 0x11cf, {0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}} ;// {0c092c21-882c-11cf-a6bb-0080c7b2d682}extern "C" const CLSID CLSID_Component1 = {0x0c092c21, 0x882c, 0x11cf, {0xa6, 0xbb, 0x0, 0x80, 0xc7, 0xb2, 0xd6, 0x82}} ;

組件

//// Cmpnt.cpp//#include <iostream.h>#include <objbase.h>#include "Iface.h" // Interface declarations#include "Registry.h" // Registry helper functions// Trace functionvoid trace(const char* msg) { cout << msg << endl ;}/////////////////////////////////////////////////////////////// 全局變量//static HMODULE g_hModule = NULL ; // DLL module handlestatic long g_cComponents = 0 ; // Count of active componentsstatic long g_cServerLocks = 0 ; // Count of locks// Friendly name of componentconst char g_szFriendlyName[] = "Inside COM, Chapter 7 Example" ;// Version-independent ProgIDconst char g_szVerIndProgID[] = "InsideCOM.Chap07" ;// ProgIDconst char g_szProgID[] = "InsideCOM.Chap07.1" ;/////////////////////////////////////////////////////////////// 組件//class CA : public IX, public IY {public: // IUnknown virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ; virtual ULONG __stdcall AddRef() ; virtual ULONG __stdcall Release() ; // Interface IX virtual void __stdcall Fx() { cout << "Fx" << endl ;} // Interface IY virtual void __stdcall Fy() { cout << "Fy" << endl ;} // Constructor CA() ; // Destructor ~CA() ;private: // 引用計數 long m_cRef ;} ;//// Constructor//CA::CA() : m_cRef(1){ InterlockedIncrement(&g_cComponents) ; //增加組件計數}//// Destructor//CA::~CA() { InterlockedDecrement(&g_cComponents) ; //減少組件計數 trace("Component:/t/tDestroy self.") ;}//// IUnknown implementation//HRESULT __stdcall CA::QueryInterface(const IID& iid, void** ppv){ if (iid == IID_IUnknown) { *ppv = static_cast<IX*>(this) ; } else if (iid == IID_IX) { *ppv = static_cast<IX*>(this) ; trace("Component:/t/tReturn pointer to IX.") ; } else if (iid == IID_IY) { *ppv = static_cast<IY*>(this) ; trace("Component:/t/tReturn pointer to IY.") ; } else { *ppv = NULL ; return E_NOINTERFACE ; } reinterpret_cast<IUnknown*>(*ppv)->AddRef() ; return S_OK ;}ULONG __stdcall CA::AddRef(){ return InterlockedIncrement(&m_cRef) ;}ULONG __stdcall CA::Release() { if (InterlockedDecrement(&m_cRef) == 0) { delete this ; return 0 ; } return m_cRef ;}/////////////////////////////////////////////////////////////// 類廠,由開發人員決定。創建好類廠后,調用CreateInstance創建組件//class CFactory : public IClassFactory{public: // IUnknown virtual HRESULT __stdcall QueryInterface(const IID& iid, void** ppv) ; virtual ULONG __stdcall AddRef() ; virtual ULONG __stdcall Release() ; //IClassFactory 接口 virtual HRESULT __stdcall CreateInstance(IUnknown* pUnknownOuter, const IID& iid, void** ppv) ; virtual HRESULT __stdcall LockServer(BOOL bLock) ; // Constructor CFactory() : m_cRef(1) {} // Destructor ~CFactory() { trace("Class factory:/t/tDestroy self.") ;}private: long m_cRef ;} ;//// Class factory IUnknown implementation//HRESULT __stdcall CFactory::QueryInterface(const IID& iid, void** ppv){ if ((iid == IID_IUnknown) || (iid == IID_IClassFactory)) { *ppv = static_cast<IClassFactory*>(this) ; } else { *ppv = NULL ; return E_NOINTERFACE ; } reinterpret_cast<IUnknown*>(*ppv)->AddRef() ; return S_OK ;}ULONG __stdcall CFactory::AddRef(){ return InterlockedIncrement(&m_cRef) ;}ULONG __stdcall CFactory::Release() { if (InterlockedDecrement(&m_cRef) == 0) { delete this ; return 0 ; } return m_cRef ;}// IClassFactory implementation//創建一個組件,然后向它查詢某個接口//CreateInstance 對其所創建的組件知根知底//pUnknownOuter:同傳給CoCreateInstance的IUnknown指針相同,用于聚合組//剩余2個參數與QueryInterface相同HRESULT __stdcall CFactory::CreateInstance(IUnknown* pUnknownOuter, const IID& iid, void** ppv) { trace("Class factory:/t/tCreate component.") ; // Cannot aggregate. if (pUnknownOuter != NULL) { return CLASS_E_NOAGGREGATION ; } // 創建組件 CA* pA = new CA ; if (pA == NULL) { return E_OUTOFMEMORY ; } // 查詢接口 HRESULT hr = pA->QueryInterface(iid, ppv) ; // 釋放 IUnknown 指針 // (If QueryInterface failed, component will delete itself.) pA->Release() ; return hr ;}// 將服務器保存在內存中,直至使用完畢HRESULT __stdcall CFactory::LockServer(BOOL bLock) { if (bLock) { InterlockedIncrement(&g_cServerLocks) ; //加鎖 } else { InterlockedDecrement(&g_cServerLocks) ; //解鎖 } return S_OK ;}/////////////////////////////////////////////////////////////// 從DLL中輸出的函數////// 被CoFreeUnusedLibraries調用,以詢問DLL是否可被卸載掉,節省內存//STDAPI DllCanUnloadNow(){ //當DLL不再提供任何組件,且類廠沒有鎖 if ((g_cComponents == 0) && (g_cServerLocks == 0)) { return S_OK ; } else { return S_FALSE ; }}//由CoGetClassObject調用,創建客戶所請求的類廠//同CreateInstance類似,創建一個組件,然后向它查詢某個接口//DllGetClassObject 對其所創建的類廠無所不知//clsid:類廠將要創建的組件的CLSID//iid:類廠中客戶希望得到的接口的ID//ppv:保存接口指針STDAPI DllGetClassObject(const CLSID& clsid, const IID& iid, void** ppv){ trace("DllGetClassObject:/tCreate class factory.") ; // 檢查客戶所請求的類廠組件是否能創建 if (clsid != CLSID_Component1) { return CLASS_E_CLASSNOTAVAILABLE ; } // 用new操作符完成組件的創建 CFactory* pFactory = new CFactory ; // No AddRef in constructor if (pFactory == NULL) { return E_OUTOFMEMORY ; } // 用類廠組件查詢客戶所請求的接口 HRESULT hr = pFactory->QueryInterface(iid, ppv) ; pFactory->Release() ; return hr ;}//// 在Windows注冊表中登記組件//STDAPI DllRegisterServer(){ return RegisterServer(g_hModule, CLSID_Component1, g_szFriendlyName, g_szVerIndProgID, g_szProgID) ;}//// 在Windows注冊表中注銷組件//STDAPI DllUnregisterServer(){ return UnregisterServer(CLSID_Component1, g_szVerIndProgID, g_szProgID) ;}/////////////////////////////////////////////////////////////// DLL module information//將模塊句柄保存在全局變量g_hModule中,以供DllRegisterServer和 //DllUnregisterServer使用BOOL APIENTRY DllMain(HANDLE hModule, DWord dwReason, void* lpReserved){ if (dwReason == DLL_PROCESS_ATTACH) { g_hModule = hModule ; } return TRUE ;}

Cmpnt.def,定義dll要輸出的函數

LIBRARY Cmpnt.dllDESCR注冊組件

#ifndef __Registry_H__#define __Registry_H__//// Registry.h// - Helper functions 登記、注銷組件//// 在Windows注冊表中登記組件// 被DllRegisterServer調用HRESULT RegisterServer(HMODULE hModule, const CLSID& clsid, const char* szFriendlyName, const char* szVerIndProgID, const char* szProgID) ;// 在Windows注冊表中注銷組件// 被DllUnregisterServer調用HRESULT UnregisterServer(const CLSID& clsid, const char* szVerIndProgID, const char* szProgID) ;#endif//// Registry.cpp////定義了 #define interface struct#include <objbase.h>#include <assert.h>#include "Registry.h"http://////////////////////////////////////////////////////////// Internal helper functions prototypes//// Set the given key and its value.BOOL setKeyAndValue(const char* pszPath, const char* szSubkey, const char* szValue) ;// Convert a CLSID into a char string.void CLSIDtochar(const CLSID& clsid, char* szCLSID, int length) ;// Delete szKeyChild and all of its descendents.LONG recursiveDeleteKey(HKEY hKeyParent, const char* szKeyChild) ;//////////////////////////////////////////////////////////// Constants//// Size of a CLSID as a stringconst int CLSID_STRING_SIZE = 39 ;///////////////////////////////////////////////////////////// Public function implementation////// 在Windows注冊表中登記組件//HRESULT RegisterServer(HMODULE hModule, // DLL module handle const CLSID& clsid, // Class ID const char* szFriendlyName, // Friendly Name const char* szVerIndProgID, // Programmatic const char* szProgID) // IDs{ // Get server location. char szModule[512] ; DWORD dwResult = ::GetModuleFileName(hModule, szModule, sizeof(szModule)/sizeof(char)) ; assert(dwResult != 0) ; // Convert the CLSID into a char. char szCLSID[CLSID_STRING_SIZE] ; CLSIDtochar(clsid, szCLSID, sizeof(szCLSID)) ; // Build the key CLSID//{...} char szKey[64] ; strcpy(szKey, "CLSID//") ; strcat(szKey, szCLSID) ; // Add the CLSID to the registry. setKeyAndValue(szKey, NULL, szFriendlyName) ; // Add the server filename subkey under the CLSID key. setKeyAndValue(szKey, "InprocServer32", szModule) ; // Add the ProgID subkey under the CLSID key. setKeyAndValue(szKey, "ProgID", szProgID) ; // Add the version-independent ProgID subkey under CLSID key. setKeyAndValue(szKey, "VersionIndependentProgID", szVerIndProgID) ; // Add the version-independent ProgID subkey under HKEY_CLASSES_ROOT. setKeyAndValue(szVerIndProgID, NULL, szFriendlyName) ; setKeyAndValue(szVerIndProgID, "CLSID", szCLSID) ; setKeyAndValue(szVerIndProgID, "CurVer", szProgID) ; // Add the versioned ProgID subkey under HKEY_CLASSES_ROOT. setKeyAndValue(szProgID, NULL, szFriendlyName) ; setKeyAndValue(szProgID, "CLSID", szCLSID) ; return S_OK ;}//// 在Windows注冊表中注銷組件//LONG UnregisterServer(const CLSID& clsid, // Class ID const char* szVerIndProgID, // Programmatic const char* szProgID) // IDs{ // Convert the CLSID into a char. char szCLSID[CLSID_STRING_SIZE] ; CLSIDtochar(clsid, szCLSID, sizeof(szCLSID)) ; // Build the key CLSID//{...} char szKey[64] ; strcpy(szKey, "CLSID//") ; strcat(szKey, szCLSID) ; // Delete the CLSID Key - CLSID/{...} LONG lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szKey) ; assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist. // Delete the version-independent ProgID Key. lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szVerIndProgID) ; assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist. // Delete the ProgID key. lResult = recursiveDeleteKey(HKEY_CLASSES_ROOT, szProgID) ; assert((lResult == ERROR_SUCCESS) || (lResult == ERROR_FILE_NOT_FOUND)) ; // Subkey may not exist. return S_OK ;}/////////////////////////////////////////////////////////////// Internal helper functions//// Convert a CLSID to a char string.void CLSIDtochar(const CLSID& clsid, char* szCLSID, int length){ assert(length >= CLSID_STRING_SIZE) ; // Get CLSID LPOLESTR wszCLSID = NULL ; HRESULT hr = StringFromCLSID(clsid, &wszCLSID) ; assert(SUCCEEDED(hr)) ; // Covert from wide characters to non-wide. wcstombs(szCLSID, wszCLSID, length) ; // Free memory. CoTaskMemFree(wszCLSID) ;}//// Delete a key and all of its descendents.//LONG recursiveDeleteKey(HKEY hKeyParent, // Parent of key to delete const char* lpszKeyChild) // Key to delete{ // Open the child. HKEY hKeyChild ; LONG lRes = RegOpenKeyEx(hKeyParent, lpszKeyChild, 0, KEY_ALL_
access, &hKeyChild) ; if (lRes != ERROR_SUCCESS) { return lRes ; } // Enumerate all of the decendents of this child. FILETIME time ; char szBuffer[256] ; DWORD dwSize = 256 ; while (RegEnumKeyEx(hKeyChild, 0, szBuffer, &dwSize, NULL, NULL, NULL, &time) == S_OK) { // Delete the decendents of this child. lRes = recursiveDeleteKey(hKeyChild, szBuffer) ; if (lRes != ERROR_SUCCESS) { // Cleanup before exiting. RegCloseKey(hKeyChild) ; return lRes; } dwSize = 256 ; } // Close the child. RegCloseKey(hKeyChild) ; // Delete this child. return RegDeleteKey(hKeyParent, lpszKeyChild) ;}//// Create a key and set its value.// - This helper function was borrowed and modifed from// Kraig Brockschmidt's book Inside OLE.//BOOL setKeyAndValue(const char* szKey, const char* szSubkey, const char* szValue){ HKEY hKey; char szKeyBuf[1024] ; // Copy keyname into buffer. strcpy(szKeyBuf, szKey) ; // Add subkey name to buffer. if (szSubkey != NULL) { strcat(szKeyBuf, "http://") ; strcat(szKeyBuf, szSubkey ) ; } // Create and open key and subkey. long lResult = RegCreateKeyEx(HKEY_CLASSES_ROOT , szKeyBuf, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL) ; if (lResult != ERROR_SUCCESS) { return FALSE ; } // Set the Value. if (szValue != NULL) { RegSetValueEx(hKey, NULL, 0, REG_SZ, (BYTE *)szValue, strlen(szValue)+1) ; } RegCloseKey(hKey) ; return TRUE ;}

在Cmpnt.dll的編譯和連接后使用以下命令

regsvr32 -s Cmpnt.dll

REGSVR32.exe將調用DllRegisterServer函數完成組件的登記

參考 http://www.360doc.com/content/12/0228/09/7023119_190190971.shtml


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 甘德县| 颍上县| 奈曼旗| 元谋县| 建水县| 乡宁县| 长武县| 张家港市| 东台市| 久治县| 雷山县| 嵩明县| 家居| 沙田区| 富源县| 广宁县| 揭西县| 临邑县| 竹溪县| 扶余县| 洛隆县| 黄浦区| 固镇县| 昔阳县| 新龙县| 广平县| 日土县| 玉龙| 沧源| 洛川县| 蓬莱市| 辽源市| 蚌埠市| 即墨市| 兰西县| 馆陶县| 房产| 垦利县| 滦平县| 祁东县| 嘉黎县|