Link to home
Start Free TrialLog in
Avatar of mgesiot
mgesiot

asked on

Reciving PSafeArray nad OleVariant Data from COM ....

Hi Experts.
I'm building an OLE Automation Server that interfaces Excel XP :-(  
Anyway.
I Recieve/Send one type of data that I don't know how to handle :

var Strings : PSafeArray
========================

PSafeArray is a pointer to a SafeArray.
In this case it points to an array of Strings
(The VB code is [strings() As Variant])
The array dimensions are [0..1, 0..x] where x is
the number of items sent.
In one case I need to read the array, in the second I need
to write it.
I DONT KNOW WHERE TO START FROM

Any help on how to read and write this structure will be appreciated.

Best Regards
Marco
Avatar of AvonWyss
AvonWyss
Flag of Switzerland image

Try this:

function SafeArrayToVariant(SaveArray: PSafeArray): OleVariant;
begin
     TVarData(Result).VType:=varArray;
     TVarData(Result).VArray:=PVarArray(SaveArray);
end;

[...]
var
     Strings: OleVariant;
     Temp: PSafeArray;
[...]
     {I assume that Temp holds the PSafeArray)
     Strings:=SafeArrayToVariant(Temp);
Btw. the other way around is like this:

function VariantToSafeArray(const Array: OleVariant): OleVariant;
begin
    Assert(TVarData(Arr).VType=varArray);
    Result:=PSafeArray(TVarData(Arr).VArray);
end;

And when it is in the Variant, you can just use it like a normal Pascal array...:

    Edit1.Text:=Strings[0];
ASKER CERTIFIED SOLUTION
Avatar of AvonWyss
AvonWyss
Flag of Switzerland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mgesiot
mgesiot

ASKER

Thanks!
Anyway I made it myself using
SAFEARREAYCREATE
SAFEARRAYGETDIM
SAFEARRAYGETUBOUND
SAFEARRAYPUTELEMENT
SAFEARRAYGETELEMENT

Api functions.
Now I wonder if I actually need all of this
or if the compiler takes charge of creating and
destroying the SafeArray.
I'll try your suggestions and let you know
within a couple of days.

Thanks again
Marco
Avatar of mgesiot

ASKER

Thanks a lot