Article ID: 167668 - View products that this article applies to.
This article was previously published under Q167668
Note Microsoft Visual C++ .NET (2002) supports both the managed code model that is provided by the Microsoft .NET Framework and the unmanaged native Microsoft Windows code model. The information in this article applies only to unmanaged Visual C++ code.
When you look at the information available on passing SafeArrays between Visual C++ and Visual Basic 6.0, most pass the SafeArray in a parameter of type VARIANT. In MFC, this translates into a "const VARIANT& varName" parameter.
As an alternative, this article explores how to pass a VARIANT*, which in MFC will be declared as a "VARIANT FAR* varName" parameter.
Why should you use one method over the other? Because most applications allow modifications to a SafeArray passed in a VARIANT, but not all. In Excel 95, you can pass a SafeArray in a VARIANT to a Visual C++ server, modify the values in the server, return the modified SafeArray, and there is no change in Excel 95. Another reason to use this technique is to work around a limitation in Visual Basic 6.0 when you use early binding.
To demonstrate this concept, start with a new "MFC AppWizard(exe)" project called StrArray. In "Step 3 of 6," select Automation, click Finish, and then click OK to generate the project files.
NOTE: If using Visual Studio.NET: Create a new "MFC Application" project called StrArray. On the Advanced Features tab, select Automation, and then click Finish to generate the project files.
Now create a single automation method using ClassWizard. In ClassWizard, select "CStrArrayDoc" under "Class name:". Click the Automation tab and click Add Method. Enter the following values into the Add Method dialog box:
NOTE: If using Visual Studio.NET: Add a single automation method using "Class View". Expand the Class View tree and look for the VCCOdeIDLLibrary entry. Find the IStrArray interface under that node. Right-click the interface, and then select Add Method. Enter the following values into the Add Method dialog box:
External Name : Sort Internal Name : Sort Return Type : long Parameter List: Name : vArray Type : VARIANT*
Click OK and Edit Code.
To implement the Sort method, use the FastSort method of the MFCARRAY sample mentioned in the REFERENCES section below. This allows you to see the differences between passing the parameter as a "const VARIANT& varName" and a "VARIANT* varName". Modify the Sort method as follows:
Now you can build and test the automation server. After you build the server, run it as a stand-alone to register it. Run Visual Basic 6.0 and create a new Visual Basic 6.0 project. Place a button on the form and modify the handler to match the code below:
The key to getting this syntax to work in Visual Basic 6.0 is to first Dim the array variable as a variant, and then ReDim it so that it becomes a variant containing an array of strings. Now you are ready to test the server. Set break points in the Visual Basic 6.0 code before and after the call to Sort. Run the Visual Basic 6.0 application, click Command1 and use the watch window to check the values for v.
For more information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
(http://support.microsoft.com/kb/122287/ )Limits of Visual Basic 3.0 & Disptest as automation controllers
(http://support.microsoft.com/kb/131046/ )Binary.exe transfers binary data using OLE automation
(http://support.microsoft.com/kb/131086/ )Use of safe arrays in automation
(http://support.microsoft.com/kb/140202/ )MFCArray using safe arrays in MFC automation
(http://support.microsoft.com/kb/122289/ )Passing structures in OLE automation
154172Microsoft Systems Journal, June 1996, "Q&A OLE" by Don Box.
(http://support.microsoft.com/kb/154172/ )How to pass binary data between an ActiveX control and VB
(c) Microsoft Corporation 1997, All Rights Reserved. Contributions by Shawn W. Karr, Microsoft Corporation.
Article ID: 167668 - Last Review: November 21, 2006 - Revision: 3.1