Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks.

Go Back   Vista Forums > Misc Newsgroups > .NET General

Vista - COM Interop and Array Pointers

Reply
 
Old 10-28-2008   #1 (permalink)
Mike


 
 

COM Interop and Array Pointers

I am using a COM DLL function that takes a pointer to the first
element in an array of short values.

Foo(short* array)

COM interop provides that function to me in the .NET world with a "ref
short" parameter.

Foo(ref short array)

It seems that no matter what I try, the function only sees the first
element in the array. Is there a way to get it to see then entire
array? Maybe by using some sort of marshalling I haven't tried yet?

Thanks,
Mike

My System SpecsSystem Spec
Old 12-07-2008   #2 (permalink)
Eusebiu


 
 

RE: COM Interop and Array Pointers

Hello...
If you write short* array is not actually... is a pointer to a memory zone
where is some information (it may be the start of an array).. so don't think
that this is an array...

To call that method correctly, you'll have to marshal the C# array to
unmanaged and send the size of the array.
So you'll have something like this...
//C++
void Foo(short* pArray, unsigned int size){...};

//C#
[DllImport("MyDll.dll")]
public static extern Foo(ref IntPtr arrayPointer, uint size);

To create arrayPointer you'll have to allocate some memory...
short[] myArray;
//init and set array elements

IntPtr arrayPointer = Marshal.AlloHGlobal(myArray.Length *
Marshal.SizeOf(typeof(short));

Marshal.Copy(myArray, 0, arrayPointer, myArray.Length);

//invoke method...
Foo(arrayPointer, myArray.Length);

Hope this helps...


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Fast copy method of sub array (=array range) possible? VB Script
COM Interop and Pointer to Array .NET General
Microsoft.VirtualServer.Interop.dll and the Microsoft.VMRCClientControl.Interop.dll Virtual Server
Using an interop assembly with interop dependencies PowerShell
how to assign values to array and how to create array via variable PowerShell


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46