以下 c(++)-struct 如何转换为 C# 以供 p/invoke 使用

发布于 2024-11-01 07:20:24 字数 1235 浏览 2 评论 0原文

我试图包装一个较旧的 dll,但遇到了表示它在 C# 中使用的结构的问题。我尝试过的一切似乎都不起作用。有魔法师可以帮忙吗?

typedef struct _PARAM_BYNAME_DATA 
{ 
    n_char *szPntName; /* (in) point name */ 
    n_char *szPrmName; /* (in) parameter name */ 
    n_long nPrmOffset; /* (in) parameter offset */ 
    PARvalue *pupvValue; /* (in/out) parameter value union */ 
    n_ushort nType; /* (in/out) value type */ 
    n_long fStatus; /* (out) status of each value access */ 
} PARAM_BYNAME_DATA; 

如果有帮助的话,下面是一个 VB 端口。

Type param_byname_data 
    point_name As String
    param_name As String
    param_offset As Long
    padding1 As Long 'for byte alignment between VB and C 
     param_value As Variant
    param_type As Integer
    padding2 As Integer 'for byte alignment between VB and C 
    status As Long status As Long 
End Type

还有下面的 Delphi...

PARAM_BYNAME_DATA=record
    PntName:pchar;       // (in) point name
    PrmName:pchar;       // (in) parameter name
    PrmOffset:longword;  // (in) parameter offset
    pValue:pointer;      // (out) parameter value union
    nType:word;          // (out) value type
    fStatus:longword;    // (out) status of each value access */
end;

Im trying to wrap an older dll and am running into issues representing a structure it uses in C#. Nothing I have tried seems to be working. Any magicians able to help?

typedef struct _PARAM_BYNAME_DATA 
{ 
    n_char *szPntName; /* (in) point name */ 
    n_char *szPrmName; /* (in) parameter name */ 
    n_long nPrmOffset; /* (in) parameter offset */ 
    PARvalue *pupvValue; /* (in/out) parameter value union */ 
    n_ushort nType; /* (in/out) value type */ 
    n_long fStatus; /* (out) status of each value access */ 
} PARAM_BYNAME_DATA; 

If it helps the below is a VB port.

Type param_byname_data 
    point_name As String
    param_name As String
    param_offset As Long
    padding1 As Long 'for byte alignment between VB and C 
     param_value As Variant
    param_type As Integer
    padding2 As Integer 'for byte alignment between VB and C 
    status As Long status As Long 
End Type

And the following Delphi as well...

PARAM_BYNAME_DATA=record
    PntName:pchar;       // (in) point name
    PrmName:pchar;       // (in) parameter name
    PrmOffset:longword;  // (in) parameter offset
    pValue:pointer;      // (out) parameter value union
    nType:word;          // (out) value type
    fStatus:longword;    // (out) status of each value access */
end;

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少年亿悲伤 2024-11-08 07:20:24

该结构应该看起来像这样...

[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
    public string point_name;
    public string param_name;
    public Int32 param_offset;
    public VariantWrapper param_value;
    public Int32 param_type;
    public Int32 status;
};

这是一篇 好文章< /a> 讨论了应该有帮助的结构和对齐。主要的是结构布局和位对齐。自从我不得不从 C++ 中整理值以来已经有一段时间了,但我希望这会有所帮助。

The struct should look something like this...

[StructLayout(LayoutKind.Sequential)]
public struct MyStruct
{
    public string point_name;
    public string param_name;
    public Int32 param_offset;
    public VariantWrapper param_value;
    public Int32 param_type;
    public Int32 status;
};

Here is a good article that talks about structs and alignment that should help. The main thing is the struct layout and the bit alignment. It has been a while since I have had to marshal values out of C++ but I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文