473,769 Members | 6,107 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

syntax for fixed array in C#?

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 characters into these char array fields.

I am calling this function with (roughly) the following:

[StructLayout(La youtKind.Explic it, Size=120, CharSet=CharSet .Auto)]
public class Foo
{
[FieldOffset(0)] public byte A;
[FieldOffset(40)] public byte B;
[FieldOffset(80)] public byte C;
}

[DllImport("Lega cy.dll")]
public static extern int copyChars (Foo fooVar);

N.B. I am using defining them as byte fields because the DLL returns 8-bit chars, and its easier to see what's going on in the debugger that way.

The calls appear to be successful, as the debugger shows enough information to see that the expected data is going to the expected fields.

My problem is getting those characters into a C# string. If I use

char dst = new char[41];
int nResult = new ASCIIEncoding() .GetChars(fooVa r.A, 0, 40, dst, 0);

the compiler complains that it cannot convert a byte field (fooVar.A) to a byte array. If, on the other hand, I avoid this complaint by changing the formal declaration of fooVar.A from
[FieldOffset(0)] public byte A;

to

[FieldOffset(0)] public byte[] A;

the compiler is happy, but I get a runtime exception complaining that A cannot be marshaled. (I don't know what the issue is there.)

I realize the details of this are fairly esoteric. I suspect there is some simple casting syntax where I can tell the compiler that, yes, A is a byte array, but the 4000 variations I have tried have failed.
On the other hand, if there is a way to accomplish what I'm trying to without jumping through this particular hoop, I'd welcome hearing baout it.

Thanks in advance,

--
PC
Nov 22 '05 #1
3 10202

"PHil Coveney" <PH*********@di scussions.micro soft.com> wrote in message
news:CA******** *************** ***********@mic rosoft.com...
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 characters into these char
array fields.

AFAIK, there is no way in C# 1.0/1.1 to create a fixed array. The only way I
can think of off the top of my head would be a byte array and manually
parsing out your values into the structure, PITA, but it should work.

However, C# 2.0 does address this with the fixed keyword, but I'm afraid
that won't be of any help to you for quite some time as its not expected
until next year.
I am calling this function with (roughly) the following:

[StructLayout(La youtKind.Explic it, Size=120, CharSet=CharSet .Auto)]
public class Foo
{
[FieldOffset(0)] public byte A;
[FieldOffset(40)] public byte B;
[FieldOffset(80)] public byte C;
}

[DllImport("Lega cy.dll")]
public static extern int copyChars (Foo fooVar);

N.B. I am using defining them as byte fields because the DLL returns
8-bit chars, and its easier to see what's going on in the debugger that
way.

The calls appear to be successful, as the debugger shows enough
information to see that the expected data is going to the expected fields.

My problem is getting those characters into a C# string. If I use

char dst = new char[41];
int nResult = new ASCIIEncoding() .GetChars(fooVa r.A, 0, 40, dst, 0);

the compiler complains that it cannot convert a byte field (fooVar.A) to
a byte array. If, on the other hand, I avoid this complaint by changing
the formal declaration of fooVar.A from
[FieldOffset(0)] public byte A;

to

[FieldOffset(0)] public byte[] A;

the compiler is happy, but I get a runtime exception complaining that A
cannot be marshaled. (I don't know what the issue is there.)

I realize the details of this are fairly esoteric. I suspect there is
some simple casting syntax where I can tell the compiler that, yes, A is a
byte array, but the 4000 variations I have tried have failed.
On the other hand, if there is a way to accomplish what I'm trying to
without jumping through this particular hoop, I'd welcome hearing baout
it.

Thanks in advance,

--
PC

Nov 22 '05 #2
>
[StructLayout(La youtKind.Explic it, Size=120, CharSet=CharSet .Auto)]
public class Foo
{
[FieldOffset(0)] public byte A;
[FieldOffset(40)] public byte B;
[FieldOffset(80)] public byte C;
}


<snip>
Assuming the above Foo, this code will work

Foo foo=new Foo();
//call Legacy
string str=null;
fixed(byte* bP=&foo.A)
{
byte[] A=new byte[40];
for(int x=0;x<A.Length; x++)
A[x]=bP[x];
str=Encoding.AS CII.GetString(A ); //if the bytes use
all 8 bits, you'll get bad values. Look at other Encoding.* classes
}

You can also declare the byte fields as byte* to save a step.

Austin
Nov 22 '05 #3
Austin, Daniel,

Thanks guys.

Using fixed, as Austin's example shows, worked fine. It was also a relief to know that I was not overlooking some simple and obvious alternative.

Austin, thanks for the tip about ASCIIEncoding, too. In the case I have to solve, I'm guaranteed to have printable ASCII so I don't have to worry about bit 8, but I'll take a look around for an alternative anyway.

--
PC
"Austin Ehlers" wrote:

[StructLayout(La youtKind.Explic it, Size=120, CharSet=CharSet .Auto)]
public class Foo
{
[FieldOffset(0)] public byte A;
[FieldOffset(40)] public byte B;
[FieldOffset(80)] public byte C;
}


<snip>
Assuming the above Foo, this code will work

Foo foo=new Foo();
//call Legacy
string str=null;
fixed(byte* bP=&foo.A)
{
byte[] A=new byte[40];
for(int x=0;x<A.Length; x++)
A[x]=bP[x];
str=Encoding.AS CII.GetString(A ); //if the bytes use
all 8 bits, you'll get bad values. Look at other Encoding.* classes
}

You can also declare the byte fields as byte* to save a step.

Austin

Nov 22 '05 #4

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

Similar topics

699
34158
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
3
464
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 characters into these char array fields. I am calling this function with (roughly) the following: public class Foo { public byte A;
11
6769
by: Linny | last post by:
Hi, I need some help in declaring an array of pointers to array of a certain fixed size. I want the pointers to point to arrays of fixed size only (should not work for variable sized arrays of the same type). eg: int arr1;//Array of 20 ints int arr2; int arr3; ............
4
7613
by: Greg Stark | last post by:
I find myself wishing I had a syntax "LIKE ANY (array)". I don't see much value in the = ANY, = ALL, <> ANY, <> ALL syntax since they're equivalent more or less to IN and NOT IN. But it could be neat if other operators were supported. As it turns out this isn't immediately relevant, it will only be relevant when one day the database drivers use the binary FE protocol and support binding arrays directly. Then I could pass an...
8
2252
by: masood.iqbal | last post by:
All this time I was under the illusion that I understand the concept of multi-dimensional arrays well ---- however the following code snippet belies my understanding. I had assumed all along that a declaration like: int intArray means that there is an array of pointers to int with a size of 5, each of whose elements is an array of int with a size of 3. This definition seemed intuitive to me since a declaration like
5
8324
by: Arthur Mnev | last post by:
This is probably beaten to death subject... Does anyone have a good idea of what penalties are for using Fixed statement in c#. On one side it allows for much greater flexibility with casts and array manipulations (i'm leaving access to legacy code out of the scope of this message) on the other hand fixed statement does consume resources to execute, not to mention if "everything" is fixed, then dynamic object reallocation becomes...
2
11202
by: coz | last post by:
I created a wrapper class for a dll written in C. Will the following code prevent the "ENTIRE ARRAY" from being moved, not just the first array element? The Wrapper class will be instantiated in a VB .NET application which doesn't have the ability to prevent the array from being moved. Thanks in advance, coz public class Wrapper {
18
2947
by: robert | last post by:
Using global variables in Python often raises chaos. Other languages use a clear prefix for globals. * you forget to declare a global * or you declare a global too much or in conflict * you have a local identical variable name and want to save/load it to/from the global with same name * while you add code, the definition of globals moves more and more apart from their use cases -> weirdness; programmers thinking is fragmented * using...
2
2516
by: JJA | last post by:
I'm looking at some code I do not understand: var icons = new Array(); icons = new GIcon(); icons.image = "somefilename.png"; I read this as an array of icons is being built. An element of the array is an object itself but what is this syntax of the consecutive double quotes inside the brackets ?
1
10431
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 System.Collections.Generic; using System.Text; using System.Runtime.InteropServices;
0
9423
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
10211
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
10045
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...
1
9994
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7408
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
6673
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5447
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3561
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
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.