我试图包装一个较旧的 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;
最佳答案
结构应该看起来像这样......
[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;
};
这是一个good article讨论应该有所帮助的结构和对齐方式。最主要的是结构布局和位对齐。自从我不得不从 C++ 中编码值以来已经有一段时间了,但我希望这会有所帮助。
关于c# - 如何将以下 c(++)-struct 转换为 C# 以供 p/invoke 使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5627730/