comp.lang.ada
 help / color / mirror / Atom feed
* DLL Troubles
@ 2006-03-19 18:46 cybersaga
  2006-03-19 19:22 ` tmoran
  2006-03-19 19:37 ` Jeffrey R. Carter
  0 siblings, 2 replies; 9+ messages in thread
From: cybersaga @ 2006-03-19 18:46 UTC (permalink / raw)


I have a DLL that gets dynamically loaded (in Windows) and contains a
function to return a random number as a subset of what it's supposed to
do.
However, it does not work. I keep getting a "raised PROGRAM_ERROR :
EXCEPTION_ACCESS_VIOLATION" when the DLL tries to reset the random
seed. I can do it fine within the main program, but not within the DLL.

The line in question is "RandomNum.Reset(Seed);". Looking at the call
stack when it dies, it shows that the Reset procedure calls
Calendar.Year (from Ada.Calendar), which calls Calendar.Split, which is
where the exception gets thrown.

I whipped up a stripped down proof of concept of the problem. Here is
the code for the DLL:
-----mydll.ads---------------------------------------
package MyDll is

   function GetRandom(Bottom, Top : Integer) return Integer;

   pragma Export_Function (
      Internal => GetRandom,
      External => "GetRandom",
      Parameter_Types => (Integer, Integer),
      Result_Type => Integer);

end MyDll;
------------------------------------
-----mydll.adb---------------------------------------
with Ada.Numerics.Discrete_Random;

package body MyDll is

   function GetRandom(Bottom, Top : Integer) return Integer is
      type NumRange is new Integer range Bottom .. Top;
      package RandomNum is new Ada.Numerics.Discrete_Random(NumRange);
      Seed : RandomNum.Generator;
   begin
      RandomNum.Reset(Seed);                             --It dies in
here
      return Integer( RandomNum.Random(Seed) );
   end GetRandom;

end MyDll;
------------------------------------

Here is the code for the main program:
-----bugtest.adb---------------------------------------
with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;
with Ada.Unchecked_Conversion;
with Ada.Numerics.Discrete_Random;
with Win32; use Win32;
with Win32.Windef; use Win32.Windef;
with Win32.Winbase; use Win32.Winbase;

procedure bugtest is
   type RandomF is access function(Bottom,Top : Integer) return
Integer;
   function Convert is new Ada.Unchecked_Conversion(FARPROC, RandomF);
   MyRandomF : RandomF;
   hDll : HINSTANCE;
   my_rand : Integer;

   type NumRange is new Integer range 1 .. 5;
   package RandomNum is new Ada.Numerics.Discrete_Random(NumRange);
   Seed : RandomNum.Generator;
begin
   --put out random number from main program (works fine)
   RandomNum.Reset(Seed);
   put(Integer(RandomNum.Random(Seed)));

   --now try through DLL (dies)
   hDll := LoadLibrary(Addr(".\dll\mydll.dll" & ASCII.NUL));
   MyRandomF := Convert( GetProcAddress(hDll, Addr("GetRandom" &
ASCII.NUL)));

   my_rand := MyRandomF.all(1,5);
   Put(my_rand);
end bugtest;
------------------------------------

Does anyone have any thoughts on why this is happening, and why only
from the DLL and not the main program?




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

end of thread, other threads:[~2006-03-20  1:10 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-19 18:46 DLL Troubles cybersaga
2006-03-19 19:22 ` tmoran
2006-03-19 19:27   ` cybersaga
2006-03-19 19:30     ` cybersaga
2006-03-19 20:13       ` Pascal Obry
2006-03-19 20:23         ` cybersaga
2006-03-19 21:19           ` cybersaga
2006-03-20  1:10             ` cybersaga
2006-03-19 19:37 ` Jeffrey R. Carter

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