component.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:4k
- // component with registration.cpp
- #include <iostream.h>
- #include "Componentcomponent.h" // Generated by MIDL
- #include "registry.h"
- const REG_DATA g_regData[] = {
- { "CLSID\{10000002-0000-0000-0000-000000000001}", 0, "Inside COM+: In Process Component" },
- { "CLSID\{10000002-0000-0000-0000-000000000001}\InprocServer32", 0, (const char*)-1 },
- { "CLSID\{10000002-0000-0000-0000-000000000001}\ProgID", 0, "Component.InsideCOM.1" },
- { "CLSID\{10000002-0000-0000-0000-000000000001}\VersionIndependentProgID", 0, "Component.InsideCOM" },
- { "Component.InsideCOM", 0, "Inside COM+: In Process Component" },
- { "Component.InsideCOM\CLSID", 0, "{10000002-0000-0000-0000-000000000001}" },
- { "Component.InsideCOM\CurVer", 0, "Component.InsideCOM.1" },
- { "Component.InsideCOM.1", 0, "Inside COM+: In Process Component" },
- { "Component.InsideCOM.1\CLSID", 0, "{10000002-0000-0000-0000-000000000001}" },
- { 0, 0, 0 }
- };
- // {10000002-0000-0000-0000-000000000001}
- const CLSID CLSID_InsideCOM = {0x10000002,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01}};
- HINSTANCE g_hInstance;
- long g_cLocks = 0;
- class CInsideCOM : public ISum
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // ISum
- HRESULT __stdcall Sum(int x, int y, int* retval);
- CInsideCOM() : m_cRef(1) { g_cLocks++; }
- ~CInsideCOM()
- {
- g_cLocks--;
- }
- private:
- ULONG m_cRef;
- };
- ULONG CInsideCOM::AddRef()
- {
- return ++m_cRef;
- }
- ULONG CInsideCOM::Release()
- {
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- *ppv = (IUnknown*)this;
- else if(riid == IID_ISum)
- *ppv = (ISum*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CInsideCOM::Sum(int x, int y, int* retval)
- {
- cout << "Component: CInsideCOM::Sum() " << x << " + " << y << " = " << x + y << endl;
- *retval = x + y;
- return S_OK;
- }
- class CFactory : public IClassFactory
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // IClassFactory
- HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv);
- HRESULT __stdcall LockServer(BOOL bLock);
- CFactory() : m_cRef(1) { g_cLocks++; }
- ~CFactory() { g_cLocks--; }
- private:
- ULONG m_cRef;
- };
- ULONG CFactory::AddRef()
- {
- return ++m_cRef;
- }
- ULONG CFactory::Release()
- {
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- *ppv = (IUnknown*)this;
- else if(riid == IID_IClassFactory)
- *ppv = (IClassFactory*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CFactory::CreateInstance(IUnknown *pUnknownOuter, REFIID riid, void** ppv)
- {
- if(pUnknownOuter != NULL)
- return CLASS_E_NOAGGREGATION;
- CInsideCOM *pInsideCOM = new CInsideCOM;
- cout << "Component: CFactory::CreateInstance() " << pInsideCOM << endl;
- if(pInsideCOM == NULL)
- return E_OUTOFMEMORY;
- // QueryInterface probably for IID_IUnknown
- HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
- pInsideCOM->Release();
- return hr;
- }
- HRESULT CFactory::LockServer(BOOL bLock)
- {
- if(bLock)
- g_cLocks++;
- else
- g_cLocks--;
- return S_OK;
- }
- HRESULT __stdcall DllCanUnloadNow()
- {
- cout << "Component: DllCanUnloadNow() " << (g_cLocks == 0 ? "Yes" : "No") << endl;
- if(g_cLocks == 0)
- return S_OK;
- else
- return S_FALSE;
- }
- HRESULT __stdcall DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv)
- {
- cout << "Component: DllGetClassObject" << endl;
-
- if(clsid != CLSID_InsideCOM)
- return CLASS_E_CLASSNOTAVAILABLE;
- CFactory* pFactory = new CFactory;
- if(pFactory == NULL)
- return E_OUTOFMEMORY;
- // QueryInterface probably for IClassFactory
- HRESULT hr = pFactory->QueryInterface(riid, ppv);
- pFactory->Release();
- return hr;
- }
- HRESULT __stdcall DllRegisterServer()
- {
- char DllPath[MAX_PATH];
- GetModuleFileName(g_hInstance, DllPath, sizeof(DllPath));
- return RegisterServerEx(g_regData, DllPath);
- }
- HRESULT __stdcall DllUnregisterServer()
- {
- return UnregisterServerEx(g_regData);
- }
- BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
- {
- g_hInstance = hInstance;
- return TRUE;
- }