local.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:6k
- // 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
- #include <stdio.h>
- #include <conio.h>
- long g_cComponents = 0;
- long g_cServerLocks = 0;
- HANDLE g_hEvent;
- class CTestIDL : public IPointerTest
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- HRESULT __stdcall GetInterfacePointer2(REFIID riid, IUnknown** ppv);
- HRESULT __stdcall GetInterfacePointer3(IPointerTest** ppv);
- HRESULT __stdcall GetInterfacePointer4(REFIID riid, void** ppv);
- CTestIDL() : m_cRef(1) { g_cComponents++; }
- ~CTestIDL() { cout << "Component: CTestIDL::~CTestIDL()" << endl, g_cComponents--; }
- private:
- ULONG m_cRef;
- };
- // HRESULT GetInterfacePointer2([in] REFIID riid, [out, iid_is(riid)] IUnknown** ppv);
- HRESULT CTestIDL::GetInterfacePointer2(REFIID riid, IUnknown** ppv)
- {
- return QueryInterface(riid, (void**)ppv);
- }
- // HRESULT GetInterfacePointer3([out] IPointerTest** ppv);
- HRESULT CTestIDL::GetInterfacePointer3(IPointerTest** ppv)
- {
- return QueryInterface(IID_IPointerTest, (void**)ppv);
- }
- // [local] HRESULT GetInterfacePointer4([in] REFIID riid, [out, iid_is(riid)] void** ppv);
- // [call_as(GetInterfacePointer4)] HRESULT RemoteGetInterfacePointer4([in] REFIID riid, [out, iid_is(riid)] IUnknown** ppv);
- HRESULT CTestIDL::GetInterfacePointer4(REFIID riid, void** ppv)
- {
- return QueryInterface(riid, (void**)ppv);
- }
- ULONG CTestIDL::AddRef()
- {
- cout << "Component: CTestIDL::AddRef() m_cRef = " << m_cRef + 1 << endl;
- return ++m_cRef;
- }
- ULONG CTestIDL::Release()
- {
- cout << "Component: CTestIDL::Release() m_cRef = " << m_cRef - 1 << endl;
- if(--m_cRef != 0)
- return m_cRef;
- SetEvent(g_hEvent); // ADD THIS!!!
- delete this;
- return 0;
- }
- HRESULT CTestIDL::QueryInterface(REFIID riid, void** ppv)
- {
- if(riid == IID_IUnknown)
- {
- cout << "Component: CTestIDL::QueryInterface() for IUnknown returning " << this << endl;
- *ppv = reinterpret_cast<IUnknown*>(this);
- }
- else if(riid == IID_IPointerTest)
- {
- cout << "Component: CTestIDL::QueryInterface() ASKING FOR IPointerTest " << this << endl;
- *ppv = (IPointerTest*)this;
- }
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- 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) { }
- ~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;
- CTestIDL *pTestIDL = new CTestIDL;
- cout << "Component: CFactory::CreateInstance() " << pTestIDL << endl;
- if(pTestIDL == NULL)
- return E_OUTOFMEMORY;
- // QueryInterface probably for IID_IUNKNOWN
- HRESULT hr = pTestIDL->QueryInterface(riid, ppv);
- pTestIDL->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);
- RegisterServer("component.exe", CLSID_PointerTest, "Test IDL Sample", "Component.InterfacePointer", "Component.InterfacePointer.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_PointerTest, "Component.InterfacePointer", "Component.InterfacePointer.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_PointerTest, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
- g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- WaitForSingleObject(g_hEvent, INFINITE);
- CoRevokeClassObject(dwRegister);
- pClassFactory->Release();
- CoUninitialize();
- _getch();
- }