activeX array transfer

  • Thread starter Thread starter Owen Woo
  • Start date Start date
O

Owen Woo

My activeX control developped in VC++ has following method:
void AppendData(double* databuf)
How can I call this method in C# and transfer the databuf parameter to it?
 
This should be declared as:

void AppendData(double[] databuf);

The reason it should be declared like this is because if you declare it
as "ref double" then it will pass only the first item of the array.

If the method modifies the array in any way, then this won't work
either. Because this is a C-style array, it doesn't know what was modified
in the unmanaged realm (SAFEARRAYs don't have this problem) and can't just
marshal all of the memory in the array back.

If the contents are modified, then you will have to declare databuf as
an IntPtr and then allocate the memory yourself (using the static Alloc*
methods on the Marshal class). Then you write to the unmanaged memory block
and pass it to the method. On return from the method, you have to read the
unmanaged memory back from the unmanaged memory block (and free the memory
block as well).

Hope this helps.
 
I compiled the control in VC++ and import the OCX into C#, the method was
defined by the system as following:
void AppendData(ref double databuf)
How can I declare the method by myself in C#?

Nicholas Paldino said:
This should be declared as:

void AppendData(double[] databuf);

The reason it should be declared like this is because if you declare it
as "ref double" then it will pass only the first item of the array.

If the method modifies the array in any way, then this won't work
either. Because this is a C-style array, it doesn't know what was modified
in the unmanaged realm (SAFEARRAYs don't have this problem) and can't just
marshal all of the memory in the array back.

If the contents are modified, then you will have to declare databuf as
an IntPtr and then allocate the memory yourself (using the static Alloc*
methods on the Marshal class). Then you write to the unmanaged memory block
and pass it to the method. On return from the method, you have to read the
unmanaged memory back from the unmanaged memory block (and free the memory
block as well).

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Owen Woo said:
My activeX control developped in VC++ has following method:
void AppendData(double* databuf)
How can I call this method in C# and transfer the databuf parameter to it?
 
Back
Top