473,320 Members | 1,865 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,320 software developers and data experts.

C# fixed array in structure

I'm new to C# and have the following problem that I hope is simple for
you C# experts out there!

I have a need to call an unmanaged DLL (written in C) that needs the
following structure defined (C syntax):

typedef struct {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
} bmBITMAPINFO;

Note how the second item is an array of 256 items and it's in that
physical block, not a reference to another block.

I'm trying to replicate this in C# so I can set up this structure and
pass it to my DLL. I don't see any obvious way to do it as arrays are
all "by reference" and this doesn't work (I need the actual data in that
structure, not a pointer to the data).

Example:

// defined in Dll.cs
public struct BmBitmapInfoStruct
{
public BITMAPINFOHEADER bmiHeader;
public RGBQUAD [] bmiColors;
}
// later... in main code...
bmi = new Dll.BmBitmapInfoStruct();
bmi.bmiColors = new Dll.RGBQUAD[256];

doesn't give the desired result as only the pointer is placed into the
structure, not the actual data.

Thanks for any insights,
Dave
May 2 '06 #1
3 7832
On Tue, 02 May 2006 09:49:39 -0500, dzar wrote:
I'm new to C# and have the following problem that I hope is simple for
you C# experts out there!

I have a need to call an unmanaged DLL (written in C) that needs the
following structure defined (C syntax):

typedef struct {
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
} bmBITMAPINFO;


See this article <http://www.vsj.co.uk/articles/display.asp?id=501> for an
detailled example of how to solve this kind of problem.
May 2 '06 #2
>// defined in Dll.cs
public struct BmBitmapInfoStruct
{
public BITMAPINFOHEADER bmiHeader;
public RGBQUAD [] bmiColors;
}


Make it

[MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
public RGBQUAD[] bmiColors;

It only works in .NET 2.0 though.
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 2 '06 #3
Mattias Sjögren wrote:
// defined in Dll.cs
public struct BmBitmapInfoStruct
{
public BITMAPINFOHEADER bmiHeader;
public RGBQUAD [] bmiColors;
}


Make it

[MarshalAs(UnmanagedType.ByValArray, SizeConst=256)]
public RGBQUAD[] bmiColors;

It only works in .NET 2.0 though.
Mattias


Beautiful solution. Thank you.

For others who see this later, the one caveat I found was that when you
reference bmiColors, you cannot get the members of the RGBQUAD. To
overcome this I simply created a local variable of type RGBQUAD and then
assigned it to bmiColors[x].

Thanks for all the suggestions.

Dave
May 2 '06 #4

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

Similar topics

3
by: PHil Coveney | last post by:
Hello, I am making calls into a legacy DLL. One function in this DLL expects as a parameter a struct with several fields, each of which is a 40-char array. The effect of this function is to copy...
2
by: Cyril | last post by:
Hello, I have a problem to marshal a structure that contains an array of an others struct. This array is an array size fixed (MyStruct myStructs and not MyStruct *myStructs). For example : ...
4
by: Scott Lemen | last post by:
Hi, Some Win APIs expect a structure with a fixed length string. How is it defined in VB .Net 2003? When I try to use the FixedLengthString class I get an "Array bounds cannot appear in type...
6
by: Eric | last post by:
.... my eternal gratitude!!! :p Here is the problem. A sample of my original VB6 code : '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Public...
4
by: taskswap | last post by:
I'm converting an application that relies heavily on a binary network protocol. Within this protocol are a lot of byte arrays of character data, like: public unsafe struct MsgAddEntry {...
0
by: Ken Varn | last post by:
I have a managed C++ assembly in which I need to interact with some 'C' APIs that take fixed size 'C' data blocks. I need to wrap these data blocks into a managed object. It seems like a lot of...
1
by: O.B. | last post by:
In the example below, I'm trying to convert a fixed byte array to a string. I get an error about needing to use "fixed" but I have no clue where to apply it. Help? using System; using...
1
by: Rick Knospler | last post by:
I am trying to convert a vb6 project to vb.net. The conversion worked for the most part except for the fixed length strings and fixed length string arrays. Bascially the vb6 programmer stored all...
1
by: shofu_au | last post by:
Hi Group, I am trying to define a class that has a fixed size array of a structure containing a fixed size array of a structure. I am using System.Runtime.InteropServices and trying to define...
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.