0

We using a private source library written in cpp with a binding in C# with some structs like this for getting the data from the cpp library into c# structs, but the problem is that i don't know how to unpack the CPP Marshalled buffer in Nodej.js :

The struct in C# :

[StructLayout(LayoutKind.Sequential, Pack = 1)]
unsafe public struct TheClass
{
    public byte XXX;
    public int XXX;
    public int XXX;
    public ushort XXX;
    public byte XXX;
    public byte XXX;
    public byte XXX;
    public byte XXX;
    public byte XXX;
    public byte XXX;
    public fixed byte XXX[12];
    public fixed byte XXX[5];
    public short XXX;
}

Thank for your help :)

0

1 Answer 1

2

Using the npm struct package:

const TheClass = Struct()
  .word8('XXX')
  .word32Sle('XXX')
  .word32Sle('XXX')
  .word16Ule('XXX')
  .word8('XXX')
  .word8('XXX')
  .word8('XXX')
  .word8('XXX')
  .word8('XXX')
  .word8('XXX')
  .array('XXX', 12, 'word8')
  .array('XXX', 5, 'word8')
  .word16Sle('XXX');

Of course, replace all the XXX with the correct field names.

Then you can use this object to parse the contents of a Buffer object:

TheClass._setBuff(yourBuffer);

// Read the data for field XXX (using a valid name).
var someField = TheClass.get('XXX');

// Or use the proxy object:
var someOtherField = TheClass.fields.XXX;

Using lodash you can write a helper method that will parse and return a copy of the struct's data:

Struct.prototype.parse = function (buffer) {
  const oldBuff = this.buffer();

  this._setBuff(buffer);
  const v = _.cloneDeep(this.fields);

  this._setBuff(oldBuff);

  return v;
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank is that i needed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.