SafeArray 的使用

 

1.  void CTestDlg::OnButton1()    

2.  {   

3.      // TODO: Add your control notification handler code here    

4.      USES_CONVERSION;//想用T2OLE就必须加上这一句    

5.     

6.      CoInitialize(NULL);   

7.     

8.      ITestArrayPtr   ptr;   

9.      ptr.CreateInstance(__uuidof(TestArray));   

10.    

11.     SAFEARRAYBOUND pSab[1];   

12.     pSab[0].lLbound =   0;   

13.     pSab[0].cElements=  3;   

14.        

15.     SAFEARRAY   *pSa;   

16.     pSa =   SafeArrayCreate(VT_BSTR, 1, pSab);   

17.    

18.     BSTR    *pBstr;   

19.     SafeArrayAccessData(pSa, (void**)&pBstr);   

20.     *(pBstr)    =   SysAllocString(T2OLE("BSTR1 "));//也可用A2W    

21.     *(pBstr+1)  =   SysAllocString(T2OLE("BSTR2 "));   

22.     *(pBstr+2)  =   SysAllocString(T2OLE("BSTR3 "));   

23.     SafeArrayUnaccessData(pSa);   

24.    

25.     ptr->Show1(pSa);   

26.    

27.     SafeArrayDestroy(pSa);   

28.    

29.     ptr.Release();   

30.    

31.     CoUninitialize();   

32. }   

33.    

34. //UDT Array    

35. void CTestDlg::OnButton2()    

36. {   

37.     // TODO: Add your control notification handler code here    

38.     const GUID GUID_STUDENT=   

39.     {0xF7D09422,0xB295,0x11d4,{0x98,0xDB,0x00,0x80,0xC8,0xF5,0xB2,0xE4}};   

40.    

41.     CoInitialize(NULL);   

42.    

43.     ITypeInfo   *pTypeInfo  = NULL;   

44.     ITypeLib    *pTypelib   = NULL;   

45.     IRecordInfo *pRecInfo   = NULL;   

46.     SAFEARRAY   *psaStudent = NULL;   

47.     SAFEARRAYBOUND rgbounds = { 4, 0 };   

48.     STUDENT  *pStudentStruct= NULL;   

49.     ITestArrayPtr   ptr;   

50.    

51.     LoadRegTypeLib(LIBID_USEARRAYLib, 1, 0, GetUserDefaultLCID(), &pTypelib);   

52.     pTypelib->GetTypeInfoOfGuid(GUID_STUDENT, &pTypeInfo);   

53.     GetRecordInfoFromTypeInfo(pTypeInfo, &pRecInfo);   

54.     pTypeInfo->Release();   

55.     pTypelib->Release();   

56.    

57.     psaStudent = SafeArrayCreateEx(VT_RECORD, 1, &rgbounds, pRecInfo);   

58.     pRecInfo->Release();   

59.    

60.     SafeArrayAccessData(psaStudent, (void**)(&pStudentStruct));   

61.     pStudentStruct[0].grade = 3;   

62.     pStudentStruct[0].name = SysAllocString(L"Lostall");//L更简单    

63.     pStudentStruct[0].sex = male;   

64.     pStudentStruct[0].graduate = true;   

65.     pStudentStruct[1].grade = 8;   

66.     pStudentStruct[1].name = SysAllocString(L"Nicesnower");   

67.     pStudentStruct[1].sex = female;   

68.     pStudentStruct[1].graduate = false;   

69.     pStudentStruct[2].grade = 12;   

70.     pStudentStruct[2].name = SysAllocString(L"Mike");   

71.     pStudentStruct[2].sex = male;   

72.     pStudentStruct[2].graduate = true;   

73.     pStudentStruct[3].grade = 3;   

74.     pStudentStruct[3].name = SysAllocString(L"Linda");   

75.     pStudentStruct[3].sex = female;   

76.     pStudentStruct[3].graduate = false;   

77.     SafeArrayUnaccessData(psaStudent);   

78.    

79.     ptr.CreateInstance(CLSID_TestArray);   

80.     ptr->Show2(psaStudent);   

81.     ptr.Release();   

82.        

83.     CoUninitialize();   

84. }

 

 

 

 

 

  

