473,504 Members | 13,621 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshaling variable length arrays

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
Feb 27 '07 #1
4 9623
"Bjarne Nielsen" <bn**************@pc.dkwrote in message
news:Xn*********************************@207.46.24 8.16...
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

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.

Feb 27 '07 #2
On Tue, 27 Feb 2007 16:49:27 +0100, Willy Denoyette [MVP] wrote:
"Bjarne Nielsen" <bn**************@pc.dkwrote in message
news:Xn*********************************@207.46.24 8.16...
>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


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.
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
Feb 28 '07 #3
"Bjarne Nielsen" <bn******@post11.tele.dkwrote in message
news:1h*****************************@40tude.net...
On Tue, 27 Feb 2007 16:49:27 +0100, Willy Denoyette [MVP] wrote:
>"Bjarne Nielsen" <bn**************@pc.dkwrote in message
news:Xn*********************************@207.46.2 48.16...
>>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


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.

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


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.

Feb 28 '07 #4
On Wed, 28 Feb 2007 10:53:57 +0100, Willy Denoyette [MVP] wrote:
"Bjarne Nielsen" <bn******@post11.tele.dkwrote in message
news:1h*****************************@40tude.net...
>On Tue, 27 Feb 2007 16:49:27 +0100, Willy Denoyette [MVP] wrote:
>>"Bjarne Nielsen" <bn**************@pc.dkwrote in message
news:Xn*********************************@207.46. 248.16...
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
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.

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

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.
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
Feb 28 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
1724
by: steflhermitte | last post by:
Dear cpp-ians, I am working with a structure: struct meta_segment { long double id; long double num; long double mean; bool done;
27
2582
by: Mike P | last post by:
I will be passing my function a two dimensional array of varying length. Within that array is one data point, and the number of times it should loop through. So, for example, I might pass this...
5
14096
by: dam_fool_2003 | last post by:
Hai, I studied that the array size is fixed. But I come across a word called "variable length array". Is it possible to change the array size? So I tried the following: #include<stdio.h>...
6
2052
by: JNY | last post by:
Hello, Is it possible to declare an array with variable indeces? i.e. int x = 4; int myArray; for (j = 0;j < 5;j++) {
3
3626
by: Rudy Velthuis | last post by:
Hello, Does anyone know how to create a struct that will marshal to the following C++ struct A, containing an array of the user defined String10 type: struct String10 { char SLen; char S;
0
1457
by: Jeff | last post by:
Hi guys Mattias, thanx for answering my last question Well, I'm struggling with marshaling a struct that has **ptr to an array of arrays of struct. Why? I'm stuck with it 1. I need to know...
23
19133
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
6
1840
by: mechanicfem | last post by:
In f(), I am passing a parameter of array type - /size/ indicates the array's length: void f(char arr, int size); In the c99 stds, it mentions the use of and says (I think) that it can be...
7
2779
by: Cromulent | last post by:
In section 6.7.5.2 it states the following: If the size is not present, the array type is an incomplete type. If the size is*instead of being an expression, the array type is a variable length...
0
7098
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7298
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7366
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7017
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5610
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4698
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3187
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3176
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
754
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.