PC Review


Reply
Thread Tools Rate Thread

COM Interop and Array Pointers

 
 
Mike
Guest
Posts: n/a
 
      28th Oct 2008
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
 
Reply With Quote
 
 
 
 
Eusebiu
Guest
Posts: n/a
 
      7th Dec 2008
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...


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
COM Interop and Array Pointers Mike Microsoft Dot NET 1 7th Dec 2008 11:07 AM
Pass from C# (framework 1.1) array of delegates to unmanaged DLL as array of function pointers verpeter@gmail.com Microsoft C# .NET 0 23rd Aug 2006 02:20 PM
Array of pointers/references to another array naveid@gmail.com Microsoft C# .NET 4 22nd Feb 2006 08:13 AM
Array element Pointers? gregory_may Microsoft VB .NET 2 20th Feb 2006 07:55 PM
array of pointers to float Marc Pelletier Microsoft VC .NET 11 8th Jun 2005 01:42 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:22 PM.