1. Bad Routine Number
- Posted by Icy_Viking Jun 01, 2019
- 1533 views
Hello,
So I recently went to check my SDL wrapper while using the new Euphoria 4.1.0 Beta 2 and noticed that I get a bad routine number -1 when trying to run a example program. This did not happen using 4.0.5.
--Wrapper Code atom sdl2 ifdef WIN32 then sdl2 = open_dll("SDL2.dll") elsifdef LINUX or FREEBSD then sdl2 = open_dll("SDL2.so") end ifdef if sdl2 = -1 then puts(1,"Failed to load SDL2.dll!\n") abort(0) end if public constant xSDL_Init = define_c_func(sdl2,"SDL_Init",{C_UINT},C_INT) public function SDL_Init(atom flags) return c_func(xSDL_Init,{flags}) end function
--Sample Program include std/machine.e include EuSDL2.ew include flags.e atom width = 640, height = 480 --Init SDL2 if SDL_Init(SDL_INIT_EVERYTHING) = -1 then --Error appears to be coming from here (This worked fine in 4.0.5) puts(1,"Could not init SDL2!\n") abort(0) end if --Window creation atom win = SDL_CreateWindow("Basic Window - Will close after 3 Seconds",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN) if win = -1 then puts(1,"Failed to create window!\n") abort(0) end if SDL_Delay(3000) -- make the window appear for three seconds --Cleanup SDL_DestroyWindow(win) SDL_Quit()
2. Re: Bad Routine Number
- Posted by irv Jun 01, 2019
- 1499 views
if sdl2 < 1 then -- not -1 puts(1,"Failed to load SDL2.dll!\n") abort(0) end if
An atom, actually a 32-bit address. 0 is returned if the .dll can't be found.
3. Re: Bad Routine Number
- Posted by ghaberek (admin) Jun 01, 2019
- 1519 views
So I recently went to check my SDL wrapper while using the new Euphoria 4.1.0 Beta 2 and noticed that I get a bad routine number -1 when trying to run a example program. This did not happen using 4.0.5.
Since 4.1 is both 32-bit and 64-bit, make sure you're not mixing versions of DLLs. 32-bit applications can only load 32-bit DLLs and vice-versa. Chances are you've got 64-bit Euphoria and 32-bit library, or again, vice-versa.
An atom, actually a 32-bit address. 0 is returned if the .dll can't be found.
To elaborate, open_dll() is supposed to return an in-memory address to the library that it's opened, so it returns NULL (0) if the library cannot be found. Conversely, define_c_func/proc() return an ordinal number of the routine as it's stored in the table of known routines, so it returns -1 if the routine cannot be found in the library. Finally define_c_var() also expects to return an in-memory address of the variable it found in the library, but for some reason it will return -1 and not NULL if the variable cannot be found. A lot of this is a side effect of the backend internals (see be_machine.c).
-Greg
4. Re: Bad Routine Number
- Posted by Icy_Viking Jun 01, 2019
- 1516 views
Ok, I figured out it was a 32-bit DLL I'm using, so that is correct. However, not I'm not getting the -1 error anymore. However nothing shows up when the basic program is executed. I ran it from the command line as well and no errors are coming up. Could it be the way windows draws windows/graphics? I'm using Windows 7 64-bit Ultimate.
atom sdl2 ifdef WIN32 then sdl2 = open_dll("SDL2.dll") elsifdef LINUX or FREEBSD then sdl2 = open_dll("SDL2.so") end ifdef if sdl2 < 1 then --this has been changed from -1 puts(1,"Failed to load SDL2.dll!\n") abort(0) end if
--EuSDL2 Basic Window Example include std/machine.e include EuSDL2.ew include flags.e atom width = 640, height = 480 --Init SDL2 if SDL_Init(SDL_INIT_EVERYTHING) < 1 then puts(1,"Could not init SDL2!\n") abort(0) end if --Window creation atom win = SDL_CreateWindow("Basic Window - Will close after 3 Seconds",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,width,height,SDL_WINDOW_SHOWN) if win < 1 then puts(1,"Failed to create window!\n") abort(0) end if SDL_Delay(3000) -- make the window appear for three seconds --Cleanup SDL_DestroyWindow(win) SDL_Quit()
5. Re: Bad Routine Number
- Posted by ghaberek (admin) Jun 01, 2019
- 1523 views
Ok, I figured out it was a 32-bit DLL I'm using, so that is correct. However, not I'm not getting the -1 error anymore. However nothing shows up when the basic program is executed. I ran it from the command line as well and no errors are coming up. Could it be the way windows draws windows/graphics? I'm using Windows 7 64-bit Ultimate.
It looks like the SDL routines are using the CDECL calling convention. Euphoria will use the STDCALL convention by default on Windows. Have you prepended the function names with '+' to force CDECL?
This is only necessary on 32-bit Windows (64-bit uses its own single convention). The '+' will be safely ignored on 64-bit Windows and non-Windows systems.
-Greg
6. Re: Bad Routine Number
- Posted by Icy_Viking Jun 01, 2019
- 1494 views
Ok, I figured out it was a 32-bit DLL I'm using, so that is correct. However, not I'm not getting the -1 error anymore. However nothing shows up when the basic program is executed. I ran it from the command line as well and no errors are coming up. Could it be the way windows draws windows/graphics? I'm using Windows 7 64-bit Ultimate.
It looks like the SDL routines are using the CDECL calling convention. Euphoria will use the STDCALL convention by default on Windows. Have you prepended the function names with '+' to force CDECL?
This is only necessary on 32-bit Windows (64-bit uses its own single convention). The '+' will be safely ignored on 64-bit Windows and non-Windows systems.
-Greg
Interesting, I didn't have this problem with 4.0.5, also, no I haven't used the "+" in naming convention. Can you give me
7. Re: Bad Routine Number
- Posted by Icy_Viking Jun 01, 2019
- 1532 views
- Last edited Jun 02, 2019
Ok, I figured out it was a 32-bit DLL I'm using, so that is correct. However, not I'm not getting the -1 error anymore. However nothing shows up when the basic program is executed. I ran it from the command line as well and no errors are coming up. Could it be the way windows draws windows/graphics? I'm using Windows 7 64-bit Ultimate.
It looks like the SDL routines are using the CDECL calling convention. Euphoria will use the STDCALL convention by default on Windows. Have you prepended the function names with '+' to force CDECL?
This is only necessary on 32-bit Windows (64-bit uses its own single convention). The '+' will be safely ignored on 64-bit Windows and non-Windows systems.
-Greg
Interesting, I didn't have this problem with 4.0.5, also, no I haven't used the "+" in naming convention. Can you give me a quick example of how it would look like?
EDIT: I figured it out. I was also able to make the window appear. It appears the SDL2 DLL is using the _cdecl format, so you need the '+' in front of the function name. Like so:
public constant xSDL_Init = define_c_func(sdl2,"+SDL_Init",{C_UINT},C_INT) --Notice the '+' right next the function name inside the quotes. public function SDL_Init(atom flags) return c_func(xSDL_Init,{flags}) end function
Now I have to add that "+" for the rest of the 2000+ functions :P.
8. Re: Bad Routine Number
- Posted by Icy_Viking Jun 02, 2019
- 1415 views
Not meaning to double post, but:
I have updated all of my wrappers for Euphoria 4.1.0 Beta 2. Since I'm using the 32-bit version, I had to add "+" inside the function names. The wrappers now work and I have tested them.
9. Re: Bad Routine Number
- Posted by ghaberek (admin) Jun 02, 2019
- 1388 views
I figured it out. I was also able to make the window appear. It appears the SDL2 DLL is using the _cdecl format, so you need the '+' in front of the function name.
Good to hear. This is definitely a problem that's tripped me up before. It's also not always intuitive from the headers you're trying to wrap. Sometimes you have to play follow-the-macro to see where the calling convention gets declared.
Now I have to add that "+" for the rest of the 2000+ functions :P.
If you use Notepad++, you can either do a Find/Replace:
- Find: sdl2,"SDL
- Replace: sdl2,"+SDL
Or use the multi-line selection feature (Shift+Alt+Up/Down) to insert the same character on every line. This does require your define_c_func/proc lines be vertically aligned, however.
-Greg
10. Re: Bad Routine Number
- Posted by Icy_Viking Jun 02, 2019
- 1408 views
I figured it out. I was also able to make the window appear. It appears the SDL2 DLL is using the _cdecl format, so you need the '+' in front of the function name.
Good to hear. This is definitely a problem that's tripped me up before. It's also not always intuitive from the headers you're trying to wrap. Sometimes you have to play follow-the-macro to see where the calling convention gets declared.
Now I have to add that "+" for the rest of the 2000+ functions :P.
If you use Notepad++, you can either do a Find/Replace:
- Find: sdl2,"SDL
- Replace: sdl2,"+SDL
Or use the multi-line selection feature (Shift+Alt+Up/Down) to insert the same character on every line. This does require your define_c_func/proc lines be vertically aligned, however.
-Greg
Thanks for the tip.