LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Accessing LPSAFEARRAY elements in CVI 6.0

I'm working with an ActiveX Automation server generated in CVI 6.0 (using C), and I'm trying to pass arrays between the activex server and IE/Vbscript. The array argument is an input/output parameter in the particular server method call.

VBSCRIPT passes all arrays as Variants, and the array is received on the CVI input side as a pointer to an LPSAFEARRAY of Variants.

Using the CA_VARIANT functions, I am able to read out the array contents.

But I have been unable to successfully edit the array
and return it to IE/VBScript without generating errors in VBSCRIPT: either type mismatch or "invalid callee".

Most of the helper functions in CVI for safearrays create new safearrays ( such as CA_Array1DToSafeArray). I need to ed
it the array that has been passed in. I was trying to use the Windows SDK SafeArrayPutElement function to access elements in the SAFEARRAY, but these functions only work with SAFEARRAY and not OLE LPSAFERRAY types. I wasn't able to find a corresponding helper function in CVI.

Do you have any suggestions? This is basically what I'm currently doing:

1) Call to CA_VariantGetSafeArrayPtr to get the pointer to the LPSAFEARRAY

2)Call to CA_SafeArrayGet1DSize to get the size

3) Call to CA_SafeArrayTo1DArrayEx to create an ARRAY of Variants from the SAFEARRAY. Once in this form I can read the data.

After this, I have tried to edit the Array of Variants, convert back to a SAFEARRAY, and use CA_VariantSetSafeArrayPtr to pass the value back. But of course, this is trying to pass back a newly created array, and hence it fails.

How do I work with the LPSAFEARRAY contents from the passed in pointer?
0 Kudos
Message 1 of 4
(3,904 Views)
Hi Joe,

OK, so I took a look at this in CVI. You didn't post your server code so I'm not sure how you have your server function prototyped. It sounds like you are doing things correctly so I'm not sure where your problem is.

To my understanding though, you want a function with an input/output parameter that is a VARIANT with a safearray of VARIANTs inside it. Then you want to be able to read the data out of the safe array and send new data out.

If you are defining your server properly your function should have a VARIANT* for your input/output parameter. There should be no problem in setting a new safe array inside the VARIANT.

I wrote a test server that does this and attached it below. I also wrote a client program for the server that i
s also in the attachment. The client program sends in a VARIANT with a SAFEARRAY of VARIANTs inside that contain integers. The server puts up a popup to show it read the data and then sets the first integer value and returns.

Hopefully, this will illustrate where you have problems in your code.

Good luck,

Chris Matthews
National Instruments
0 Kudos
Message 2 of 4
(3,905 Views)
Chris,


Thanks so much for your detailed reply. I wound up making my code work, but I did so by accessing the actual memory locations of the safearray via pointers.

I'm going to take a close look at how you accessed the array elements and do some experimentation.
0 Kudos
Message 3 of 4
(3,904 Views)

Hi,

 

I had the same problem while passing an array from VBScript (written in Notepad) to my COM Server written as a Local Server (EXE).

 

My IDL file looks like this:

 

[id(2)] HRESULT ReadVar ([in] VARIANT *, [in, out] VARIANT *);

 

When I tried to access the array using CA_VariantGet1DArray() and CA_VariantSet1DArray() I got error codes from these functions.

 

I found out that the following works perfectly:

 

1) Call CA_VariantGetSafeArrayPtr() to get a pointer to the safearray inside the variant

2) Call regular Win32 safearray manipulation functions to access the safearray data

3) You can optionally call CA_SafeArrayTo1DArrayEx() with flag CVIAUTO_RETAIN_SAFEARRAY set so the safearray contents be kept inside the variant. You will get a C style array where you can play with the data.

 

Hope it helps

0 Kudos
Message 4 of 4
(3,467 Views)