Tyler Durden
Tyler Durden

Reputation: 1228

Translating C++ structure to C#

I have a structure definition in C++ as follows:

struct check1
{
  check1(USHORT vaultLen)
  {
    size = sizeof(*this->vaultLen) + vaultLen + sizeof(*this->digestKey);
    buffer = new UCHAR[size];

    this->vaultLen = (USHORT*)buffer;
    this->vaultData = buffer + sizeof(vaultLen);
    this->digestKey = (UCHAR(*)[8])(buffer + sizeof(vaultLen) + vaultLen);

    *(this->vaultLen) = vaultLen;
  }


  USHORT *vaultLen;
  UCHAR *vaultData;
  UCHAR (*digestKey)[8];

  UCHAR* buffer;
  USHORT size;
}

I don't wish to use unsafe code so pointers are not allowed. What would be an equivalent structure in C#? Do the members defined as pointers actually take up space?

With respect to how this struct is used, an object of this struct is created and its size member is passed to an int.

Upvotes: 1

Views: 171

Answers (1)

Zac Howland
Zac Howland

Reputation: 15870

An equivalent C# structure that is not used for interop would look something like:

public class check1
{
    public byte[] digestKey = new byte[8];
    public List<byte> vaultData;
}

The other members are not necessary as buffer is just a memory block to hold digestKey and vaultData, and the others are size buffers to allow quick access to the proper locations in the buffer for the various data members.

Upvotes: 1

Related Questions