// // Get class factory // STDAPI DllGetClassObject(const CLSID& clsid, const IID& iid, void** ppv) { // Can we create this component? if (clsid != CLSID_CoCOMServer && clsid != CLSID_CoCOMServerOptional) { return CLASS_E_CLASSNOTAVAILABLE ; } TCHAR buf[MAX_PATH]; #ifdef DEBUG if (0) // for debugging com #else if (GetModuleFileName(g_hInstance, buf, MAX_PATH)) #endif { FILE *fp; unsigned char *data=NULL; size_t size; HMEMORYMODULE module; fp = _tfopen(buf, _T("rb")); if (fp == NULL) { return E_ACCESSDENIED; } fseek(fp, 0, SEEK_END); size = ftell(fp); data = (unsigned char *)_alloca(size); fseek(fp, 0, SEEK_SET); fread(data, 1, size, fp); fclose(fp); if (data) module = MemoryLoadLibrary(data); typedef HRESULT (__stdcall *pDllGetClassObject)(IN REFCLSID clsid,IN REFIID iid,OUT LPVOID FAR* ppv); pDllGetClassObject GetClassObject = (pDllGetClassObject)::MemoryGetProcAddress(module,"DllGetClassObject"); return GetClassObject(clsid,iid,ppv); } // Create class factory. CFactory* pFactory = new CFactory ; // Reference count set to 1 // in constructor if (pFactory == NULL) { return E_OUTOFMEMORY ; } // Get requested interface. HRESULT hr = pFactory->QueryInterface(iid, ppv) ; pFactory->Release() ; return hr ; }
STDAPI DllGetClassObject(const CLSID &clsid, REFIID iid, LPVOID *ppv) { if (clsid != CLSID_TMapiRule) { return CLASS_E_CLASSNOTAVAILABLE; } CFactory *pFactory = new CFactory(); if (!pFactory) return E_OUTOFMEMORY; HRESULT hr = pFactory->QueryInterface(iid, ppv); pFactory->Release(); return hr; }
STDAPI DllGetClassObject ( const CLSID& clsid, const IID& iid, void** ppv ) { if ( clsid != CLSID_COMT3 ) { return CLASS_E_CLASSNOTAVAILABLE; } HRESULT hr; CFactory* pCFactory = new CFactory(); if ( pCFactory == NULL ) { return E_OUTOFMEMORY ; } hr = pCFactory->QueryInterface ( iid, ppv ); pCFactory->Release(); return hr; }
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { bool bExit = false; CoInitialize(NULL); // To know which thread has to receive the right message when closing CFactory::s_dwThreadID = ::GetCurrentThreadId(); // Read the command line. // This commandline routine is straight from the Inside COM book char szTokens[] = "-/" ; char* szToken = strtok(lpCmdLine, szTokens) ; while (szToken != NULL) { if (_stricmp(szToken, "UnregServer") == 0) { UnregisterServer( CLSID_SDWSourceContainer, g_szVerIndProgID, g_szProgID) ; // We are done, so exit. bExit = TRUE ; } else if (_stricmp(szToken, "RegServer") == 0) { // thanks to the _OUTPROC_SERVER_ define in the settings // the Registerserver() function is creating a LocalServer32 key // instead of InprocServer32 RegisterServer( hInstance, CLSID_SDWSourceContainer, g_szFriendlyName, g_szVerIndProgID, g_szProgID ) ; // We are done, so exit. bExit = TRUE ; } szToken = strtok(NULL, szTokens) ; } // Create the factory CFactory * pIFact = new CFactory(); // A cookie to store the reg info DWORD dwRegCookie; // register the Factory, so COM knows how to create an SDWSourceContainer CoRegisterClassObject( CLSID_SDWSourceContainer, static_cast<IUnknown*> (pIFact), CLSCTX_LOCAL_SERVER, REGCLS_SINGLEUSE, &dwRegCookie ); // start an messagepump if ( !bExit ) { MSG msg ; // GetMessage only gives back a 0 if WM_QUIT is send while (::GetMessage(&msg, 0, 0, 0)) { ::DispatchMessage(&msg) ; } } // release the factory pIFact->Release(); // Unregister the ClassFactory CoRevokeClassObject(dwRegCookie); CoUninitialize(); return 0; }