client.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
- // client.cpp
- #define _WIN32_DCOM
- #include <iostream.h> // For cout
- #include "Component with FTMcomponent.h" // Generated by MIDL
- DWORD threadid;
- void __stdcall MyThread(IStream* pScream)
- {
- HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
- if(FAILED(hr))
- cout << "CoInitializeEx failed" << endl;
- ISum* pSum;
- hr = CoGetInterfaceAndReleaseStream(pScream, IID_ISum, (void**)&pSum);
- if(FAILED(hr))
- cout << "CoGetInterfaceAndReleaseStream failed" << endl;
- cout << "Adding numbers 0 to 50000 in MTA thread " << GetCurrentThreadId() << endl;
- for(int count = 0; count < 50000; count++)
- {
- int sum;
- hr = pSum->Sum(count, count, &sum);
- // cout << "Client: Calling Sum(" << count << ", " << count << ") = " << sum << endl;
- }
- pSum->Release();
- CoUninitialize();
- PostThreadMessage(threadid, WM_QUIT, 0, 0);
- }
- void main()
- {
- DWORD time = GetTickCount();
- threadid = GetCurrentThreadId();
- HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
- if(FAILED(hr))
- cout << "CoInitializeEx failed" << 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 newthreadid;
- HANDLE thread_handle = CreateThread(0, 0, (LPTHREAD_START_ROUTINE)MyThread, (void*)pStream, 0, &newthreadid);
- cout << "Adding numbers 0 to 50000 in STA thread " << GetCurrentThreadId() << endl;
- for(int count = 0; count < 50000; count++)
- {
- int sum = 0;
- hr = pSum->Sum(count, count, &sum);
- // cout << "Client: Calling Sum(" << count << ", " << count << ") = " << sum << endl;
- }
- hr = pSum->Release();
- MSG msg;
- while(GetMessage(&msg, 0, 0, 0))
- DispatchMessage(&msg);
-
- CoUninitialize();
- cout << "Time elapsed = " << GetTickCount() - time << " milliseconds." << endl;
- }