Error translating structure code from C ++ to C #

1

I'm trying to read a string from a process, I found this structure on the internet:

struct name
{
    wchar_t nome[32];
};

I did it this way:

struct name
{
    public char[] nome = new char[32];
}

And I'm returning the following error:

  

Can not have instance properties or initializers in structures.

Where am I using struct :

ReadMemory<name>((0x4EFDE5C + 0x54) + (0x1E8 * (i + 1) + 0x24));

public static T ReadMemory<T>(int Adress)
{
    int ByteSize = Marshal.SizeOf(typeof(T)); // Get ByteSize Of DataType
    byte[] buffer = new byte[ByteSize]; // Create A Buffer With Size Of ByteSize
    ReadProcessMemory((int)m_pProcessHandle, Adress, buffer, buffer.Length, ref m_iNumberOfBytesRead); // Read Value From Memory

    return ByteArrayToStructure<T>(buffer); // Transform the ByteArray to The Desired DataType
}

How to proceed?

    
asked by anonymous 27.11.2017 / 22:13

2 answers

4

First, you can not bridge the semantics of C to C #. Nothing you do will stay the same. It has several differences, even though it seems to be the same.

You can not initialize members of a structure directly, you have to:

  • be in a constructor
  • You have to initialize with a value passed to the constructor (can not be the default constructor)
  • You have to initialize all members.

This would work:

public class Program {
    public static void Main() {
        var nome = new Name(new char[32]);
    }
}

public struct Name {
    public Name(char[] nome) => Nome = nome;
    public char[] Nome;
}

See running on .NET Fiddle . Also I placed GitHub for future reference .

If you want you can do a validation to make sure the size is 32 characters. But as I said it will never be equal to C. It would also be interesting to see if reasonable data came. A structure should never be in an invalid state. A class too, but in structures it is much more important.

One way to make the semantics a little closer is to use a fixed , such as shown in another question .

It is possible to give other solutions, but without context it is difficult to say something. To tell the truth the example shows that something very wrong is being done.

There are those who do something like this (I do not like it for a lot of reasons, for me it's a rough and bad gambi for performance and memory consumption)

public class Program {
    public static void Main() {
        var nome = new Name();
    }
}

public struct Name {
    private bool inicializado;
    private char[] nome;
    public char[] Nome { get {
        if (!inicializado) {
            nome = new char[32]; 
            inicializado = true;
        }
        return nome;
    }
}

See running on .NET Fiddle . Also I put GitHub for future reference .

One possibility is to create a property that initializes (C # 6 up), but only if you have only this member, if you have two members no longer works. The above example works.

In C # you need to be very careful about building structures. In C too, but in C it's worth for anything. Structure has peculiarities that many programmers do not understand.

One is that this structure will be 4 or 8 bytes long depending on the architecture and you never need to verify this . Like I said, it's not like C.

This structure in the way it is is completely unnecessary in C # and C as well.

I've improved the style, but there are still some weird things in the code.

    
27.11.2017 / 22:50
0

Complementing the LINQ response

public char[] nome;

In C # you can not initialize a field inside the struct, so you have received the error.

link

    
27.11.2017 / 22:38