将 C# 结构编组为 C++ dll - 不同的值

发布于 2025-01-09 11:40:20 字数 1484 浏览 0 评论 0原文

我有一个用 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 函数之前检查了这些值,它们是正确的。所以我假设在编组过程中发生了一些事情。

C++ 结构的快速观察

有什么想法为什么会发生这种情况吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文