From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.9 required=5.0 tests=BAYES_00,FORGED_GMAIL_RCVD, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: 103376,87b8cfa03eafd839,start X-Google-Attributes: gid103376,public X-Google-Language: ENGLISH,ASCII-7-bit Path: g2news1.google.com!postnews.google.com!t31g2000cwb.googlegroups.com!not-for-mail From: "cybersaga" Newsgroups: comp.lang.ada Subject: DLL Troubles Date: 19 Mar 2006 10:46:14 -0800 Organization: http://groups.google.com Message-ID: <1142793974.914652.154480@t31g2000cwb.googlegroups.com> NNTP-Posting-Host: 69.49.45.202 Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" X-Trace: posting.google.com 1142793980 10114 127.0.0.1 (19 Mar 2006 18:46:20 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 19 Mar 2006 18:46:20 +0000 (UTC) User-Agent: G2/0.2 X-HTTP-UserAgent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.1) Gecko/20060111 Firefox/1.5.0.1,gzip(gfe),gzip(gfe) Complaints-To: groups-abuse@google.com Injection-Info: t31g2000cwb.googlegroups.com; posting-host=69.49.45.202; posting-account=2ELL3w0AAABA6Hu9BtxJuf-wgpQRCSl8 Xref: g2news1.google.com comp.lang.ada:3456 Date: 2006-03-19T10:46:14-08:00 List-Id: 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?