473,508 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to pass array of structures into a C dll with PInvoke?

Hello :)

I have a DLL file written in C where one function accepts an array of
structures as input paramter and pointer to length integer.
This function will fill up the array and specify the number of items set.
In other words the prototype looks like: void Func(mystruct *pStruct, int
*npLength);

I would like to call this from C# but I'm having a lot of trouble getting it
to accept this array as parameter.

I mapped my structure into a C# class:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class Host
{
public HOST_TYPES nHostType;

// char * string of length MAX_SHORT_STRING_LENGTH
[MarshalAs(UnmanagedType.LPStr, SizeConst = MAX_SHORT_STRING_LENGTH)]
public String strDescription;

// unsigned char array of length MAX_SHORT_STRING_LENGTH
[MarshalAs(UnmanagedType.U1, SizeConst = MAX_SHORT_STRING_LENGTH)]
public byte [] sValue;

public int nLength;

[MarshalAs(UnmanagedType.LPStr, SizeConst = MAX_SHORT_STRING_LENGTH)]
public String strStringValue;
}

Then I made a prototype in C# for my function:

[DllImport("mydll.dll", CharSet=CharSet.Ansi, EntryPoint = "#14")]
private static extern ERRORCODES CSHost(IntPtr Handle, HOST_TYPES nHostType,
[MarshalAs(UnmanagedType.LPArray)] Host [] lpHost, ref int nHost);

By doing printouts from my C function I can see that all parameters except
Host [] gets mapped like they should.
For testing I tried to make an array and set the first member but still it
prints out garbage from inside the DLL. Similarly when I try to fill the
first member of the array with data it causes the program to crash with an
exception.

Inside C# the array is initialized like this:

lpHost = new Host[MAX_HOSTS];

for (int i = 0; i < lpHost.Length; i++)
lpHost[i] = new Host();

What am I missing here? I tried lots of combinations of keywords including:
ref and [In,Out]

Thanks in advance.

-- John
Nov 16 '05 #1
2 3544
John,
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class Host


Try making Host a struct.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
> >[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class Host


Try making Host a struct.


Thanks alot! :)

I should have asked earlier since it did the trick. I naturally found out
afterwards I had more bugs with the marshalling but now it's working like
it's suppose to.

However theres one thing I don't quite understand.

To make it work I have to convert the type:
char szDescription[MAX_SHORT_STRING_LENGTH];
to
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_SHORT_STRING_LENGTH+1)]
public String strDescription;

What annoys me is the "+1" in the size which I didn't find any clear answer
for. The best guess I have is something like the zero termination, which is
about the only explanation I can think of. Any of you know more?

Thanks in advance.
-- John
Nov 16 '05 #3

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

Similar topics

4
1775
by: jrefactors | last post by:
In the following program, are parameters s in function reverse() and x in function count() both pass by value? How come value k is not changed, but value str has changed? Please advise....
4
7272
by: emma middlebrook | last post by:
Hi Straight to the point - I don't understand why System.Array derives from IList (given the methods/properties actually on IList). When designing an interface you specify a contract. Deriving...
16
14079
by: Ekim | last post by:
hello, I'm allocating a byte-Array in C# with byte byteArray = new byte; Now I want to pass this byte-Array to a managed C++-function by reference, so that I'm able to change the content of the...
6
2247
by: Mythran | last post by:
struct MyStruct { public int Number; } struct MyStruct { private int mNumber; public int Number
6
1709
by: Boni | last post by:
Dear all, I have written a C++ COM object: STDMETHOD(myfunc)(INT* array) Now I need to pass an array from c# to COM. I generated a COM interop and the signature of the function is ...
1
2534
by: Scott McFadden | last post by:
What is the proper way to pass pointers by reference from managed c++ calls to native c++ calls? In managed C++, I have a pointer to an array of structures, that I pass to a native c++ method by...
0
978
by: srinivas | last post by:
Hi , Pls help me to solve this problem . /* --this is one of the function in COM component */ int ProjectLIsts(LPCTSTR prj , VARIANT* prj_list) { prj_Lists= { ARRY OF STRUCTURES . ...
44
5732
by: svata | last post by:
Hello, I wonder how to resize such array of structures using realloc()? #include <stdio.h> #include <stdlib.h> #define FIRST 7 typedef struct { char *name;
4
8088
by: robert.waters | last post by:
I have to parse a binary file having a number of fixed records, each record containing datas in fixed positions. I would like to parse this binary file into an array of structures having members...
0
7342
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
7410
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
7067
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
7505
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...
0
5650
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,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4729
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
3215
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
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.