Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#封送C DLL结构bool_C#_C_Dll_Struct_Boolean - Fatal编程技术网

C#封送C DLL结构bool

C#封送C DLL结构bool,c#,c,dll,struct,boolean,C#,C,Dll,Struct,Boolean,我在封送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 {

我在封送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; 
}
在C中#

但是对于float类型,set和get值是正确的,而当我设置boll(例如true)时,我总是收到false 你能帮我吗?

试试:

[StructLayout(LayoutKind.Sequential)]
public struct BoolStruct
{
    [MarshalAs(UnmanagedType.U1)]
    public bool a;

    [MarshalAs(UnmanagedType.U1)]
    public bool b;

    public float c;
}

您应该总是指定<代码>结构布局>代码>(但是请注意,对于<代码>结构> <代码>:为了减少与自动值相关的布局相关问题,C语言、Visual Basic和C++编译器指定值类型的顺序布局)。 啊,您忘记了调用约定:对于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);

(同时测试了x86和x64)

我尝试了您的代码和建议,但不起作用。。。 我通过编辑文件CBool暂时解决了这个问题

typedef int boolean; /* false=0, otherwise true */
它真的起作用了! 在另一个类似的案例中

typedef struct {

    // don't work!
    //char TestString[5];

    // work!
    int TestString[5];

} MyStruct;
和C#


似乎char和unsigned是问题所在,或者我做错了什么,但不知道C对结构使用了什么包装?如果您不知道,请尝试不同的值,通过指定,
[DllImport(DLL_NAME,EntryPoint=“GetBStruct”,Pack=8)]
并尝试Pack=4、2和1。根据cbool的大小,您可以使用byte或int16匹配c结构,您应该为
BoolStruct
指定
[StructLayout(LayoutKind.Sequential)]
typedef struct {

    // don't work!
    //char TestString[5];

    // work!
    int TestString[5];

} MyStruct;
[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;  

}