comp.lang.ada
 help / color / mirror / Atom feed
* StdCall and pragma Import_Function
@ 2011-08-19 16:24 Felix Krause
  2011-08-19 16:51 ` Dmitry A. Kazakov
  2011-08-19 18:10 ` Jeffrey Carter
  0 siblings, 2 replies; 16+ messages in thread
From: Felix Krause @ 2011-08-19 16:24 UTC (permalink / raw)


I'm writing a cross-platform Ada binding for a C library. It currently works on OSX and Linux, and I'm trying to get it to work on Windows. I have some code like this:

package P is
   type ULong is mod 2 ** 64;
   for ULong'Size use 64;

   type Bit_Vector is record
      First_Bit  : Boolean;
      Second_Bit : Boolean;
   end record;
   for Bit_Vector use record
      First_Bit  at 0 range 0 .. 0;
      Second_Bit at 0 range 1 .. 1;
   end record;
   for Bit_Vector'Size use ULong'Size;

   function C_Function (Param : Bit_Vector) return ULong;
   pragma Import (StdCall, C_Function, "cFunc");
   pragma Import_Function (Internal => C_Function, External => "cFunc", Mechanism => (Param => Value));
end P;

On Linux and OSX, this works without problems. On Windows however, I get this error: "undefined reference to 'cFunc@4'". The GNAT user manual tells me that for StdCall, the number behind the '@' is the total number of bytes used by the function parameters. Now I told the compiler quite explicitly that Param should be passed by value and therefore needs 8 bytes, not 4 bytes.

I'm not sure what goes wrong here. Is Import_Function ignored on Windows? As I compile for 32bit, the number of bytes used by Param would be 4 if it was passed by reference.

Of course, this example is simplified and the actual C function has more parameters that could be the cause of the wrong byte number, but if I use "Param : ULong" for the function definition, it works, so I'm pretty sure the problem lies in the usage of Bit_Vector for the parameter. I also used dumpbin.exe to extract the correct function names in the C library .lib file, and indeed, the symbol name is "_cFunc@8".

So, I do have a possibility to work around this problem; namely defining C_Function having a ULong parameter and then converting all Bit_Vector variables to ULong when calling C_Function. But I'm still curious what the problem is here, and if there is an easier workaround. Can anybody tell me?

Thanks,
Felix



^ permalink raw reply	[flat|nested] 16+ messages in thread

end of thread, other threads:[~2011-08-21 18:25 UTC | newest]

Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-08-19 16:24 StdCall and pragma Import_Function Felix Krause
2011-08-19 16:51 ` Dmitry A. Kazakov
2011-08-19 17:07   ` Felix Krause
2011-08-19 18:58     ` Dmitry A. Kazakov
2011-08-19 19:23       ` Felix Krause
2011-08-19 19:43         ` Dmitry A. Kazakov
2011-08-19 20:07           ` Felix Krause
2011-08-19 20:37             ` Dmitry A. Kazakov
2011-08-19 20:45             ` Jeffrey Carter
2011-08-21 18:25               ` Felix Krause
2011-08-20  8:04           ` Georg Bauhaus
2011-08-20  9:23             ` Dmitry A. Kazakov
2011-08-19 17:26   ` Adam Beneschan
2011-08-19 17:38     ` Felix Krause
2011-08-19 18:10 ` Jeffrey Carter
2011-08-19 18:37   ` Felix Krause

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox