'Marshaling C# struct to C++ dll - different values

I have a DLL written in C++ and I am trying to call a function that expects a struct from my C# project.

Here's its declaration

int _stdcall InitialiseMotors(int i_nBoard, Array_Of_Structures moveParameters)

And here is the declaration of the struct

typedef struct MoveMotorSolenoidParametersTag
{
    bool SetDirectionForMotor; 
    int SetNumberOfStepsForMotor;
    bool TurnSolenoidsON_OFF;
} MoveMotorSolenoidParameters;
        

The corresponding call in C#:

[DllImport(DllBeingUsed)]
private static extern int InitialiseMotors(int i_nBoard, 
DllDatatypes.MoveMotorSolenoidParameters moveMotorSolenoidParameters);

And the definition of my structure:

    [StructLayout(LayoutKind.Sequential)]
    public struct MoveMotorSolenoidParameters
    {
        public bool SetDirectionForMotor;
        public int SetNumberOfStepsForMotor;
        public bool TurnSolenoidsON_OFF;

        public MoveMotorSolenoidParameters(bool setDirectionForMotor = true, 
            int setNumberofStepsForMotor = 10,
            bool turnSolenoidsON_OFF = true )
        {
            SetDirectionForMotor = setDirectionForMotor;
            SetNumberOfStepsForMotor = setNumberofStepsForMotor;
            TurnSolenoidsON_OFF = turnSolenoidsON_OFF;
        }
    }

Even though I am passing the values true, 10 and true, my DLL seems to be getting different values. true, 167772160 and false. I can see these values by putting a breakpoint on the DLL. I checked the values before the DLL function is called and they are correct. So I am assuming something is happening during the marshalling.

QuickWatch of C++ struct

Any ideas why this is happening?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source