473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType =
UnmanagedType.I1, SizeConst = 9)]
public bool[] bArray;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, 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(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}

[StructLayout(LayoutKind.Sequential)]
public struct BoolArray
{
[MarshalAs(UnmanagedType.I1)]
public bool b1;
[MarshalAs(UnmanagedType.I1)]
public bool b2;
// more of the same
[MarshalAs(UnmanagedType.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 6448
gpg,

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

"gpg" <gp****@hotmail.com> wrote in message
news:11*********************@i39g2000cwa.googlegro ups.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(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType =
UnmanagedType.I1, SizeConst = 9)]
public bool[] bArray;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, 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(LayoutKind.Sequential)]
public struct MyArrayStruct2
{
[MarshalAs(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
public BoolArray bArray;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}

[StructLayout(LayoutKind.Sequential)]
public struct BoolArray
{
[MarshalAs(UnmanagedType.I1)]
public bool b1;
[MarshalAs(UnmanagedType.I1)]
public bool b2;
// more of the same
[MarshalAs(UnmanagedType.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 TestArrayInStruct2( 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(UnmanagedType.I1)]
public bool flag;
[MarshalAs(UnmanagedType.I1)]
public bool flag2;
public int val1;
public int val2;
[MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1,
SizeConst = 9)]
public byte[] bArray;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public int[] vals;
}

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

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

and we can conversely read the values from the array as

bool bFlag = Convert.ToBoolean(myStruct2.bArray[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
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...
9
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...
1
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...
6
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
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; }...
2
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...
2
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...
0
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...
2
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...
0
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,...

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.