CompCtrl.cpp
上传用户:biuytresa
上传日期:2007-12-07
资源大小:721k
文件大小:2k
源码类别:

DNA

开发平台:

Visual C++

  1. // CompCtrl.cpp : Defines the entry point for the console application.
  2. //
  3. #include "stdafx.h"
  4. #include "windows.h"
  5. #include <stdio.h>
  6. #include <comutil.h>
  7. #include "SomeIFace.h"
  8. #include "OtherIFace.h"
  9. // {16DCB982-BEEB-11d2-B362-00104B08CC22}
  10. extern "C" const GUID IID_SomeInterface = 
  11. { 0x16dcb982, 0xbeeb, 0x11d2, 
  12. { 0xb3, 0x62, 0x0, 0x10, 0x4b, 0x8, 0xcc, 0x22 } };
  13. // {39C07942-BFCC-11d2-A100-00A0C9A6F472}
  14. extern "C" const GUID IID_OtherInterface = 
  15. { 0x39c07942, 0xbfcc, 0x11d2, 
  16. { 0xa1, 0x0, 0x0, 0xa0, 0xc9, 0xa6, 0xf4, 0x72 } };
  17. int main(int argc, char* argv[])
  18. {
  19. IUnknown *pUnknown;
  20. ISomeInterface *pSomeInterface;
  21. IOtherInterface *pOtherInterface;
  22. HRESULT hResult;
  23. if (CoInitialize(NULL) != S_OK) {
  24. printf("Initialize COM library failed!n");
  25. return -1;
  26. }
  27. GUID compBCLSID;
  28. hResult = ::CLSIDFromProgID(L"CompB.Object", &compBCLSID);
  29. if (FAILED(hResult)) 
  30. {
  31. printf("Can't find the CompB CLSID!n");
  32. CoUninitialize();
  33. return -2;
  34. }
  35. hResult = CoCreateInstance(compBCLSID, NULL, 
  36. CLSCTX_INPROC_SERVER, IID_IUnknown, (void **)&pUnknown);
  37. if (FAILED(hResult)) 
  38. {
  39. printf("Create object failed!n");
  40. CoUninitialize();
  41. return -2;
  42. }
  43. hResult = pUnknown->QueryInterface(IID_SomeInterface, (void **)&pSomeInterface);
  44. pUnknown->Release();
  45. if (FAILED(hResult)) 
  46. {
  47. printf("QueryInterface ISomeInterface failed!n");
  48. CoUninitialize();
  49. return -3;
  50. }
  51. pSomeInterface->SomeFunction();
  52. hResult = pSomeInterface->QueryInterface(IID_OtherInterface, (void **)&pOtherInterface);
  53. pSomeInterface->Release();
  54. if (FAILED(hResult)) 
  55. {
  56. printf("QueryInterface IOtherInterface failed!n");
  57. CoUninitialize();
  58. return -4;
  59. }
  60. pOtherInterface->OtherFunction();
  61. pOtherInterface->Release();
  62. CoUninitialize();
  63. return 0;
  64. }