How to use safe arrays in MFC Automation (140202)



The information in this article applies to:

  • Microsoft OLE Libraries
  • Microsoft Visual C++, 32-bit Editions 4.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 5.0
  • Microsoft Visual C++, 32-bit Enterprise Edition 6.0
  • Microsoft Visual C++, 32-bit Professional Edition 5.0
  • Microsoft Visual C++, 32-bit Professional Edition 6.0
  • Microsoft Visual C++, 32-bit Learning Edition 6.0

This article was previously published under Q140202

SUMMARY

MFCArray is an MFC OLE Automation server application that demonstrates the use of safe arrays. The array is passed to and returned from automation methods in a VARIANT.

Microsoft Visual C++ 5.0 was used to create the automation server. Vb.vbp and Vb.frm in the sample are Visual Basic 5.0 files that can be used to control the automation server.

Note The sample also works in Visual C++ 6.0 and in Visual Basic 6.0.

The following file is available for download from the Microsoft Download Center:
For more information about how to download Microsoft support files, click the following article number to view the article in the Microsoft Knowledge Base:

119591 How to obtain Microsoft support files from online services

Microsoft scanned this file for viruses. Microsoft used the most current virus-detection software that was available on the date that the file was posted. The file is stored on security-enhanced servers that help prevent any unauthorized changes to the file.

After downloading the file, use the following command to extract the sample and build the appropriate directory structure:

MFCArray.exe -d

MORE INFORMATION

MFCArray implements the following methods:
  • SlowSort: Sorts an input safearray of BSTRs and returns the sorted array and the time required for the sort. The array is passed by reference because it needs to be modified by the sort. SlowSort uses SafeArrayGetElement and SafeArrayPutElement to gain access to the array elements.
  • FastSort: Sorts an input safearray of BSTRs as does SlowSort but uses SafeArrayAccessData to get a pointer to the array elements. This allows direct access to the array elements instead of using SafeArrayGetElement and SafeArrayPutElement. This accounts for the speed improvement over SlowSort.
  • Average: Finds the average of an input safe array of integers. The array is not passed by reference.
  • GetArray: Creates and returns a safe array of BSTRs.

REFERENCES

For more information and an example of referencing the values that are in a VARIANT that contains a SAFEARRAY in an MFC client, click the following article number to view the article in the Microsoft Knowledge Base:

167668 How to pass a SafeArray of strings in a VARIANT* between Visual C++ and Visual Basic 6.0


Modification Type:MajorLast Reviewed:9/1/2005
Keywords:kbdownload kbAutomation kbfile kbhowto kbSample KB140202 kbAudDeveloper