local.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:5k
- // local.cpp
- #define _WIN32_DCOM
- #include <iostream.h>
- #include "Componentcomponent.h"
- #include "registry.h"
- HANDLE g_hEvent;
- 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();
- ~CInsideCOM();
- private:
- long m_cRef;
- };
- void InitiateComponentShutdown()
- {
- cout << "InitiateComponentShutdown()" << endl;
- PostQuitMessage(0);
- }
- CInsideCOM::CInsideCOM() : m_cRef(1)
- {
- CoAddRefServerProcess();
- }
- CInsideCOM::~CInsideCOM()
- {
- cout << "Component: CInsideCOM::~CInsideCOM()" << endl;
- if(CoReleaseServerProcess() == 0)
- InitiateComponentShutdown();
- }
- ULONG CInsideCOM::AddRef()
- {
- cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return InterlockedIncrement(&m_cRef);
- }
- ULONG CInsideCOM::Release()
- {
- cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
- unsigned cRef = InterlockedDecrement(&m_cRef);
- if(cRef != 0)
- return cRef;
- delete this;
- return 0;
- }
- HRESULT CInsideCOM::QueryInterface(REFIID riid, void **ppv)
- {
- if(riid == IID_IUnknown)
- {
- cout << "Component: CInsideCOM::QueryInterface() for IUnknown" << endl;
- *ppv = (IUnknown*)this;
- }
- else if(riid == IID_ISum)
- {
- cout << "Component: CInsideCOM::QueryInterface() for ISum" << endl;
- *ppv = (ISum*)this;
- }
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- HRESULT CInsideCOM::Sum(int x, int y, int *retval)
- {
- *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) { }
- ~CFactory() { cout << "Component: CFactory::~CFactory()" << endl; }
- private:
- long m_cRef;
- };
- ULONG CFactory::AddRef()
- {
- cout << "Component: CFactory::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return InterlockedIncrement(&m_cRef);
- }
- ULONG CFactory::Release()
- {
- cout << "Component: CFactory::Release() m_cRef = " << m_cRef - 1 << endl;
- unsigned cRef = InterlockedDecrement(&m_cRef);
- if(cRef != 0)
- return cRef;
- delete this;
- return 0;
- }
- HRESULT CFactory::QueryInterface(REFIID riid, void** ppv)
- {
- if((riid == IID_IUnknown) || (riid == IID_IClassFactory))
- {
- cout << "Component: CFactory::QueryInteface() for IUnknown or IClassFactory " << this << endl;
- *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;
- HRESULT hr = pInsideCOM->QueryInterface(riid, ppv);
- pInsideCOM->Release();
- return hr;
- }
- HRESULT CFactory::LockServer(BOOL bLock)
- {
- if(bLock)
- CoAddRefServerProcess();
- else
- if(CoReleaseServerProcess() == 0)
- InitiateComponentShutdown();
- return S_OK;
- }
- void RegisterComponent()
- {
- ITypeLib* pTypeLib;
- LoadTypeLibEx(L"component.exe", REGKIND_DEFAULT, &pTypeLib);
- pTypeLib->Release();
- RegisterServer("component.exe", CLSID_InsideCOM, "Inside COM+ Sample", "Component.InsideCOM", "Component.InsideCOM.1", NULL);
- }
- void CommandLineParameters(int argc, char** argv)
- {
- RegisterComponent();
- if(argc < 2)
- {
- cout << "No parameter, but registered anyway..." << endl;
- exit(false);
- }
- char* szToken = strtok(argv[1], "-/");
- if(_stricmp(szToken, "RegServer") == 0)
- {
- RegisterComponent();
- cout << "RegServer" << endl;
- exit(true);
- }
- if(_stricmp(szToken, "UnregServer") == 0)
- {
- UnRegisterTypeLib(LIBID_Component, 1, 0, LANG_NEUTRAL, SYS_WIN32);
- UnregisterServer(CLSID_InsideCOM, "Component.InsideCOM", "Component.InsideCOM.1");
- cout << "UnregServer" << endl;
- exit(true);
- }
- if(_stricmp(szToken, "Embedding") != 0)
- {
- cout << "Invalid parameter" << endl;
- exit(false);
- }
- }
- void main(int argc, char** argv)
- {
- CommandLineParameters(argc, argv);
- CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
- DWORD dwRegister;
- IClassFactory *pIFactory = new CFactory();
- CoRegisterClassObject(CLSID_InsideCOM, pIFactory, CLSCTX_LOCAL_SERVER, REGCLS_SUSPENDED|REGCLS_MULTIPLEUSE, &dwRegister);
- CoResumeClassObjects();
- MSG msg;
- while(GetMessage(&msg, NULL, 0, 0))
- DispatchMessage(&msg);
- CoRevokeClassObject(dwRegister);
- pIFactory->Release();
- CoUninitialize();
- }