local embed stub.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:5k
- // local.cpp
- #define _WIN32_DCOM
- #include <windows.h>
- #include <iostream.h> // For cout
- #include "registry.h" // For registry functions
- #include "Componentcomponent.h" // Generated by MIDL
- long g_cComponents = 0;
- long g_cServerLocks = 0;
- HANDLE g_hEvent;
- class CInsideCOM : public ISum
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID iid, void** ppv);
- // ISum
- HRESULT __stdcall Sum(int x, int y, int* retval);
- CInsideCOM() : m_cRef(1) { g_cComponents++; }
- ~CInsideCOM() { cout << "Component: CInsideCOM::~CInsideCOM()" << endl, g_cComponents--; }
- private:
- ULONG m_cRef;
- };
- ULONG CInsideCOM::AddRef()
- {
- cout << "Component: CInsideCOM::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return ++m_cRef;
- }
- ULONG CInsideCOM::Release()
- {
- cout << "Component: CInsideCOM::Release() m_cRef = " << m_cRef - 1 << endl;
- if(--m_cRef != 0)
- return m_cRef;
- SetEvent(g_hEvent); // ADD THIS!!!
- delete this;
- return 0;
- }
- HRESULT CInsideCOM::QueryInterface(REFIID iid, void** ppv)
- {
- if(iid == IID_IUnknown)
- {
- cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
- *ppv = reinterpret_cast<IUnknown*>(this);
- }
- else if(iid == IID_ISum)
- {
- cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
- *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 iid, void** ppv);
- // IClassFactory
- HRESULT __stdcall CreateInstance(IUnknown *pUnknownOuter, REFIID iid, void** ppv);
- HRESULT __stdcall LockServer(BOOL bLock);
- CFactory() : m_cRef(1) { }
- ~CFactory() { }
- 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 iid, void** ppv)
- {
- if((iid == IID_IUnknown) || (iid == 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 iid, 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(iid, ppv);
- pInsideCOM->Release();
- return hr;
- }
- HRESULT CFactory::LockServer(BOOL bLock)
- {
- if(bLock)
- g_cServerLocks++;
- else
- g_cServerLocks--;
- 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);
- cout << "Component: CoInitializeEx()" << endl;
- CoInitializeEx(NULL, COINIT_MULTITHREADED);
- // Code to avoid proxy-stub DLL
- IUnknown* pUnknown;
- DWORD dwUnused;
- DllGetClassObject(IID_ISum, IID_IUnknown, (void**)&pUnknown);
- CoRegisterClassObject(IID_ISum, pUnknown, CLSCTX_INPROC_SERVER, REGCLS_MULTIPLEUSE, &dwUnused);
- CoRegisterPSClsid(IID_ISum, IID_ISum);
- IClassFactory *pClassFactory = new CFactory();
- cout << "Component: CoRegisterClassObject()" << endl;
- DWORD dwRegister;
- CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
- g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- WaitForSingleObject(g_hEvent, INFINITE);
- CoRevokeClassObject(dwRegister);
- pClassFactory->Release();
- CoUninitialize();
- }