473,467 Members | 1,603 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

DllImport - Function use as parameter an array of structures

Hello everybody. I have a problem importing a function from a Dll. This dll was made in C++. I get in troubles when I try to use the following function:

Function
DLL DWORD __stdcall ReadWithArray(
byte MyByte,
TStruct* pStruct,
long nMaxStructCount,
long* pStructReadCount);
C# [DllImport("MyDll.dll", EntryPoint="ReadArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount,
out long pStructReadCount);
Here are also the structures; mabe come the problem from here too.

structures:
DLL typedef struct
{
TS1 MyS1;
byte MyByte;
TS2 MyS2;
} TStruct; typedef struct
{
DWORD DW;
BYTE BYTE1;
BYTE BYTE2;
BYTE BYTES[8];
} TS1; typedef struct
{
DWORD DW;
WORD W1;
WORD W2;
} TS2;

C# [StructLayout(LayoutKind.Sequential)]
public struct TSruct
{
public TS MyS1;
public byte MyByte;
public TS2 MyS2;
} [StructLayout(LayoutKind.Sequential)]
public struct TS1
{
public uint DW;
public byte BYTE1;
public byte BYTE2;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
public byte[] BYTES;
} [StructLayout(LayoutKind.Sequential)]
public struct TS2
{
public uint DW;
public ushort B1;
public ushort B2;
}
/-----------------------------------------------------------------------------------------------------------
I don't know if the problem is the memory allocation for the array of my TStruct but is the only thing which I didn't made before (a pointer to an arrray in an imported function). Should I make some Marshaling?? (i have seen some examples about that but I'm not sure of I have to do something like that in my case).

TStruct[] Buffer;
int iREadCount, iCount = 10;
byte MyByte;

Buffer= new TStruct[iCount];
for(int i= 0; i < iCount; i++)
Buffer[i].MyS1.BYTES = new byte[8];

///---- and here come the error!!! ----///
//
ecRes = ReadWithArray(MyByte,Buffer,iCount,out iReadCount);
I will appreciate all the information about this.
Many Thanks in advance

/-----------------------------------------------------------------------------------------------------------

KW
Feb 28 '06 #1
4 3576
C# [DllImport("MyDll.dll", EntryPoint="ReadArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount,
out long pStructReadCount);


Change the longs to ints. You may also have to add the [Out] attribute
to the array parameter for it to be marshaled back correctly.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 28 '06 #2
Thanks a lot!

"Mattias Sjögren" <ma********************@mvps.org> schrieb im Newsbeitrag
news:eX**************@TK2MSFTNGP14.phx.gbl...
C# [DllImport("MyDll.dll", EntryPoint="ReadArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount,
out long pStructReadCount);


Change the longs to ints. You may also have to add the [Out] attribute
to the array parameter for it to be marshaled back correctly.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 28 '06 #3
Hello Mattias,

with the changes it works. But now the problem is the data. When I
receive a couple of data in the array, only is correct the first item, the
rest is not Ok, there are missing values or random dada and even invalid
data....
by example... the function return a value of 10 (ten structures read) and
when I try to index after the 7, I get on a Out of Index problem....

any Idea?.

Thanks Again.

"Mattias Sjögren" <ma********************@mvps.org> schrieb im Newsbeitrag
news:eX**************@TK2MSFTNGP14.phx.gbl...
C# [DllImport("MyDll.dll", EntryPoint="ReadArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount,
out long pStructReadCount);


Change the longs to ints. You may also have to add the [Out] attribute
to the array parameter for it to be marshaled back correctly.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 28 '06 #4
I have already found the problem... I add the Attribute [Pack=1] in my
structure definition
[StructLayout(LayoutKind.Sequential,Pack=1)].

Thanks Again.

"Mattias Sjögren" <ma********************@mvps.org> schrieb im Newsbeitrag
news:eX**************@TK2MSFTNGP14.phx.gbl...
C# [DllImport("MyDll.dll", EntryPoint="ReadArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount,
out long pStructReadCount);


Change the longs to ints. You may also have to add the [Out] attribute
to the array parameter for it to be marshaled back correctly.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 28 '06 #5

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

Similar topics

8
by: kalinga1234 | last post by:
there is a problem regarding passing array of characters to another function(without using structures,pointer etc,).can anybody help me to solve the problem.
15
by: Daniel Rudy | last post by:
Hello, Consider the following code: /* resolve_hostname this resolves the hostname into an ip address. */ static void resolve_hostname(char result, const char hostname, const char server) {
6
by: Tim Mulholland | last post by:
Whats the correct C# datatype (or marshalling function or something) to use when you're importing a function that has a signature similar to char* FuncA(char c) ? Assuming you know that...
3
by: Mark Jerde | last post by:
I'm sill learning VS .NET 2003, not an expert yet. I'm calling an unmanaged C++ DLL from C# using . When the whole project is done I will be calling a total of 5 C++ DLLs from C#. All the DLLs...
1
by: Stephen Richardson | last post by:
I have a C++ dll which returns a structure, the structure contains a char variable, as shown below. struct MyCStruct { short iNumber; char Name; }; I've declared a structure in C# as shown...
8
by: =?Utf-8?B?UHVjY2E=?= | last post by:
Hi, I'm using vs2005, .net 2, C# for Windows application. I use DllImport so I can call up a function written in C++ as unmanaged code and compiled as a dll us vs2005. My application is able to...
3
by: Elikhom | last post by:
Hi, I'm trying to call a C++ function that has a char** as a parameter from C# code using DllImport. After googling around and finding some threads I haven't been able to solve my problem. This is...
1
by: JohnCox | last post by:
I have a simple Win32 DLL I wrote named "SimpleLib" that exports two functions. It is written in C++ and compiled with __stdcall (/Gz) and with the preprocessor definition _MBCS (not Unicode). ...
1
by: elke | last post by:
Hi, I want to use an unmanaged dll in C# .net and I'm having some troubles witch a function that should return an array. I'm new at this, so I don't know what I'm doing wrong. Here is some...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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
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
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
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
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 ...

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.