comp.lang.ada
 help / color / mirror / Atom feed
* Minimal pragma Export (StdCall... example?
@ 2004-03-11 13:31 Happy Segfault
  2004-03-11 14:03 ` Dmitry A. Kazakov
  2004-03-11 15:00 ` Frank
  0 siblings, 2 replies; 8+ messages in thread
From: Happy Segfault @ 2004-03-11 13:31 UTC (permalink / raw)


Hi all, 

I'm trying to write a DLL using GNAT 3.15p on Windows XP.  The DLL needs to 
export functions with the StdCall calling convention so that they can be 
called from Visual Basic.  Using samples/dll/ from the gwindows distribution 
as a starting point, I haven't been successful in getting this to work.  

If I just change the occurrences of pragma Export (C... to pragma Export 
(StdCall..., the Ada test driver program call_gw_dll.adb fails to link because 
the link library libgw_in_a_dll.a exports "_pop_a_window" only, while the 
linker wants "pop_a_window@0".  Neither of these is the "_pop_a_window@0" I 
would have expected to see for a StdCall function.  

Does anybody have a minimal set of code for a DLL, test program, and makefile 
that demonstrates building a StdCall DLL using GNAT on Windows?  

Best regards, 
John



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

* Re: Minimal pragma Export (StdCall... example?
  2004-03-11 13:31 Minimal pragma Export (StdCall... example? Happy Segfault
@ 2004-03-11 14:03 ` Dmitry A. Kazakov
  2004-03-12 12:50   ` Happy Segfault
  2004-03-11 15:00 ` Frank
  1 sibling, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2004-03-11 14:03 UTC (permalink / raw)


On 11 Mar 2004 05:31:24 -0800, happysegfault@yahoo.com (Happy
Segfault) wrote:

>I'm trying to write a DLL using GNAT 3.15p on Windows XP.  The DLL needs to 
>export functions with the StdCall calling convention so that they can be 
>called from Visual Basic.  Using samples/dll/ from the gwindows distribution 
>as a starting point, I haven't been successful in getting this to work.  
>
>If I just change the occurrences of pragma Export (C... to pragma Export 
>(StdCall..., the Ada test driver program call_gw_dll.adb fails to link because 
>the link library libgw_in_a_dll.a exports "_pop_a_window" only, while the 
>linker wants "pop_a_window@0".  Neither of these is the "_pop_a_window@0" I 
>would have expected to see for a StdCall function.  
>
>Does anybody have a minimal set of code for a DLL, test program, and makefile 
>that demonstrates building a StdCall DLL using GNAT on Windows?  

Why do you use C convention? It should be rather

pragma Export (StdCall, Local_Name [, "External_Name"]);

--
Regards,
Dmitry Kazakov
www.dmitry-kazakov.de



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

* Re: Minimal pragma Export (StdCall... example?
  2004-03-11 13:31 Minimal pragma Export (StdCall... example? Happy Segfault
  2004-03-11 14:03 ` Dmitry A. Kazakov
@ 2004-03-11 15:00 ` Frank
  2004-03-12 13:02   ` Happy Segfault
  1 sibling, 1 reply; 8+ messages in thread
From: Frank @ 2004-03-11 15:00 UTC (permalink / raw)


Hi!

Some weeks ago I had difficulties with the same and managed to compile/link
when I wrote the spec according to the following pattern,
when I worked with a DLL written in Delphi Pascal :

   pragma Import (Stdcall, UpdateAll, External_Name => "UpdateAll",
Link_Name => "_UpdateAll");


Unfortunately I havent tested the resulting executable since I do not have
the necessary HW to do it, but it compiled.
You could give it a try.

Frank





"Happy Segfault" <happysegfault@yahoo.com> wrote in message
news:ed66a020.0403110531.19b21907@posting.google.com...
> Hi all,
>
> I'm trying to write a DLL using GNAT 3.15p on Windows XP.  The DLL needs
to
> export functions with the StdCall calling convention so that they can be
> called from Visual Basic.  Using samples/dll/ from the gwindows
distribution
> as a starting point, I haven't been successful in getting this to work.
>
> If I just change the occurrences of pragma Export (C... to pragma Export
> (StdCall..., the Ada test driver program call_gw_dll.adb fails to link
because
> the link library libgw_in_a_dll.a exports "_pop_a_window" only, while the
> linker wants "pop_a_window@0".  Neither of these is the "_pop_a_window@0"
I
> would have expected to see for a StdCall function.
>
> Does anybody have a minimal set of code for a DLL, test program, and
makefile
> that demonstrates building a StdCall DLL using GNAT on Windows?
>
> Best regards,
> John





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

* Re: Minimal pragma Export (StdCall... example?
  2004-03-11 14:03 ` Dmitry A. Kazakov
@ 2004-03-12 12:50   ` Happy Segfault
  2004-03-12 13:36     ` Dmitry A. Kazakov
  0 siblings, 1 reply; 8+ messages in thread
From: Happy Segfault @ 2004-03-12 12:50 UTC (permalink / raw)


Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote in message news:<d6s0501t3r52o1aikiedgcbh65q2rfhuvr@4ax.com>...
> On 11 Mar 2004 05:31:24 -0800, happysegfault@yahoo.com (Happy
> Segfault) wrote:
> 
> >I'm trying to write a DLL using GNAT 3.15p on Windows XP.  The DLL needs to 
> >export functions with the StdCall calling convention so that they can be 
> >called from Visual Basic.  Using samples/dll/ from the gwindows distribution 
> >as a starting point, I haven't been successful in getting this to work.  
> >
> >If I just change the occurrences of pragma Export (C... to pragma Export 
> >(StdCall..., the Ada test driver program call_gw_dll.adb fails to link because 
> >the link library libgw_in_a_dll.a exports "_pop_a_window" only, while the 
> >linker wants "pop_a_window@0".  Neither of these is the "_pop_a_window@0" I 
> >would have expected to see for a StdCall function.  
> >
> >Does anybody have a minimal set of code for a DLL, test program, and makefile 
> >that demonstrates building a StdCall DLL using GNAT on Windows?  
> 
> Why do you use C convention? It should be rather
> 
> pragma Export (StdCall, Local_Name [, "External_Name"]);


That's what I'm trying to do, and I'm having the problems above.  

John



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

* Re: Minimal pragma Export (StdCall... example?
  2004-03-11 15:00 ` Frank
@ 2004-03-12 13:02   ` Happy Segfault
  0 siblings, 0 replies; 8+ messages in thread
From: Happy Segfault @ 2004-03-12 13:02 UTC (permalink / raw)


"Frank" <franjoe@frisurf.no> wrote in message news:<40507f55$1@news.wineasy.se>...
> Hi!
> 
> Some weeks ago I had difficulties with the same and managed to compile/link
> when I wrote the spec according to the following pattern,
> when I worked with a DLL written in Delphi Pascal :
> 
>    pragma Import (Stdcall, UpdateAll, External_Name => "UpdateAll",
> Link_Name => "_UpdateAll");
> 
> 
> Unfortunately I havent tested the resulting executable since I do not have
> the necessary HW to do it, but it compiled.
> You could give it a try.
> 
> Frank

I've found that Visual Basic can successfully call functions in the DLL.  
By using objdump to check the symbol table, the (I think) properly decorated 
function name is there: _pop_a_window@0.  A call to "pop_a_window" in VB 
works just fine.  

I don't think the DLL itself is a problem---it has the right function names.  
There are two other parts, and neither one seems to do what I expect.  The 
link library that gnatdll produces has only _pop_a_window (leaves off the 
@0), and the pragma Import (Stdcall, Pop_A_Window, "pop_a_window") in 
the test app causes the linker to look for pop_a_window@0 in the link 
library.  Both should be _pop_a_window@0 if they're really StdCall, right?  

I've tried several combinations of linker options on gnatdll, but I 
wasn't able to get it to export _pop_a_window@0 in the link library.  I was 
hoping someone had a minimal working example of Stdcall export and import 
(especially the makefile).  Anybody?  

Thanks for the tip!  

John



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

* Re: Minimal pragma Export (StdCall... example?
  2004-03-12 12:50   ` Happy Segfault
@ 2004-03-12 13:36     ` Dmitry A. Kazakov
  2004-03-14  1:01       ` Happy Segfault
  0 siblings, 1 reply; 8+ messages in thread
From: Dmitry A. Kazakov @ 2004-03-12 13:36 UTC (permalink / raw)


On 12 Mar 2004 04:50:59 -0800, happysegfault@yahoo.com (Happy
Segfault) wrote:

>Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote in message news:<d6s0501t3r52o1aikiedgcbh65q2rfhuvr@4ax.com>...
>> On 11 Mar 2004 05:31:24 -0800, happysegfault@yahoo.com (Happy
>> Segfault) wrote:
>> 
>> >I'm trying to write a DLL using GNAT 3.15p on Windows XP.  The DLL needs to 
>> >export functions with the StdCall calling convention so that they can be 
>> >called from Visual Basic.  Using samples/dll/ from the gwindows distribution 
>> >as a starting point, I haven't been successful in getting this to work.  
>> >
>> >If I just change the occurrences of pragma Export (C... to pragma Export 
>> >(StdCall..., the Ada test driver program call_gw_dll.adb fails to link because 
>> >the link library libgw_in_a_dll.a exports "_pop_a_window" only, while the 
>> >linker wants "pop_a_window@0".  Neither of these is the "_pop_a_window@0" I 
>> >would have expected to see for a StdCall function.  
>> >
>> >Does anybody have a minimal set of code for a DLL, test program, and makefile 
>> >that demonstrates building a StdCall DLL using GNAT on Windows?  
>> 
>> Why do you use C convention? It should be rather
>> 
>> pragma Export (StdCall, Local_Name [, "External_Name"]);
>
>That's what I'm trying to do, and I'm having the problems above.  

The pragma Export gives you an opportunity to explicitly specify the
name used in the DLL. So you can name your Pop_A_Window as
"pop_a_window@0". You can also move it to Visual Basic side, where you
can explicitly specify the link name:

Declare Function Pop_A_Window Lib "my_Ada.dll" Alias "_pop_a_window"
(...) As Long

Probably you want to automate it and omit specifying any link name
altogether. I doubt it that is possible.

Though you can always buy GNAT Pro and try to convince ACT (GNAT
vendor) to implement

pragma Export/Import (VisualBasic, ...);

--
Regards,
Dmitry Kazakov
www.dmitry-kazakov.de



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

* Re: Minimal pragma Export (StdCall... example?
  2004-03-12 13:36     ` Dmitry A. Kazakov
@ 2004-03-14  1:01       ` Happy Segfault
  2004-03-16 13:54         ` Happy Segfault
  0 siblings, 1 reply; 8+ messages in thread
From: Happy Segfault @ 2004-03-14  1:01 UTC (permalink / raw)


Dmitry A. Kazakov <mailbox@dmitry-kazakov.de> wrote in message news:<2de350pqsadlrvvgkrpc7261k31l5ejuap@4ax.com>...
> 
> The pragma Export gives you an opportunity to explicitly specify the
> name used in the DLL. So you can name your Pop_A_Window as
> "pop_a_window@0". You can also move it to Visual Basic side, where you
> can explicitly specify the link name:
> 
> Declare Function Pop_A_Window Lib "my_Ada.dll" Alias "_pop_a_window"
> (...) As Long
> 
> Probably you want to automate it and omit specifying any link name
> altogether. I doubt it that is possible.
> 
> Though you can always buy GNAT Pro and try to convince ACT (GNAT
> vendor) to implement
> 
> pragma Export/Import (VisualBasic, ...);

I took a look at glBegin() as an example Stdcall function.  It appears in 
Windows' OPENGL32.DLL as glBegin, not _glBegin or _glBegin@4, according to 
objdump -p.  It is pramga-Import'ed in win32-gl.ads in my GNAT distribution 
with the command pragma Import (Stdcall, glBegin, "glBegin");  If I take a 
look at the symbol table for the GNAT-supplied import library libopengl32.a, 
using objdump -t, I find it listed as _glBegin@4 and not also as glBegin 
or glBegin@4.  

If I could replicate this with my own combination of a pragma Export (Stdcall, 
... and the right gnatmake, gnatdll (or directly gnatlink, gnatbind, dlltool) 
options, I think I'd have it.  Unfortunately, I've got pop_a_window@0 in my 
DLL (bad) and _pop_a_window@0 in my import library (good) at this point, 
which messes up the import into Visual Basic.  If I leave off the @0 in the 
def file, I can get pop_a_window in the DLL (good) but then have 
_pop_a_window in the import library (bad).  

Any ideas, anyone?  

Thanks for the reply!  After checking out your homepage, I'm pretty sure 
you're ten times the programmer I am!  

John



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

* Re: Minimal pragma Export (StdCall... example?
  2004-03-14  1:01       ` Happy Segfault
@ 2004-03-16 13:54         ` Happy Segfault
  0 siblings, 0 replies; 8+ messages in thread
From: Happy Segfault @ 2004-03-16 13:54 UTC (permalink / raw)


I've got a minimal almost-working example now.  In case someone else 
goes looking for one later, I'll paste it in here.  Of course, if I'm 
doing something wrong, I'd appreciate someone pointing it out.  

There is some weirdness, however.  There is a seemingly 
GWindows-related access violation exception being thrown for some 
variations of this code.  I cannot figure it out!  

-- gw_in_a_dll.def
LIBRARY gw_in_a_dll
EXPORTS
        pop_a_window=pop_a_window@0
        pop_a_window@0
        pop_a_window_int=pop_a_window_int@4
        pop_a_window_int@4

-- makefile
PACKAGES = gw_in_a_dll call_gw_dll
 
ADAOPTS = -I../../bindings/
 
all: gw_in_a_dll call_gw_dll
 
gw_in_a_dll:
        gnatmake ${ADAOPTS} gw_in_a_dll ${POSTOPTS}
        gnatdll -egw_in_a_dll.def -dgw_in_a_dll.dll gw_in_a_dll.ali
 
call_gw_dll:
        gnatmake ${ADAOPTS} call_gw_dll ${POSTOPTS}
 
-- gw_in_a_dll.ads
-- Demonstrates using GWindows in a DLL
--
-- Modified from original GWindows version to use Stdcall calling
-- convention and to test parameter passing
--
-- This version does not work...
 
with Interfaces.C;
 
package GW_In_A_DLL is
 
   procedure Pop_A_Window;
   pragma Export (Stdcall, Pop_A_Window);
 
   procedure Pop_A_Window_Int(I : Interfaces.C.Int);
   pragma Export (Stdcall, Pop_A_Window_Int);
 
end GW_In_A_DLL;

-- gw_in_a_dll.adb
-- Demonstrates using GWindows in a DLL
--
-- Modified from original GWindows version to use Stdcall calling 
-- convention and to test parameter passing
--
-- This version does not work...
 
with Interfaces.C;
 
with GWindows.Application;
with GWindows.Windows;
with GWindows.GStrings;
 
package body GW_In_A_DLL is
 
   DLL_PROCESS_DETACH : constant := 0;
   DLL_PROCESS_ATTACH : constant := 1;
 
   procedure Adainit;
   pragma Import (C, Adainit);
 
   procedure Adafinal;
   pragma Import (C, Adafinal);
                                                                                
   function DllMain
     (hinstDLL    : Interfaces.C.long;
      fdwReason   : Interfaces.C.unsigned_short;
      lpvReserved : Integer)
     return Interfaces.C.int;
   pragma Export (StdCall, DllMain, "DllMain");
 
   -------------
   -- DllMain --
   -------------
 
   function DllMain
     (hinstDLL    : Interfaces.C.long;
      fdwReason   : Interfaces.C.unsigned_short;
      lpvReserved : Integer)
     return Interfaces.C.int
   is
      pragma Warnings (Off, lpvReserved);
   begin
      case fdwReason is
         when DLL_PROCESS_ATTACH =>
            GWindows.Application.Set_hInstance (HinstDLL);
            return 1;
         when DLL_PROCESS_DETACH =>
            Adafinal;
            return 1;
         when others =>
            return 1;
      end case;
   end DllMain;
 
   ------------------
   -- Pop_A_Window --
   ------------------
 
   procedure Pop_A_Window is
      use GWindows.Windows;
      use GWindows.GStrings;
 
      Window : Window_Type;
      J : constant Interfaces.C.Int := 2;
   begin
      Adainit;
 
      Create (Window,
   -- To_GString_From_String("Your number is " & "not here"),
   -- To_GString_From_String("Your number is not here"),
   -- To_GString_From_String("Your number defaults to " & J'Img),
   -- Any of the above will crash with an access violation                          
   "Your number is " & "not here",
   -- This line works fine!
   Width => 300, Height => 100);
      Visible (Window);
 
      GWindows.Application.Show_Modal (Window);
   end Pop_A_Window;
 
   ----------------------
   -- Pop_A_Window_Int --
   ----------------------
 
   procedure Pop_A_Window_Int(I : Interfaces.C.Int) is
      use GWindows.Windows;
      use GWindows.GStrings;
 
      Window : Window_Type;
      J : constant Interfaces.C.Int := 222;
   begin
      Adainit;
 
      Create (Window,
   -- To_GString_From_String("Your number is " & J'Img),
   To_GString_From_String("Your number is " & I'Img),
   -- To_GString_From_String("Your number is not here either"),
   -- All of the above work from call_gw_dll.exe, but this 
   -- procedure crashes Excel if I call it from Visual Basic.  
   Width => 300, Height => 100);
      Visible (Window);
 
      GWindows.Application.Show_Modal (Window);
   end Pop_A_Window_Int;
 
end GW_In_A_DLL;
 
-- call_gw_dll.adb
with Ada.Text_IO;
with Interfaces.C;
 
procedure Call_GW_DLL is
   pragma Linker_Options ("-lgw_in_a_dll");
 
   procedure Pop_A_Window;
   pragma Import (Stdcall, Pop_A_Window);
 
   procedure Pop_A_Window_Int(I : Interfaces.C.Int);
   pragma Import (Stdcall, Pop_A_Window_Int);
 
   I : constant Interfaces.C.Int := 3;
begin
   Ada.Text_IO.Put_Line("Doing Pop_A_Window");
   Pop_A_Window;
   Ada.Text_IO.Put_Line("Doing Pop_A_Window_Int(I)");
   Pop_A_Window_Int(3);
   Ada.Text_IO.Put_Line("Done!");
 
end Call_GW_DLL;



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

end of thread, other threads:[~2004-03-16 13:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-03-11 13:31 Minimal pragma Export (StdCall... example? Happy Segfault
2004-03-11 14:03 ` Dmitry A. Kazakov
2004-03-12 12:50   ` Happy Segfault
2004-03-12 13:36     ` Dmitry A. Kazakov
2004-03-14  1:01       ` Happy Segfault
2004-03-16 13:54         ` Happy Segfault
2004-03-11 15:00 ` Frank
2004-03-12 13:02   ` Happy Segfault

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