473,660 Members | 2,428 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Marshal bool array to dll

gpg
I am using a legacy DLL and need to marshal some structures for use in
the DLL. For the most part, I have figured out my needs except for one
small item.

I have a structure that contain, among other items, an array of bools
(not BOOL). The array is a fixed size and is contained in the
structure.

Should be simple ie:

[StructLayout(La youtKind.Sequen tial)]
public struct MyArrayStruct2
{
[MarshalAs(Unman agedType.I1)]
public bool flag;
[MarshalAs(Unman agedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(Unman agedType.ByValA rray, ArraySubType =
UnmanagedType.I 1, SizeConst = 9)]
public bool[] bArray;
public BoolArray bArray;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 3)]
public int[] vals;
}

except when the structure is passed to the DLL, the data after the bool
array is broken and the data. I have found this same issue when the
data alignment in the passed structure is incorrect (ie single bool
type passed without the MarshalAs).

I have found a solution, which involves creating another strucure of
only bool (marshalled) and using the reference to this structure in my
main structure.

[StructLayout(La youtKind.Sequen tial)]
public struct MyArrayStruct2
{
[MarshalAs(Unman agedType.I1)]
public bool flag;
[MarshalAs(Unman agedType.I1)]
public bool flag2;
public int val1;
public int val2;
public BoolArray bArray;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 3)]
public int[] vals;
}

[StructLayout(La youtKind.Sequen tial)]
public struct BoolArray
{
[MarshalAs(Unman agedType.I1)]
public bool b1;
[MarshalAs(Unman agedType.I1)]
public bool b2;
// more of the same
[MarshalAs(Unman agedType.I1)]
public bool b9;
}

This works fine but is ugly and a bit unwieldy. I have tried different
size and packing attributes for the StructLayout to no avail.

Any ideas? It seems that the ByValArray with the subtype of I1 should
work fine.

Thanks!

GPG

May 10 '06 #1
4 6470
gpg,

Can you show the original structure declaration in C?
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"gpg" <gp****@hotmail .com> wrote in message
news:11******** *************@i 39g2000cwa.goog legroups.com...
I am using a legacy DLL and need to marshal some structures for use in
the DLL. For the most part, I have figured out my needs except for one
small item.

I have a structure that contain, among other items, an array of bools
(not BOOL). The array is a fixed size and is contained in the
structure.

Should be simple ie:

[StructLayout(La youtKind.Sequen tial)]
public struct MyArrayStruct2
{
[MarshalAs(Unman agedType.I1)]
public bool flag;
[MarshalAs(Unman agedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(Unman agedType.ByValA rray, ArraySubType =
UnmanagedType.I 1, SizeConst = 9)]
public bool[] bArray;
public BoolArray bArray;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 3)]
public int[] vals;
}

except when the structure is passed to the DLL, the data after the bool
array is broken and the data. I have found this same issue when the
data alignment in the passed structure is incorrect (ie single bool
type passed without the MarshalAs).

I have found a solution, which involves creating another strucure of
only bool (marshalled) and using the reference to this structure in my
main structure.

[StructLayout(La youtKind.Sequen tial)]
public struct MyArrayStruct2
{
[MarshalAs(Unman agedType.I1)]
public bool flag;
[MarshalAs(Unman agedType.I1)]
public bool flag2;
public int val1;
public int val2;
public BoolArray bArray;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 3)]
public int[] vals;
}

[StructLayout(La youtKind.Sequen tial)]
public struct BoolArray
{
[MarshalAs(Unman agedType.I1)]
public bool b1;
[MarshalAs(Unman agedType.I1)]
public bool b2;
// more of the same
[MarshalAs(Unman agedType.I1)]
public bool b9;
}

This works fine but is ugly and a bit unwieldy. I have tried different
size and packing attributes for the StructLayout to no avail.

Any ideas? It seems that the ByValArray with the subtype of I1 should
work fine.

Thanks!

GPG

May 10 '06 #2
gpg
Here it is along with the function call.

typedef struct _MYARRAYSTRUCT2
{
bool flag;
bool flag2;
long val1;
long val2;
bool bFrequencies[9];
int vals[ 3 ];
} MYARRAYSTRUCT2;

extern "C" PINVOKELIB_API void TestArrayInStru ct2( MYARRAYSTRUCT2*
pStruct );

