client.cpp
上传用户:bjlvip
上传日期:2010-02-08
资源大小:744k
文件大小:1k
源码类别:

Windows编程

开发平台:

Visual C++

  1. // client.cpp
  2. #include <iostream.h>
  3. #include "Componentcomponent.h" // Generated by MIDL
  4. void main()
  5. {
  6. cout << "Client: Calling CoInitialize()" << endl;
  7. CoInitialize(NULL);
  8. IUnknown* pUnknown;
  9. IDispatch* pDispatch;
  10. cout << "Client: Calling CoCreateInstance()" << endl;
  11. CoCreateInstance(CLSID_InsideCOM, NULL, CLSCTX_INPROC_SERVER, IID_IUnknown, (void**)&pUnknown);
  12. cout << "Client: Calling QueryInterface() for IDispatch on " << pUnknown << endl;
  13. pUnknown->QueryInterface(IID_IDispatch, (void**)&pDispatch);
  14. cout << "Client: Calling Release() for IUnknown on " << pUnknown << endl;
  15. pUnknown->Release();
  16. cout << "Client: Calling GetIDsOfNames" << endl;
  17. OLECHAR* name = L"Sum";
  18. DISPID dispid;
  19. pDispatch->GetIDsOfNames(IID_NULL, &name, 1, GetUserDefaultLCID(), &dispid);
  20. cout << "GetIDsOfNames(Sum) = " << dispid << endl;
  21. VARIANTARG test[2];
  22. VariantInit(&test[0]);
  23. test[0].vt = VT_I4;
  24. test[0].lVal = 7;
  25. VariantInit(&test[1]);
  26. test[1].vt = VT_I4;
  27. test[1].lVal = 2;
  28. VARIANT result;
  29. VariantInit(&result);
  30. DISPPARAMS param = { test, 0, 2, NULL };
  31. if(FAILED(pDispatch->Invoke(dispid, IID_NULL, GetUserDefaultLCID(), DISPATCH_METHOD, &param, &result, NULL, NULL)))
  32. cout << "pDispatch->Invoke() failed" << endl;
  33. cout << test[0].lVal << " + " << test[1].lVal << " = " << result.lVal << endl;
  34. pDispatch->Release();
  35. cout << "Client: Calling CoUninitialize()" << endl;
  36. CoUninitialize();
  37. }