component.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:9k
- // component.cpp
- #define _WIN32_DCOM
- #include <windows.h>
- #include <ocidl.h>
- #include <iostream.h>
- #include <stdio.h>
- #include <conio.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}", "AppID", "{10000002-0000-0000-0000-000000000001}" },
- { "CLSID\{10000002-0000-0000-0000-000000000001}\InprocServer32", 0, (const char*)-1 },
- { "CLSID\{10000002-0000-0000-0000-000000000001}\InprocServer32", "ThreadingModel", "Both" },
- { "CLSID\{10000002-0000-0000-0000-000000000001}\ProgID", 0, "Component.InsideCOM.1" },
- { "CLSID\{10000002-0000-0000-0000-000000000001}\VersionIndependentProgID", 0, "Component.InsideCOM" },
- { "AppID\{10000002-0000-0000-0000-000000000001}", 0, "Inside COM+: In Process Component" },
- { "AppID\{10000002-0000-0000-0000-000000000001}", "DllSurrogate", "" },
- { "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 }
- };
- HINSTANCE g_hInstance;
- long g_cLocks = 0;
- interface INoAggregationUnknown
- {
- virtual HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv)=0;
- virtual ULONG __stdcall AddRef_NoAggregation()=0;
- virtual ULONG __stdcall Release_NoAggregation()=0;
- };
- class CPrimeCall : public AsyncIPrime, public INoAggregationUnknown
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // INoAggregationUnknown
- ULONG __stdcall AddRef_NoAggregation();
- ULONG __stdcall Release_NoAggregation();
- HRESULT __stdcall QueryInterface_NoAggregation(REFIID riid, void** ppv);
- // AsyncIPrime
- HRESULT __stdcall Begin_IsPrime(int testnumber);
- HRESULT __stdcall Finish_IsPrime(int* retval);
- void Init();
- CPrimeCall(IUnknown* pCtrlUnk);
- ~CPrimeCall();
- private:
- ISynchronize* m_pSynchronize;
- IUnknown* m_pCtrlUnk;
- unsigned char m_retval;
- ULONG m_cRef;
- };
- void CPrimeCall::Init()
- {
- HRESULT hr = 0;
- if(m_pCtrlUnk)
- {
- hr = m_pCtrlUnk->QueryInterface(IID_ISynchronize, (void**)&m_pSynchronize);
- if(FAILED(hr))
- MessageBox(0, "QI FAILED", "for ISynch", MB_OK);
- }
- }
- CPrimeCall::CPrimeCall(IUnknown* pCtrlUnk) : m_cRef(1)
- {
- g_cLocks++;
- m_pCtrlUnk = pCtrlUnk;
- m_pSynchronize = 0;
- }
- CPrimeCall::~CPrimeCall()
- {
- g_cLocks--;
- m_pSynchronize->Release();
- }
- HRESULT CPrimeCall::QueryInterface_NoAggregation(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- *ppv = (INoAggregationUnknown*)this;
- else if(riid == IID_AsyncIPrime)
- *ppv = (AsyncIPrime*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- ((IUnknown*)(*ppv))->AddRef();
- return S_OK;
- }
- ULONG CPrimeCall::AddRef_NoAggregation()
- {
- return ++m_cRef;
- }
- ULONG CPrimeCall::Release_NoAggregation()
- {
- if(--m_cRef != 0)
- return m_cRef;
- delete this;
- return 0;
- }
- ULONG CPrimeCall::AddRef()
- {
- return m_pCtrlUnk->AddRef();
- }
- ULONG CPrimeCall::Release()
- {
- return m_pCtrlUnk->Release();
- }
- HRESULT CPrimeCall::QueryInterface(REFIID riid, void** ppv)
- {
- return m_pCtrlUnk->QueryInterface(riid, ppv);
- }
- class CInsideCOM : public IPrime, public ICallFactory
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // IPrime
- HRESULT __stdcall IsPrime(int testnumber, int* retval);
- // ICallFactory
- HRESULT __stdcall CreateCall(REFIID riid, IUnknown* pCtrlUnk, REFIID riid2, IUnknown** ppv);
- CInsideCOM() : m_cRef(1) { g_cLocks++; }
- ~CInsideCOM()
- {
- cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
- g_cLocks--;
- }
- private:
- ULONG m_cRef;
- };
- HRESULT CInsideCOM::CreateCall(REFIID riid, IUnknown* pCtrlUnk, REFIID riid2, IUnknown** ppv)
- {
- if(riid != IID_AsyncIPrime)
- return E_INVALIDARG;
- CPrimeCall* pPrimeCall = new CPrimeCall(pCtrlUnk);
- pPrimeCall->Init();
- HRESULT hr = pPrimeCall->QueryInterface_NoAggregation(riid2, (void**)ppv);
- pPrimeCall->Release_NoAggregation();
- return hr;
- }
- HRESULT CPrimeCall::Begin_IsPrime(int testnumber)
- {
- unsigned long count;
- unsigned long HalfNumber = testnumber / 2 + 1;
- HRESULT hr = 0;
- for(count = 2; count < HalfNumber; count++)
- {
- if(CoTestCancel() == RPC_E_CALL_CANCELED)
- {
- char buffer[255];
- wsprintf(buffer, "Begin_IsPrime(%d) canceled", testnumber);
- MessageBox(0, buffer, "Call Canceled", MB_OK);
- return RPC_E_CALL_CANCELED;
- }
- if(testnumber % count == 0)
- {
- m_retval = false;
- m_pSynchronize->Signal();
- return S_OK;
- }
- }
- m_retval = true;
- m_pSynchronize->Signal();
- return S_OK;
- }
- HRESULT CPrimeCall::Finish_IsPrime(int* retval)
- {
- *retval = m_retval;
- return S_OK;
- }
- HRESULT CInsideCOM::IsPrime(int testnumber, int* retval)
- {
-
- unsigned long count;
- unsigned long HalfNumber = testnumber / 2 + 1;
-
- // MessageBox(0, "BEGIN", "ISPRIME", MB_OK);
- for(count = 2; count < HalfNumber; count++)
- {
- if(testnumber % count == 0)
- {
- *retval = false;
- return S_OK;
- }
- }
- *retval = true;
- 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 = (IPrime*)this;
- else if(riid == IID_ICallFactory)
- *ppv = (ICallFactory*)this;
- else if(riid == IID_IPrime)
- *ppv = (IPrime*)this;
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- ((IUnknown*)(*ppv))->AddRef();
- 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->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;
- }