client.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:3k
- // 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;
-
- // Using positional parameters
- VARIANTARG SumArgsPos[2];
- VariantInit(&SumArgsPos[0]);
- SumArgsPos[0].vt = VT_I4;
- SumArgsPos[0].lVal = 7;
- VariantInit(&SumArgsPos[1]);
- SumArgsPos[1].vt = VT_I4;
- SumArgsPos[1].lVal = 2;
- VARIANT result;
- VariantInit(&result);
- DISPPARAMS Params1 = { SumArgsPos, NULL, 2, 0 };
- if(FAILED(pDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, &Params1, &result, NULL, NULL)))
- cout << "pDispatch->Invoke() failed" << endl;
- cout << SumArgsPos[0].lVal << " + " << SumArgsPos[1].lVal << " = " << result.lVal << endl;
- // Using named arguments
- VARIANTARG SumArgsName[2];
- VariantInit(&SumArgsName[0]);
- SumArgsName[0].vt = VT_I4;
- SumArgsName[0].lVal = 5; // normally y
- SumArgsName[1].vt = VT_I4;
- SumArgsName[1].lVal = 7; // normally x
- DISPID DispIds[2] = { 0, 1 }; // y first, then x
- DISPPARAMS Params2 = { SumArgsName, DispIds, 2, 2 };
- if(FAILED(pDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, &Params2, &result, NULL, NULL)))
- cout << "pDispatch->Invoke() failed" << endl;
- cout << SumArgsName[0].lVal << " + " << SumArgsName[1].lVal << " = " << result.lVal << endl;
- // Using positional optional arguments
- VARIANTARG SumArgsOpt[2];
- VariantInit(&SumArgsOpt[0]);
- SumArgsOpt[0].vt = VT_ERROR;
- SumArgsOpt[0].scode = DISP_E_PARAMNOTFOUND; // normally y
- VariantInit(&SumArgsOpt[1]);
- SumArgsOpt[1].vt = VT_I4;
- SumArgsOpt[1].lVal = 3; // normally x
- DISPPARAMS Params3 = { SumArgsOpt, NULL, 2, 0 };
- if(FAILED(pDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, &Params3, &result, NULL, NULL)))
- cout << "pDispatch->Invoke() failed" << endl;
- cout << "Default + " << SumArgsOpt[1].lVal << " = " << result.lVal << endl;
- // Using named optional arguments
- VARIANTARG SumArg;
- VariantInit(&SumArg);
- SumArg.vt = VT_I4;
- SumArg.lVal = 5;
- DISPID DispId = 1; // The y parameter
- DISPPARAMS Params4 = { &SumArg, &DispId, 1, 1 };
- if(FAILED(pDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, &Params4, &result, NULL, NULL)))
- cout << "pDispatch->Invoke() failed" << endl;
- cout << "Default + " << SumArg.lVal << " = " << result.lVal << endl;
- pDispatch->Release();
- cout << "Client: Calling CoUninitialize()" << endl;
- CoUninitialize();
- }