local.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:4k
- // 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
- 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);
- };
- // No need for reference counting in a singleton
- ULONG CInsideCOM::AddRef()
- {
- return 2;
- }
- ULONG CInsideCOM::Release()
- {
- return 1;
- }
- HRESULT CInsideCOM::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- {
- cout << "Component: CInsideCOM::QueryInterface() for IUnknown returning " << this << endl;
- *ppv = reinterpret_cast<IUnknown*>(this);
- }
- else if(riid == IID_ISum)
- {
- cout << "Component: CInsideCOM::QueryInterface() for ISum returning " << this << endl;
- *ppv = (ISum*)this;
- }
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- 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) { }
- ~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 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;
- // Always return the same instance
- static CInsideCOM InsideCOM;
- cout << "Component: CFactory::CreateInstance() " << &InsideCOM << endl;
- // QueryInterface probably for IID_IUnknown
- return InsideCOM.QueryInterface(riid, ppv);
- }
- HRESULT CFactory::LockServer(BOOL bLock)
- {
- return S_OK;
- }
- void RegisterComponent()
- {
- ITypeLib* pTypeLib;
- LoadTypeLibEx(L"Component.exe", REGKIND_DEFAULT, &pTypeLib);
- RegisterServer("Component.exe", CLSID_InsideCOM, "Inside COM Sample #1", "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);
- IClassFactory *pClassFactory = new CFactory();
- cout << "Component: CoRegisterClassObject()" << endl;
- DWORD dwRegister;
- CoRegisterClassObject(CLSID_InsideCOM, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
- HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- WaitForSingleObject(hEvent, INFINITE);
- CoRevokeClassObject(dwRegister);
- pClassFactory->Release();
- CoUninitialize();
- }