client.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
- // client.cpp
- #define _WIN32_DCOM
- #include <iostream.h> // For cout
- #include "Componentcomponent.h" // Generated by MIDL
- void __stdcall MyThread(IStream* pScream)
- {
- HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
- if(FAILED(hr))
- cout << "CoInitializeEx failed" << endl;
- ISum* pSum;
- CoGetInterfaceAndReleaseStream(pScream, IID_ISum, (void**)&pSum);
- for(int count = 0; count < 10; count++)
- {
- int sum;
- hr = pSum->Sum(count, count, &sum);
- cout << "Client: Calling Sum(" << count << ", " << count << ") = " << sum << endl;
- }
- pSum->Release();
- CoUninitialize();
- }
- void main()
- {
- cout << "Client: Calling CoInitialize()" << endl;
- HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
- if(FAILED(hr))
- cout << "CoInitializeEx failed" << endl;
- cout << "Client: Calling CoCreateInstance()" << endl;
- ISum* pSum;
- hr = CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_ISum, (void**)&pSum);
- if(FAILED(hr))
- cout << "CoCreateInstance failed" << endl;
- IStream* pStream;
- CoMarshalInterThreadInterfaceInStream(IID_ISum, pSum, &pStream);
- DWORD threadid;
- HANDLE thread_handle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)MyThread, (void*)pStream, 0, &threadid);
- int sum;
- hr = pSum->Sum(2, 3, &sum);
- if(SUCCEEDED(hr))
- cout << "Client: Calling Sum(2, 3) = " << sum << endl;
- hr = pSum->Release();
- cout << "Client: Calling pSum->Release() reference count = " << hr << endl;
- WaitForSingleObject(thread_handle, INFINITE);
- cout << "Client: Calling CoUninitialize()" << endl;
- CoUninitialize();
- }