[英]C# Marshaling C DLL struct bool
i have some trouble with bool while Marshaling C DLL struct this is code in C DLL 我在编组C DLL结构时遇到了布尔问题,这是C DLL中的代码
// cbool.h
#ifndef CBOOL_H
#define CBOOL_H
#define false 0
#define true !false
typedef unsigned char boolean; /* false=0, otherwise true */
#endif
// test.h
#include "cbool.h"
typedef struct
{
bool a;
bool b;
float c;
} BoolStruct;
extern BoolStruct BStruct;
__declspec(dllexport) void GetBStruct (BoolStruct* bs);
__declspec(dllexport) void SetBStruct (BoolStruct* bs);
// test.c
#include "test.h"
BoolStruct BStruct;
void GetBStruct (BoolStruct* bs)
{
*bs = BStruct;
}
void SetBStruct (BoolStruct* bs)
{
BStruct = *bs;
}
and in C# 和在C#中
public struct BoolStruct
{
public bool a;
public bool b;
public float c;
}
BoolStruct bs;
[DllImport(DLL_NAME, EntryPoint = "GetBStruct")]
public static extern void GetBStruct(ref BoolStruct bs);
[DllImport(DLL_NAME, EntryPoint = "SetBStruct")]
public static extern void SetBStruct(ref BoolStruct bs);
bs.a = true;
bs.c = 0.5F;
... set
...get
But with float type set and get values are correct, while when i set boll (eg true) i receive always false Can you help me? 但是使用float类型设置并获取值正确,而当我设置boll(例如true)时,我总是收到false可以帮我吗?
i try your codes and suggestions, but don't work... I temporarily resolved by editing the file CBool 我尝试了您的代码和建议,但不起作用...我通过编辑文件CBool暂时解决了
typedef int boolean; /* false=0, otherwise true */
and it work! 而且有效! in another similar case
在另一个类似的情况下
typedef struct {
// don't work!
//char TestString[5];
// work!
int TestString[5];
} MyStruct;
and C# 和C#
[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Ansi)]
public struct MyStruct
{
public MyStruct(uint i)
{
// ......
TestString = new char[5];
}
[MarshalAs(UnmanagedType.ByValArray, SizeConst=5)]
public char[] TestString;
}
it seems that char and unsigned are the problems, or am I doing wrong somethin 'else, but do not know what 似乎char和unsigned是问题所在,还是我在做其他错误的事,但不知道是什么
Try: 尝试:
[StructLayout(LayoutKind.Sequential)]
public struct BoolStruct
{
[MarshalAs(UnmanagedType.U1)]
public bool a;
[MarshalAs(UnmanagedType.U1)]
public bool b;
public float c;
}
By default the bool
is marshaled as an Int32
in .NET. 默认情况下,
bool
在.NET中被封为Int32
。
And you should always specify the StructLayout
when you do PInvoke (but note that for struct
it isn't necessary : To reduce layout-related problems associated with the Auto value, C#, Visual Basic, and C++ compilers specify Sequential layout for value types. ) 而且,在执行PInvoke时,您应该始终指定
StructLayout
(但请注意,对于struct
它不是必需的 : 要减少与Auto值相关的与布局有关的问题,C#,Visual Basic和C ++编译器为值类型指定顺序布局。 )
Ah and you forgot the CallingConvention
: normally it is Cdecl
for C functions: 啊,您忘记了
CallingConvention
:通常,对于C函数,它是Cdecl
:
[DllImport("CPlusPlusSide.dll", EntryPoint = "GetBStruct", CallingConvention = CallingConvention.Cdecl)]
public static extern void GetBStruct(ref BoolStruct bs);
[DllImport("CPlusPlusSide.dll", EntryPoint = "SetBStruct", CallingConvention = CallingConvention.Cdecl)]
public static extern void SetBStruct(ref BoolStruct bs);
(tested both x86 and x64) (同时测试了x86和x64)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.