Use arrays in COM

zhaozj2021-02-16  74

I don't want to write this article, because using an array in COM is nothing more than the use of SafeArray, and the MSDN can be. But I saw a lot of people asking this question, I think it is better to imitate it for everyone. Everyone see MSDN is not so hard.

The use of two arrays is given in the code, one is a string array, and the other is a custom data structure array.

First, the string array

Key code: Components:

STDMETHODIMP CTestArray :: Show1 (SAFEARRAY * pSa) {// TODO: Add your implementation code here if (! PSa = NULL) {VARTYPE vt; SafeArrayGetVartype (pSa, & vt); // array element type is determined if (vt = VT_BSTR! RETURN S_FALSE; Long Llbound, Lubound; BSTR * PBSTR; Char Strtext [1024] = "", Strtemp [100]; SafeArraygetLbound; // One - dimensional array subscript SafeArraygetubound (PSA, 1, & Lubound) (PSA, 1, & Lubound) ); // One-dimensional array subscript SafearrayAccessData (PSA, (void **) & pbstr); for (long (long i = llbound; i <= lubound; i ) {WCSTombs (strTemp, * (PBSTR I), 100); STRCAT (STRTEXT, STRTEMP);} SafeArrayunAccessData (PSA); :: MessageBox (Null, Strtext, "Use Array IN COM DLL", MB_OK); Return S_OK;} Return S_FALSE;}

client:

Void ctestdlg :: OnButton1 () {Uses_conversion; // Want to use T2ole to add this Coinitialize (null); PTR.CREATEINSTANCE (__ uuidof (testArray); SafeArrayBound PSAB [1]; PSAB [0]. lLbound = 0; pSab [0] .cElements = 3; SAFEARRAY * pSa; pSa = SafeArrayCreate (VT_BSTR, 1, pSab); BSTR * pBstr; SafeArrayAccessData (pSa, (void **) & pBstr); * (pBstr) = SysAllocString (T2OLE ("BSTR1")); // A2W * (PBSTR 1) = sysallocstring (T2OLE ("BSTR2")); * (PBSTR 2) = sysallocstring (t2ole ("BSTR3"); SafearRayunAccessData PSA); PTR-> Show1 (PSA); SafeArrayDestroy (PSA); PTR.RELEASE (); couninitialize ();

Second, custom data structural arrays

Key code: Components:

STDMETHODIMP CTestArray :: Show2 (SAFEARRAY * pSa) {if (pSa = NULL!) {VARTYPE vt; SafeArrayGetVartype (pSa, & vt); // array element type is determined if (vt = VT_RECORD!) // VT_RECORD type return S_FALSE; long Llbound, Lubound; Student * Pstudent; Char Strtext [4096] = "" ", Strtemp [100]; SafeArraygetLbound; // One - dimensional array subscript SafeArraygetubound (PSA, 1, & Lubound); // Dimensional group SafearrayAccessData (PSA, (void **) & pstudent; for (long (long i = llbound; i <= lubound; i ) {wcstombs (strtemp, (* (pstudent i)). Name, 100); strcat (Strtext, "Name ="); strcat (strcat (strtext, "/ n"); sprintf (strTemp, "grade ="% d / n "," (* (PStudent i)). Grade); strcat (strcat, strTemp); if (* (pstudent i)). SEX = "=" 0) STRCPY (strTemp, "Male / N"); Else Strcpy (Strtemp, "Female / N") STRCAT (Strtext, "SEX ="); strcat (strcat, strtemp); if (* (pstudent i)). graduate) STRCPY (Strtemp, "True / N"); Else Strcpy (strTemp, "False / n "); strcat (strcat," graduate: "); strcat (strcat (strcat); strcat (strcat," / n ");} SafeArrayunAccessData (PSA); :: MessageBox (null, strtext , "Use array in com dll", MB_OK); Return S_ok;} Return S_FALSE;} Client:

void CTestDlg :: OnButton2 () {const GUID GUID_STUDENT = "{0xF7D09422,0xB295,0x11d4, {0x98,0xDB, 0x00,0x80,0xC8,0xF5,0xB2,0xE4}};" CoInitialize (NULL); ITypeInfo * pTypeInfo = NULL ; ITypeLib * pTypelib = NULL; IRecordInfo * pRecInfo = NULL; SAFEARRAY * psaStudent = NULL; SAFEARRAYBOUND rgbounds = {4, 0}; STUDENT * pStudentStruct = NULL; ITestArrayPtr ptr; LoadRegTypeLib (LIBID_USEARRAYLib, 1, 0, GetUserDefaultLCID (), & pTypelib ); pTypelib-> GetTypeInfoOfGuid (GUID_STUDENT, & pTypeInfo); GetRecordInfoFromTypeInfo (pTypeInfo, & pRecInfo); pTypeInfo-> Release (); pTypelib-> Release (); // can be used to identify the data structure VT_RECORD psaStudent = SafeArrayCreateEx (VT_RECORD, 1, & rgbounds, pRecInfo); pRecInfo-> Release (); SafeArrayAccessData (psaStudent, (void **) (& pStudentStruct)); pStudentStruct [0] .grade = 3; pStudentStruct [0] .name = SysAllocString (L "Lostall"); L @ ​​a simpler pStudentStruct [0] .sex = male; pStudentStruct [0] .graduate = true; pStudentStruct [1] .grade = 8; pStudentStruct [1] .name = SysAllocString (L "Nicesnower"); pStudentStruct [ 1] .SE x = female; pStudentStruct [1] .graduate = false; pStudentStruct [2] .grade = 12; pStudentStruct [2] .name = SysAllocString (L "Mike"); pStudentStruct [2] .sex = male; pStudentStruct [2] .graduate = true; pStudentStruct [3] .grade = 3; pStudentStruct [3] .name = SysAllocString (L "Linda"); pStudentStruct [3] .sex = female; pStudentStruct [3] .graduate = false; SafeArrayUnaccessData (psaStudent ); ptr.createinstance; ptr-> show2 (psastudent); ptr.release (); couninitialize ();} Download sample code (55KB)

Related Technology:

Mutual conversion between BSTR strings and general strings

Related reference:

User-defined data types in COM components

* This article is selected from: COM concentration

转载请注明原文地址:https://www.9cbs.com/read-15800.html

New Post(0)