Pass the Boolean Ada type in the interfaces. C

I would now like to pass the standard Boolean Type to Ada through the Interfaces.C package to call the DLL function. The Interfaces.C package does not contain an Ada buffer type, since the logical type does not exist in ANSI C. I have a DLL function written in C ++, the exported function prototype has an argument of type Bool. How is this passed in the Intefaces.C package to call the DLL export function?

+3
source share
2 answers

traditionally, in C, a boolean is represented with a type int. in C ++, a type is defined bool, but pay attention to the lower case "b". if your argument is of type bool(note the uppercase "B"), you should have a class declaration somewhere (in your C ++ library) that will tell you more about the implementation of this type. as a rule, such a Bool class is compatible with the standard bool type through some language artifact: the Bool class does not define a virtual function and stores its value in a member that is reasonably placed first in the class definition.

I have not tested it, but I would say that you can go with type int. do a test to make sure it is accepted by the C ++ function. to convert from boolean to intyou can use something like Interfaces.C.int(Boolean'Pos(your_bool_value)).

, , Ada [B.3.62]: An implementation may provide additional declarations in the C interface packages., Interfaces.C, bool.

+4
type Boolean_2_Int is (Bool : Boolean := False) is
record
    I : Interfaces.C.Unsigned_Long := 0;
    case Bool is
       when True => 
          I := 1;
       when False => 
          I := 0;
    end case;
end record;
0

Source: https://habr.com/ru/post/1723103/


All Articles