Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / February 2007

Tip: Looking for answers? Try searching our database.

Marshaling variable length arrays

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Bjarne Nielsen - 27 Feb 2007 12:36 GMT
Hi all

From a C# program, I need to call an unmanaged (C++) dll. The dll fills an
array with variables and returns that to the C# program. When I have a
fixed sized array, this works fine by using the attribute

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 25)]

The calling program doesn't know the size of the array, so I would like the
dll to dynamically expand the array, and return the right sized array.
I have tried using the
[MarshalAs(UnmanagedType.ByValArray, SizeParamIndex=0)]
but I can't seem to get it to work.

Any suggestions?

Cheers
Bjarne Nielsen
Willy Denoyette [MVP] - 27 Feb 2007 15:49 GMT
> Hi all
>
[quoted text clipped - 14 lines]
> Cheers
> Bjarne Nielsen

Who allocated the array?
If the caller doesn't know the length of the array, then it's up to the callee to return the
length and a pointer to the buffer (IntPtr in C#).
After the function returned, you'll have to unmarshal the buffer.

Willy.
Bjarne Nielsen - 28 Feb 2007 09:15 GMT
>> Hi all
>>
[quoted text clipped - 21 lines]
>
> Willy.

Hi Willy

Thank you for your suggestion. It is the callee that allocates the buffer,
and I can return a length and a pointer to the array, but how do I
unmarshal the buffer in my C# code?

Bjarne Nielsen
Willy Denoyette [MVP] - 28 Feb 2007 09:53 GMT
>>> Hi all
>>>
[quoted text clipped - 30 lines]
>
> Bjarne Nielsen

Well you have to solve two problems here:
- Unmarshal the returned buffer.
- Free the buffer.

The first one depends on how you want to access the buffer (say an array of int's), do you
want to access the native array elements directly, or do you need a managed copy of the
array.

Following is a sample for the latter, the C++ takes a aparamter that is defined as a pointer
to an int[] and a the address of an int to store the length.

//C++, error checking omitted!
extern "C" void  __declspec(dllexport) __stdcall Foo(int *d[], int &size)
{
size = 100;
int *arPtr = new int[size];
*d = arPtr;
for(int n = 0; n < size; n++)
 *arPtr++ = n;
}

//C#
 [DllImport("somedll"), SuppressUnmanagedCodeSecurity]
       static extern void Foo(out IntPtr d, out int size);

...
  IntPtr ptrArray = IntPtr.Zero;
  int arraySize;
  GetSimpleArray(out ptrArray, out arraySize);
  int[] mngdArray = new int[arraySize];
  try {
      Marshal.Copy(ptrArray, mngdArray, 0, arraySize);
   }
   finally
   {
       // call function to delete the unmanaged array
   }
  foreach(int i in mngdArray )
   Console.WriteLine(i);
...

Freeing the buffer depends on the allocator used, in above sample, the new operator is used
to allocate an int[], so you 'l have to declare a function that calls delete[] of the
pointer to the array. You need to make sure you call this function whenever you have done
with the marshaling.

extern "C" void  __declspec(dllexport) __stdcall DeleteArray(void *d)
{
if(d)
 delete[] d;
...
}

Willy.
Bjarne Nielsen - 28 Feb 2007 10:20 GMT
>>>> Hi all
>>>>
[quoted text clipped - 85 lines]
>
> Willy.

Thank you VERY much for this detailed description. I haven't tried it yet,
but I shall get to it ASAP, and if I get get further problems, I'll get
back.

Thanks again!!

Bjarne Nielsen

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.