local.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:11k
- /*
- Bug in MIDL: Comment out the three references to _maxcount_test in the component_p.c file
- */
- // 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 ITest
- {
- public:
- // IUnknown
- ULONG __stdcall AddRef();
- ULONG __stdcall Release();
- HRESULT __stdcall QueryInterface(REFIID riid, void** ppv);
- // ITest
- HRESULT __stdcall SquareInteger(int myInteger, int* square);
- HRESULT __stdcall SumOfIntegers1(int myIntegers[5], int* sum);
- HRESULT __stdcall SumOfIntegers2(int cMax, int* myIntegers, int* sum);
- HRESULT __stdcall SumOfIntegers3(SUM_STRUCT* myIntegers, int* sum);
- HRESULT __stdcall SumOfIntegers4(int cFirst, int cActual, int* myIntegers[7], int* sum);
- HRESULT __stdcall ProduceIntegers1(int cMax, int* myIntegers);
- HRESULT __stdcall ProduceIntegers2(int* pcActual, int myIntegers[4096]);
- HRESULT __stdcall ProduceIntegers3(int cMax, int* pcActual, int* myIntegers);
- HRESULT __stdcall SendReceiveIntegers(int* pcActual, int myIntegers[4096]);
- HRESULT __stdcall SendString1(int cLength, wchar_t* myString);
- HRESULT __stdcall SendString2(wchar_t* myString);
- HRESULT __stdcall SendReceiveString1(wchar_t* myString);
- HRESULT __stdcall SendReceiveString2(int cMax, wchar_t* myString);
- HRESULT __stdcall SendReceiveString3(wchar_t** myString);
- HRESULT __stdcall SendInteger(int** test);
- HRESULT __stdcall SendIntegers1(int** test);
- HRESULT __stdcall SendIntegers2(int** test);
- HRESULT __stdcall SendIntegers3(int** test);
- CTestIDL() : m_cRef(1) { g_cComponents++; }
- ~CTestIDL() { cout << "Component: CTestIDL::~CTestIDL()" << endl, g_cComponents--; }
- private:
- ULONG m_cRef;
- };
- 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_ITest)
- {
- cout << "Component: CTestIDL::QueryInterface() for ITest returning " << this << endl;
- *ppv = (ITest*)this;
- }
- else
- {
- *ppv = NULL;
- return E_NOINTERFACE;
- }
- AddRef();
- return S_OK;
- }
- // HRESULT SquareInteger([in] int myInteger, [out] int* square);
- HRESULT CTestIDL::SquareInteger(int myInteger, int* square)
- {
- *square = myInteger * myInteger;
- return S_OK;
- }
- // HRESULT SumOfIntegers1([in] int myIntegers[5], [out] int* sum);
- HRESULT CTestIDL::SumOfIntegers1(int myIntegers[5], int* sum)
- {
- *sum = 0;
- for(int i = 0; i < 5; i++)
- *sum = *sum + myIntegers[i];
- return S_OK;
- }
- // HRESULT SumOfIntegers2([in] int cMax, [in, size_is(cMax)] int* myIntegers, [out] int* sum);
- HRESULT CTestIDL::SumOfIntegers2(int cMax, int* myIntegers, int* sum)
- {
- *sum = 0;
- for(int i = 0; i < cMax; i++)
- *sum = *sum + myIntegers[i];
- return S_OK;
- }
- /* typedef struct tagSUM_STRUCT
- {
- int cMax;
- [size_is(cMax)] int* myIntegers;
- } SUM_STRUCT; */
- // HRESULT SumOfIntegers3([in] SUM_STRUCT* myIntegers, [out] int* sum);
- HRESULT CTestIDL::SumOfIntegers3(SUM_STRUCT* myIntegers, int* sum)
- {
- *sum = 0;
- for(int i = 0; i < myIntegers->cMax; i++)
- *sum = *sum + myIntegers->myIntegers[i];
- return S_OK;
- }
- // HRESULT SumOfIntegers4([in] int cFirst, [in] int cActual, [in, first_is(cFirst), length_is(cActual)] int* myIntegers[7], [out] int* sum);
- HRESULT CTestIDL::SumOfIntegers4(int cFirst, int cActual, int* myIntegers[7], int* sum)
- {
- *sum = 3;
- for(int i = cFirst; i < cFirst + cActual; i++)
- {
- cout << "i = " << i << " cFirst = " << cFirst << " cActual = " << cActual << endl;
- cout << "myIntegers = " << myIntegers[i][0] << endl;
- // *sum = *sum + *myIntegers[i];
- }
- _getch();
- return S_OK;
- }
- // HRESULT ProduceIntegers1([in] int cMax, [out, size_is(cMax)] int* myIntegers);
- HRESULT CTestIDL::ProduceIntegers1(int cMax, int* myIntegers)
- {
- for(int i = 0; i < cMax; i++)
- myIntegers[i] = i;
- return S_OK;
- }
- // HRESULT ProduceIntegers2([out] int* pcActual, [out, length_is(*pcActual)] int myIntegers[4096]);
- HRESULT CTestIDL::ProduceIntegers2(int* pcActual, int myIntegers[4096])
- {
- *pcActual = 400;
- for(int i = 0; i < *pcActual; i++)
- myIntegers[i] = i;
- return S_OK;
- }
- // HRESULT ProduceIntegers3([in] int cMax, [out] int* pcActual, [out, size_is(cMax), length_is(*pcActual)] int* myIntegers);
- HRESULT CTestIDL::ProduceIntegers3(int cMax, int* pcActual, int* myIntegers)
- {
- *pcActual = cMax / 2;
- for(int i = 0; i < *pcActual; i++)
- myIntegers[i] = i;
- return S_OK;
- }
- // HRESULT SendReceiveIntegers([in, out] int* pcActual, [in, out, length_is(*pcActual)] int myIntegers[4096]);
- HRESULT CTestIDL::SendReceiveIntegers(int* pcActual, int myIntegers[4096])
- {
- // In
- cout << "SendReceiveIntegers: << " << *pcActual << " integers received" << endl;
- for(int i = 0; i < *pcActual; i++)
- cout << "SendReceiveIntegers: " << myIntegers[i] << endl;
- // Out
- *pcActual = 400;
- for(i = 0; i < *pcActual; i++)
- myIntegers[i] = i;
- return S_OK;
- }
- // HRESULT SendString1([in] int cLength, [in, size_is(cLength)] wchar_t* myString);
- HRESULT CTestIDL::SendString1(int cLength, wchar_t* myString)
- {
- wprintf(L"String of %d characters contains %sn", cLength, myString);
- return S_OK;
- }
- // HRESULT SendString2([in, string] wchar_t* myString);
- HRESULT CTestIDL::SendString2(wchar_t* myString)
- {
- wprintf(L"String contains %sn", myString);
- return S_OK;
- }
- // HRESULT SendReceiveString1([in, out, string] wchar_t* myString);
- HRESULT CTestIDL::SendReceiveString1(wchar_t* myString)
- {
- wprintf(L"SendReceiveString1: %sn", myString); // Hello COM
- // This will cause a fault!
- // wcscpy(myString, L"Nice weather today Nice weather today Nice weather today Nice weather today Nice weather today Nice weather today Nice weather today"); // Oh-oh
- // Nine characters max
- wcscpy(myString, L"only nine"); // OK
- return S_OK;
- }
- // HRESULT SendReceiveString2([in] int cMax, [in, out, string, size_is(cMax)] wchar_t* myString);
- HRESULT CTestIDL::SendReceiveString2(int cMax, wchar_t* myString)
- {
- wprintf(L"SendReceiveString2: %sn", myString); // Hello COM
- for(int i = 0; i < cMax - 1; i++)
- myString[i] = L'A';
- myString[cMax - 1] = L' ';
- return S_OK;
- }
- // HRESULT SendReceiveString3([in, out, string] wchar_t** myString);
- HRESULT CTestIDL::SendReceiveString3(wchar_t** myString)
- {
- wprintf(L"SendReceiveString3: %sn", *myString);
- CoTaskMemFree(*myString);
- wchar_t returnString[] = L"Nice weather today";
- *myString = (wchar_t*)CoTaskMemAlloc((wcslen(returnString)+1)*sizeof(wchar_t));
- wcscpy(*myString, returnString);
- return S_OK;
- }
- // HRESULT SendInteger([in] int** test);
- HRESULT CTestIDL::SendInteger(int** test)
- {
- cout << "SendInteger: test[0][0] = " << test[0][0] << endl;
- return S_OK;
- }
- // HRESULT SendIntegers1([in, size_is(, 2)] int** test);
- HRESULT CTestIDL::SendIntegers1(int** test)
- {
- cout << "SendIntegers1: test[0][0] = " << test[0][0] << endl;
- cout << "SendIntegers1: test[0][1] = " << test[0][1] << endl;
- return S_OK;
- }
- // HRESULT SendIntegers2([in, size_is(3, )] int** test);
- HRESULT CTestIDL::SendIntegers2(int** test)
- {
- cout << "SendIntegers2: test[0][0] = " << test[0][0] << endl;
- cout << "SendIntegers2: test[1][0] = " << test[1][0] << endl;
- cout << "SendIntegers2: test[2][0] = " << test[2][0] << endl;
- return S_OK;
- }
- // HRESULT SendIntegers3([in, size_is(3, 2)] int** test);
- HRESULT CTestIDL::SendIntegers3(int** test)
- {
- cout << "SendIntegers3: test[0][0] = " << test[0][0] << endl;
- cout << "SendIntegers3: test[0][1] = " << test[0][1] << endl;
- cout << "SendIntegers3: test[1][0] = " << test[1][0] << endl;
- cout << "SendIntegers3: test[1][1] = " << test[1][1] << endl;
- cout << "SendIntegers3: test[2][0] = " << test[2][0] << endl;
- cout << "SendIntegers3: test[2][1] = " << test[2][1] << endl;
- 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_TestIDL, "Test IDL Sample", "Component.TestIDL", "Component.TestIDL.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_TestIDL, "Component.TestIDL", "Component.TestIDL.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_TestIDL, pClassFactory, CLSCTX_LOCAL_SERVER, REGCLS_MULTIPLEUSE, &dwRegister);
- g_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
- WaitForSingleObject(g_hEvent, INFINITE);
- CoRevokeClassObject(dwRegister);
- pClassFactory->Release();
- CoUninitialize();
- _getch();
- }