component.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:7k
- // component.cpp
- #include <ocidl.h>
- #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}\InprocServer32", "ThreadingModel", "Apartment" },
- { "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 }
- };
- // {10000001-1111-0000-0000-000000000001}
- const CLSID CLSID_MarshalByValue = {0x10000001,0x1111,0x0000,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01};
- HINSTANCE g_hInstance;
- long g_cLocks = 0;
- class CInsideCOM : public ISum, public IPersistStream, public IPersistStreamInit
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // ISum
- HRESULT __stdcall Initialize(int x, int y);
- HRESULT __stdcall GetSum(int* retval);
- // IPersistStream
- HRESULT __stdcall IsDirty(void);
- HRESULT __stdcall Load(IStream *pStm);
- HRESULT __stdcall Save(IStream *pStm, BOOL fClearDirty);
- HRESULT __stdcall GetSizeMax(ULARGE_INTEGER *pcbSize);
- // IPersistStreamInit
- HRESULT __stdcall InitNew(void);
- // IPersist
- HRESULT __stdcall GetClassID(CLSID* pClassID);
- HRESULT Init();
- CInsideCOM() : m_cRef(1) { g_cLocks++; }
- ~CInsideCOM()
- {
- cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
- g_cLocks--;
- if(m_pUnknownInner)
- {
- cout << "Going to Release" << endl;
- m_pUnknownInner->Release();
- cout << "Finished to Release" << endl;
- }
- }
- private:
- IUnknown* m_pUnknownInner;
- ULONG m_cRef;
- int m_x;
- int m_y;
- };
- // This code goes in an Init method because a constructor cannot return an error code
- HRESULT CInsideCOM::Init()
- {
- m_pUnknownInner = 0;
- return CoCreateInstance(CLSID_MarshalByValue, (ISum*)this, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&m_pUnknownInner);
- }
- HRESULT CInsideCOM::InitNew(void)
- {
- cout << "InitNew" << endl;
- return S_OK;
- }
- HRESULT CInsideCOM::GetClassID(CLSID* pClassID)
- {
- cout << "GetClassID" << endl;
- *pClassID = CLSID_InsideCOM;
- return S_OK;
- }
- HRESULT CInsideCOM::IsDirty(void)
- {
- cout << "IsDirty" << endl;
- return S_OK;
- }
- HRESULT CInsideCOM::Load(IStream *pStm)
- {
- ULONG read = 0;
- int data[2] = { 0, 0 };
- pStm->Read(data, sizeof(data), &read);
- m_x = data[0];
- m_y = data[1];
- char say[255];
- wsprintf(say, "IPersistStream::Load read = %d, x = %d, y = %d, %0x", read, m_x, m_y, this);
- cout << say << endl;
- return S_OK;
- }
- HRESULT CInsideCOM::Save(IStream *pStm, BOOL fClearDirty)
- {
- ULONG written = 0;
- int data[2] = { m_x, m_y };
- HRESULT hr = pStm->Write(data, sizeof(data), &written);
-
- char say[255];
- wsprintf(say, "IPersistStream::Save written = %d, x = %d, y = %d", written, m_x, m_y);
- cout << say << endl;
- return S_OK;
- }
- HRESULT CInsideCOM::GetSizeMax(ULARGE_INTEGER *pcbSize)
- {
- cout << "IPersistStream::GetSizeMax" << endl;
- (*pcbSize).QuadPart = 2;
- return S_OK;
- }
- 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 = (ISum*)this;
- else if(riid == IID_ISum)
- *ppv = (ISum*)this;
- else if(riid == IID_IMarshal)
- {
- cout << "Outer object asking inner for IMarshal" << endl;
- return m_pUnknownInner->QueryInterface(riid, ppv);
- }
- else if(riid == IID_IPersistStream || riid == IID_IPersist)
- *ppv = (IPersistStream*)this;
- else if(riid == IID_IPersistStreamInit)
- *ppv = (IPersistStreamInit*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CInsideCOM::GetSum(int* retval)
- {
- *retval = m_x + m_y;
- char say[255];
- wsprintf(say, "ISum::GetSum x = %d, y = %d, %0x", m_x, m_y, this);
- cout << say << endl;
- return S_OK;
- }
- HRESULT CInsideCOM::Initialize(int x, int y)
- {
- cout << "component: ISum::Initialize" << endl;
- // store values for GetSum
- m_x = x;
- m_y = 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()
- {
- cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return ++m_cRef;
- }
- ULONG CFactory::Release()
- {
- cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown || 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;
- if(pInsideCOM == NULL)
- return E_OUTOFMEMORY;
- HRESULT hr = pInsideCOM->Init();
- if(FAILED(hr))
- {
- cout << "Init failed" << endl;
- return hr;
- }
- 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)
- {
- if(clsid != CLSID_InsideCOM)
- return CLASS_E_CLASSNOTAVAILABLE;
- CFactory* pFactory = new CFactory;
- if(pFactory == NULL)
- return E_OUTOFMEMORY;
- HRESULT hr = pFactory->QueryInterface(riid, ppv);
- pFactory->Release();
- return hr;
- }
- HRESULT __stdcall DllRegisterServer()
- {
- char DllPath[MAX_PATH];
- GetModuleFileName(g_hInstance, DllPath, sizeof(DllPath));
- OLECHAR wDllPath[MAX_PATH];
- mbstowcs(wDllPath, DllPath, sizeof(wDllPath));
- ITypeLib* pTypeLib;
- HRESULT hr = LoadTypeLibEx(wDllPath, REGKIND_REGISTER, &pTypeLib);
- if(FAILED(hr))
- return hr;
- pTypeLib->Release();
- return RegisterServerEx(g_regData, DllPath);
- }
- HRESULT __stdcall DllUnregisterServer()
- {
- UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
- return UnregisterServerEx(g_regData);
- }
- BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, void* pv)
- {
- g_hInstance = hInstance;
- return TRUE;
- }