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 number key code: Component: stdmethodimp ctestarray :: show1 (SafeArray * psa) {// Todo: add your importation code Hereif (PSA! = Null) {Vartype vt; SafeArraygetvartype (PSA, & VT); // Discriminate array element type 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); // One-dimensional array is lacquered SafearrayAccessData (PSA, (Void **) & PBSTR);
For (long i = llbound; i <= lubound; i ) {wcstombs (strtemp, * (pbstr i), 100); strcat (strcat, 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 sentence
Coinitialize (NULL);
ITESTARRAYPTR PTR; 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; SafeAaCcessData (PSA, (void **) & pbstr); * (PBSTR) = sysallocstring (t2ole ("bSTR1")); // can also be used 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 array key code: Component: STDMETHODIMP CTESTARRAY: Show2 (SafeArray * PSA) {if (PSA! = NULL) {Vartype Vt; SafeArraygetvartype (PSA, & VT); // Discriminate array element type 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); // One-dimensional array of SafeAccessData (PSA, (void **) & pstudent);
For (long i = llbound; i <= lubound; i ) {wcstombs (strtemp, (* (pstudent i)). name, 100); strcat (strcat, "name ="); strcat (strcat, strtemp); STRCAT STRCAT (STRTEXT, "/ N"); sprintf (strTemp, "grade ="% d / n "," (* (pstudent i))); strcat (strcat (strcat, strtemp); if ((* (PStudent) i)). SEX = "=" 0) STRCPY (strTemp, "Male / N"); Else STRCPY (Strtemp, "Female / N"); strcat (strcat, "SEX ="); strcat (Strtext, Strtemp ); if (* (pstudent i)). graduate) STRCPY (Strtemp, "True / N"); Else Strcpy (Strtemp, "False / N"); strcat (Strtext, "Graduate:"); strcat StRText, Strtemp; Strcat (Strtext, "/ N");} SafeArrayunAccessData (PSA); :: MessageBox (Null, Strtext, "Use Array IN COM DLL", MB_OK); Return S_OK;} Return S_false;} customer : 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 rgbons = "{" 4, 0}; student * pstudentstruc t = "NULL;" ITestArrayPtr ptr; LoadRegTypeLib (LIBID_USEARRAYLib, 1, 0, GetUserDefaultLCID (), & pTypelib); pTypelib-> GetTypeInfoOfGuid (GUID_STUDENT, & pTypeInfo); GetRecordInfoFromTypeInfo (pTypeInfo, & pRecInfo); pTypeInfo-> Release (); pTypelib- > Release (); // Only use VT_Record to identify data structures psastudent = SafeArrayCreateex (VT_Record, 1, & rgbounds, precinfo); Precinfo-> Release ();
SafeArrayAccessData (psaStudent, (void **) (& pStudentStruct)); pStudentStruct [0] .grade = 3; pStudentStruct [0] .name = SysAllocString (L "Lostall"); // use simpler pStudentStruct L [0] .sex = male; pStudentStruct [0] .graduate = true; pStudentStruct [1] .grade = 8; pStudentStruct [1] .name = SysAllocString (L "Nicesnower"); pStudentStruct [1] .sex = 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 (CLSID_TestArray); Ptr-> Show2 (PSAstudent); Ptr.Release (); counitialize ();
Download Sample Code (55KB)