473,774 Members | 2,105 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(Unman agedType.ByValA rray, 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(Unman agedType.ByValA rray, SizeParamIndex= 0)]
but I can't seem to get it to work.

Any suggestions?

Cheers
Bjarne Nielsen
Feb 27 '07 #1
4 9666
"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(Unman agedType.ByValA rray, 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(Unman agedType.ByValA rray, 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.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(Unman agedType.ByValA rray, 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(Unman agedType.ByValA rray, 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******@post1 1.tele.dkwrote in message
news:1h******** *************** ******@40tude.n et...
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(Unman agedType.ByValA rray, 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(Unman agedType.ByValA rray, 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(dlle xport) __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("some dll"), SuppressUnmanag edCodeSecurity]
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(pt rArray, mngdArray, 0, arraySize);
}
finally
{
// call function to delete the unmanaged array
}
foreach(int i in mngdArray )
Console.WriteLi ne(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(dlle xport) __stdcall DeleteArray(voi d *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******@post1 1.tele.dkwrote in message
news:1h******** *************** ******@40tude.n et...
>On Tue, 27 Feb 2007 16:49:27 +0100, Willy Denoyette [MVP] wrote:
>>"Bjarne Nielsen" <bn************ **@pc.dkwrote in message
news:Xn****** *************** ************@20 7.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(Unman agedType.ByValA rray, 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(Unman agedType.ByValA rray, 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(dlle xport) __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("some dll"), SuppressUnmanag edCodeSecurity]
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(pt rArray, mngdArray, 0, arraySize);
}
finally
{
// call function to delete the unmanaged array
}
foreach(int i in mngdArray )
Console.WriteLi ne(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(dlle xport) __stdcall DeleteArray(voi d *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
1754
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
2634
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 to the function: example = new Array("A",2); example = new Array("Q",4); function loopIt(example);
5
14114
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> #include<stdlib.h> int main(void) { int y = { 7, 9,10},i; for (;i<20;i++)
6
2070
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
3645
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
1472
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 how to get to the contents of "dumberstruct" contained within "dumbstruct" (i.e. dumbstruct.dstruct) 2. How to get to dumbstruct.tag_field_value and dumbstruct.field_value Code follows
23
19203
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
1855
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 used to indicate that an array parameter has /variable length/ - so, f() may be rewitten as: void f(char arr, int size);
7
2798
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 array type of unspeciļ¬ed size, which can only be used in declarations with function prototype scope;124) such arrays are nonetheless complete types. If the size is an integer constant expression and the element
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10106
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9914
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5355
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5484
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4012
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3611
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2852
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.