Convert SafeArray to managed array

M

Maxim

Hi all,

I'm calling a COM Interface method that returnes SafeArray wrapped into
variant. Is it possible to convert it to managed array? Because working with
SAFEARRAY directly is a bit complicated. Or maybe there is a managed wrapper
class for safe array?

Thanks in advance.
 
B

Brian Muth

Maxim said:
Hi all,

I'm calling a COM Interface method that returnes SafeArray wrapped into
variant. Is it possible to convert it to managed array? Because working
with SAFEARRAY directly is a bit complicated. Or maybe there is a managed
wrapper class for safe array?

A SAFEARRAY of what? Usually the COM Interop that is normally generated when
you references the COM object can convert SAFEARRAY parameters to their
managed counterpart. For example a SAFEARRAY of elements of type VT_I2 will
convert to short[], etc.

Brian
 
M

Maxim

Thank you for your reply, Brian.

The point is that I'm using #import directive, to generate wrappers for COM
interafces. This is because there are some problems with using Add
Reference. So after importing I have unmanaged wrappers that make use of
SAFEARRAY. So now I want to convert them to managed classes to simplify the
usage.
BTW I need to convert SAFEARRAY of BSTRs


Brian Muth said:
Maxim said:
Hi all,

I'm calling a COM Interface method that returnes SafeArray wrapped into
variant. Is it possible to convert it to managed array? Because working
with SAFEARRAY directly is a bit complicated. Or maybe there is a managed
wrapper class for safe array?

A SAFEARRAY of what? Usually the COM Interop that is normally generated
when you references the COM object can convert SAFEARRAY parameters to
their managed counterpart. For example a SAFEARRAY of elements of type
VT_I2 will convert to short[], etc.

Brian
 
B

Brian Muth

Maxim said:
Thank you for your reply, Brian.

The point is that I'm using #import directive, to generate wrappers for
COM interafces. This is because there are some problems with using Add
Reference. So after importing I have unmanaged wrappers that make use of
SAFEARRAY. So now I want to convert them to managed classes to simplify
the usage.
BTW I need to convert SAFEARRAY of BSTRs

In that case you can simply use the unmanaged code to fetch the SAFEARRAY
and directly extract the BSTR members. Converting from BSTR to String^ is a
very straightforward step that you can google for if you don't know how.

Brian
 
M

Maxim

In that case you can simply use the unmanaged code to fetch the SAFEARRAY
and directly extract the BSTR members. Converting from BSTR to String^ is
a very straightforward step that you can google for if you don't know how.

That's the way I'm doing it now. But fetching of multidimentional SAFEARRAY
is a little bit clumsy, so I thought that there is a more effective way.
 
B

Brian Muth

Maxim said:
That's the way I'm doing it now. But fetching of multidimentional
SAFEARRAY is a little bit clumsy, so I thought that there is a more
effective way.

You might take a look at the CComSafeArray class, although personally I
never use it.

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top