1.    

2.  // CTestArray    

3.  STDMETHODIMP CTestArray::Show1(SAFEARRAY *pSa)   

4.  {   

5.      // TODO: Add your implementation code here    

6.      if  (pSa !=NULL)   

7.      {   

8.          VARTYPE vt;   

9.          SafeArrayGetVartype(pSa, &vt);//判别数组元素类型    

10.         if  (vt != VT_BSTR)   

11.             return  S_FALSE;   

12.    

13.         long    lLBound, lUBound;   

14.         BSTR    *pBstr;   

15.         char    strText[1024]="", strTemp[100];   

16.            

17.         SafeArrayGetLBound(pSa, 1, &lLBound);//一维数组下标    

18.         SafeArrayGetUBound(pSa, 1, &lUBound);//一维数组上标    

19.         SafeArrayAccessData(pSa, (void**)&pBstr);   

20.    

21.         for (long i=lLBound; i<=lUBound; i++)   

22.         {   

23.             wcstombs(strTemp, *(pBstr+i), 100);   

24.             strcat(strText, strTemp);   

25.         }   

26.         SafeArrayUnaccessData(pSa);   

27.         ::MessageBox(NULL, strText, "Use Array in COM DLL", MB_OK);   

28.    

29.         return S_OK;   

30.     }   

31.     return  S_FALSE;   

32. }   

33.    

34.    

35. STDMETHODIMP CTestArray::Show2(SAFEARRAY *pSa)   

36. {   

37.     // TODO: Add your implementation code here    

38.     if  (pSa !=NULL)   

39.     {   

40.         VARTYPE vt;   

41.         SafeArrayGetVartype(pSa, &vt);//判别数组元素类型    

42.         if  (vt != VT_RECORD)   //VT_RECORD    

43.             return  S_FALSE;   

44.    

45.         long    lLBound, lUBound;   

46.         STUDENT *pStudent;   

47.         char    strText[4096]="", strTemp[100];   

48.            

49.         SafeArrayGetLBound(pSa, 1, &lLBound);//一维数组下标    

50.         SafeArrayGetUBound(pSa, 1, &lUBound);//一维数组上标    

51.         SafeArrayAccessData(pSa, (void**)&pStudent);   

52.    

53.         for (long i=lLBound; i<=lUBound; i++)   

54.         {   

55.             wcstombs(strTemp, (*(pStudent+i)).name, 100);   

56.             strcat(strText, "name=");   

57.             strcat(strText, strTemp);   

58.             strcat(strText, "\n");   

59.             sprintf(strTemp, "grade=%d\n", (*(pStudent+i)).grade);   

60.             strcat(strText, strTemp);   

61.             if  ((*(pStudent+i)).sex == 0)   

62.                 strcpy(strTemp, "male\n");   

63.             else   

64.                 strcpy(strTemp, "female\n");   

65.             strcat(strText, "sex=");   

66.             strcat(strText, strTemp);   

67.             if  ((*(pStudent+i)).graduate)   

68.                 strcpy(strTemp, "true\n");   

69.             else   

70.                 strcpy(strTemp, "false\n");   

71.             strcat(strText, "graduate:");   

72.             strcat(strText, strTemp);   

73.             strcat(strText, "\n");   

74.         }   

75.         SafeArrayUnaccessData(pSa);   

76.         ::MessageBox(NULL, strText, "Use Array in COM DLL", MB_OK);   

77.    

78.         return S_OK;   

79.     }   

80.     return  S_FALSE;   

81. }   

转载于:https://www.cnblogs.com/livenn/archive/2009/11/04/1595686.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值