May 10 '06 #3
>Any ideas? It seems that the ByValArray with the subtype of I1 should
work fine.


Indeed, but if you can't get it to work, you can try using a byte[]
instead.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
May 10 '06 #4
gpg
Well ... I have come up with a "sort of" solution to this issue
although I am of the general consesus that this issue is reflective of
a bug of some sort.

On the C# side I have done the following:

public struct MyArrayStruct2
{
[MarshalAs(Unman agedType.I1)]
public bool flag;
[MarshalAs(Unman agedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(Unman agedType.ByValA rray, ArraySubType = UnmanagedType.I 1,
SizeConst = 9)]
public byte[] bArray;
[MarshalAs(Unman agedType.ByValA rray, SizeConst = 3)]
public int[] vals;
}

Notice the change from bool[] to byte[]. Now the consumer initializes
the "bool" array as

myStruct2.bArra y = new byte[9];
for (int i = 0; i < 9; i++)
{
myStruct2.bArra y[i] = Convert.ToByte( false);
}

and we can conversely read the values from the array as

bool bFlag = Convert.ToBoole an(myStruct2.bA rray[i]);

This works with the legacy DLL and everything is happy --- but this is
certainly an awkward and hacky way to perform this.

May 11 '06 #5

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

Similar topics

0
1891
by: William Stacey | last post by:
The following code works, but I can't figure out why. I take a struct with two members, a single byte and byte. I then marshal the whole struct to a byte. I create a new struct (without init'ing any members) and marshal the tmp array back to the new struct. The new struct shows the same data. The single type is easy, that just gets copied. The byte ref type is the interesting part. The address (i.e ref) of the original byte gets...
9
2809
by: Angel | last post by:
Hi again, I'm trying to call functions from a proprietary DLL but it's turned out to be more difficult than I thought. I have this W32.DLL which was written in C by USPS. They don't provide the code so I only have the documentation. I'm trying to call a function called z4date that, according to the docs, returns the date as "an 8-byte character string in the "YYYYMMDD" format". When I run it with this code I've written , I get "Can not...
1
4208
by: dhornyak | last post by:
I have been banging my head against the wall for a while now, and can't seem to id the problem. I've been through a ton of posts and the code doesn't seem any different. Can anybody see it? When I call to GetTokenInformation I receive a buffer size (see //HERE... comment in code), but when I let the code continue, asp.net just sits there returning nothing, apparently on the Marshal.AllocHGlobal call. Here's the library function:
6
6682
by: Howard Kaikow | last post by:
Given: private struct PROCESSENTRY32 { public int dwSize; public int cntUsage; public int th32ProcessID; public int th32DefaultHeapID; public int th32ModuleID;
0
1407
by: [Yosi] | last post by:
I want to access a function implemented in a C-dll with the following data prototype: //// TS - Test Structure typedef struct _T_TestStructure { char Name ; int TestId; } T_TestStructure;
2
4852
by: twawsico | last post by:
I have a piece of code that needs to read the contents of a binary file (that I've created with another app) into an array of structures. The binary data in the file represents just a series of singles that correspond to those in my structure detailed below. So when I load the file, all that I know for certain is that there will be some multiple of these eight singles represented in the binary data. My code below will read the data...
2
7200
by: O.B. | last post by:
When using Marshal to copy data from a byte array to the structure below, only the first byte of the "other" array is getting copied from the original byte array. What do I need to specify to get Marshal.PtrToStructure to copy the all the data into the "other" array? unsafe public struct DeadReckoning {
0
2728
by: xrxst32 | last post by:
Hello there, I have some doubts about the best practice for using COM automation, the Runtime Callable Wrapper (RCW) and Marshal.ReleaseComObject. So the question is/are: Do I need to release each COM object explicit by a call to Marshal.ReleaseComObject, does the RCW take care of that or does it leaks unmanaged resources?
2
15928
by: O.B. | last post by:
I have operation within a class that marshals the data into a byte array. Below are three different ways that work. Are there any downsides to using one over the the other? public virtual byte ToRaw1() { byte byteArray = new byte; IntPtr pointer = Marshal.AllocHGlobal(Size); Marshal.StructureToPtr(this, pointer, false); Marshal.Copy(pointer, byteArray, 0, Size);
0
8341
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
8851
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8754
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...
0
7362
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6181
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4177
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
4343
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1984
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1740
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.