client.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:2k
- // client.cpp
- #include <iostream.h>
- #include "Componentcomponent.h" // Generated by MIDL
- void main()
- {
- cout << "Client: Calling CoInitialize()" << endl;
- CoInitialize(NULL);
-
- IUnknown* pUnknown;
- IDispatch* pDispatch;
- cout << "Client: Calling CoCreateInstance()" << endl;
- CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&pUnknown);
- cout << "Client: Calling QueryInterface() for IDispatch on " << pUnknown << endl;
- pUnknown->QueryInterface(IID_IDispatch, (void**)&pDispatch);
- cout << "Client: Calling Release() for IUnknown on " << pUnknown << endl;
- pUnknown->Release();
- cout << "Client: Calling GetIDsOfNames" << endl;
- OLECHAR* name = L"Sum";
- DISPID dispid;
- pDispatch->GetIDsOfNames(IID_NULL, &name, 1, GetUserDefaultLCID(), &dispid);
- cout << "GetIDsOfNames(Sum) = " << dispid << endl;
-
- VARIANTARG test[2];
- VariantInit(&test[0]);
- test[0].vt = VT_I4;
- test[0].lVal = 7;
- VariantInit(&test[1]);
- test[1].vt = VT_I4;
- test[1].lVal = 2;
- VARIANT result;
- VariantInit(&result);
- DISPPARAMS param = { test, 0, 2, NULL };
- if(FAILED(pDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, ¶m, &result, NULL, NULL)))
- cout << "pDispatch->Invoke() failed" << endl;
- cout << test[0].lVal << " + " << test[1].lVal << " = " << result.lVal << endl;
- IDispatchEx* pDispEx;
- pDispatch->QueryInterface(IID_IDispatchEx, (void**)&pDispEx);
- DISPID new_dispid;
- HRESULT hr = pDispEx->GetDispID(L"NewMethod", fdexNameEnsure, &new_dispid);
-
- if(hr == DISP_E_UNKNOWNNAME)
- cout << "ERROR: DISP_E_UNKNOWNNAME" << endl;
- cout << "dispid = " << new_dispid << endl;
- test[0].lVal = 4;
- test[1].lVal = 3;
- hr = pDispEx->InvokeEx(new_dispid, LOCALE_USER_DEFAULT, DISPATCH_METHOD, ¶m, &result, NULL, NULL);
- cout << test[0].lVal << " + " << test[1].lVal << " = " << result.lVal << endl;
- pDispEx->GetDispID(L"Sum", 0, &new_dispid);
- cout << "Sum = " << new_dispid << endl;
- pDispEx->Release();
- pDispatch->Release();
- cout << "Client: Calling CoUninitialize()" << endl;
- CoUninitialize();
- }