将 C# 结构编组为 C++ dll - 不同的值
我有一个用 C++ 编写的 DLL,我试图调用一个需要来自我的 C# 项目的结构的函数。
这是它的声明
int _stdcall InitialiseMotors(int i_nBoard, Array_Of_Structures moveParameters)
这是结构的声明
typedef struct MoveMotorSolenoidParametersTag
{
bool SetDirectionForMotor;
int SetNumberOfStepsForMotor;
bool TurnSolenoidsON_OFF;
} MoveMotorSolenoidParameters;
C# 中的相应调用:
[DllImport(DllBeingUsed)]
private static extern int InitialiseMotors(int i_nBoard,
DllDatatypes.MoveMotorSolenoidParameters moveMotorSolenoidParameters);
以及我的结构的定义:
[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;
}
}
尽管我传递了值 true、10 和 true,但我的 DLL 似乎得到了不同的值。正确、167772160 和错误。我可以通过在 DLL 上放置断点来查看这些值。我在调用 DLL 函数之前检查了这些值,它们是正确的。所以我假设在编组过程中发生了一些事情。
有什么想法为什么会发生这种情况吗?
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.
Any ideas why this is happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论