473,568 Members | 2,898 Online
Bytes | Software Development & Data Engineering Community
+ 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* pStructReadCoun t);
C# [DllImport("MyDl l.dll", EntryPoint="Rea dArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount ,
out long pStructReadCoun t);
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(La youtKind.Sequen tial)]
public struct TSruct
{
public TS MyS1;
public byte MyByte;
public TS2 MyS2;
} [StructLayout(La youtKind.Sequen tial)]
public struct TS1
{
public uint DW;
public byte BYTE1;
public byte BYTE2;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 8)]
public byte[] BYTES;
} [StructLayout(La youtKind.Sequen tial)]
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(M yByte,Buffer,iC ount,out iReadCount);
I will appreciate all the information about this.
Many Thanks in advance

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

KW
Feb 28 '06 #1
4 3581
C# [DllImport("MyDl l.dll", EntryPoint="Rea dArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount ,
out long pStructReadCoun t);


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.o rg> schrieb im Newsbeitrag
news:eX******** ******@TK2MSFTN GP14.phx.gbl...
C# [DllImport("MyDl l.dll", EntryPoint="Rea dArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount ,
out long pStructReadCoun t);


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.o rg> schrieb im Newsbeitrag
news:eX******** ******@TK2MSFTN GP14.phx.gbl...
C# [DllImport("MyDl l.dll", EntryPoint="Rea dArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount ,
out long pStructReadCoun t);


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(La youtKind.Sequen tial,Pack=1)].

Thanks Again.

"Mattias Sjögren" <ma************ ********@mvps.o rg> schrieb im Newsbeitrag
news:eX******** ******@TK2MSFTN GP14.phx.gbl...
C# [DllImport("MyDl l.dll", EntryPoint="Rea dArray")]
public static extern uint ReadWithArray(
byte MyByte,
TSruct[] pStruct,
long nMaxStructCount ,
out long pStructReadCoun t);


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
4103
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
2219
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
5906
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 both the parameter for the function and the return value are c-style strings?
3
3452
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 have the same signature, and I never have to call more than one DLL in the same program. (The DLLs parse and validate 5 different binary formats.)...
1
2558
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 below
8
2922
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 call the function, EncodeAsnUser. And it's returning OK but when I display the decoded data in another part of my application it shows no data has...
3
6384
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 what I have... C++ side void populateRequiredIndexNames(char** names); C# side
1
2755
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). The first function is called "StrFirst" and takes in a LPTSTR as the first parameter and a long as the second, like this: SIMPLELIB_API int...
1
5785
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 code: #define USERINT_FUNC __cdecl #ifdef __cplusplus extern "C" { #endif
0
7604
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...
0
7916
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8117
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...
0
7962
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...
0
6275
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
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
0
932
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...

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.