GLBasic forum

Main forum => GLBasic - en => Topic started by: Hemlos on 2009-Oct-22

Title: Creating a DLL, question
Post by: Hemlos on 2009-Oct-22
I know i cant use glbasic internals in a DLL file..
I cant seem to get any functions to do anything at all...everything gets ignored, except for the call to the function inside itself.

So my question is: What exactly can we put in a dll??

Title: Re: Creating a DLL, question
Post by: MrTAToad on 2009-Oct-22
You should be able to put anything you like in a DLL - I'll see what I can do...
Title: Re: Creating a DLL, question
Post by: Kitty Hello on 2009-Oct-22
mostly math functions. If you are using grapics, the DLL will open a complete new window, I'm uncertain, but I think you must call SETSCREEN before doing so.
Title: Re: Creating a DLL, question
Post by: MrTAToad on 2009-Oct-22
I'm trying to call SETSCREEN in a DLL. but the DLL crashes.

The main code is :

Code (glbasic) Select
test()

FUNCTION p%:
ENDFUNCTION

INLINE
DECLARE_C(start, "test2.app/test2.dll", (void), void);
ENDINLINE

FUNCTION test%:
INLINE
if (start==NULL)
{
DEBUG("Here");
}
else
{
start();
}
ENDINLINE
ENDFUNCTION


And the DLL is :

Code (glbasic) Select
FUNCTION start%:
SETSCREEN 1024,768,1
ENDFUNCTION

EXPORT start


It does look like non SDL stuff only can be in DLL's
Title: Re: Creating a DLL, question
Post by: Kitty Hello on 2009-Oct-22
When you create a DLL, there will be a wrapper gbas created. Use that instead of INLINE.
It might be that it's buggy - noone ever used it AFAIK. That's what you get for implementing features just because people ask for it...
Title: Re: Creating a DLL, question
Post by: MrTAToad on 2009-Oct-22
The wrapper (finally found it), does appear to be invalid :

Code (glbasic) Select
// Wrapper DLL
INLINE
}// namespace
#define DLL_NAME "tEST2.dll"
DECLARE_C_ALIAS(dll_start, DLL_NAME, "start", (), DGNat);
namespace __GLBASIC__ {
ENDINLINE


//!
//?
// \return     |

INLINE
return dll_start();
ENDINLINE
ENDFUNCTION


I think we can all spot the deliberate mistake :)

It still crashes in the DLL too...

Using SDL in a DLL does appear to be a common problem : http://lists.libsdl.org/pipermail/sdl-libsdl.org/2005-January/048252.html (http://lists.libsdl.org/pipermail/sdl-libsdl.org/2005-January/048252.html)
Title: Re: Creating a DLL, question
Post by: Hemlos on 2009-Oct-22
QuoteIt might be that it's buggy - noone ever used it AFAIK. That's what you get for implementing features just because people ask for it...

Hehe, ya theres a wrapper bug, but i fixed that, it was obvious the syntax was off...sorry i didnt report this....

But thats not the end of the bugs....i think theres more because i surely did get the function to call properly, and with no compiler issues.
The next thing i noticed, variables...dont stick, i tried GLOBAL A=1 etc..

Oh ya, i found another issue......not dll related...if i declare GLOBAL in a function, it is not byref throughout the program.

Title: Re: Creating a DLL, question
Post by: MrTAToad on 2009-Oct-22
I dont think you can export global variables (if gives a warning if you try).  Returning the values from a function is fine.
Title: Re: Creating a DLL, question
Post by: Hemlos on 2009-Oct-22
Mrtoad,
Hmm i had no such luck.
Do you have a working sample of passing numbers around into and out of a dll?
Title: Re: Creating a DLL, question
Post by: MrTAToad on 2009-Oct-22
Try this :

DLL wrapper :
Code (glbasic) Select
// Wrapper DLL
INLINE
}// namespace
#define DLL_NAME "tEST2.dll"
DECLARE_C_ALIAS(dll_start, DLL_NAME, "start", (), DGNat);
DECLARE_C_ALIAS(dll_start2, DLL_NAME, "start2", (), DGNat);
namespace __GLBASIC__ {
ENDINLINE


FUNCTION test1%:
INLINE
return dll_start();
ENDINLINE
ENDFUNCTION

FUNCTION test2%:
INLINE
return dll_start2();
ENDINLINE
ENDFUNCTION


Test code :

Code (glbasic) Select
DEBUG test1()+"\n"
DEBUG test2()+"\n"


DLL :

Code (glbasic) Select
GLOBAL A%=123

FUNCTION start%:
RETURN 43
ENDFUNCTION

FUNCTION start2%:
RETURN A%
ENDFUNCTION

EXPORT start
EXPORT start2
Title: Re: Creating a DLL, question
Post by: Hemlos on 2009-Oct-22
Strange, i cant figgure out why i get this error.

linking:
gpc_temp1.o:gpc_temp1.cpp:(.text+0x72b0): multiple definition of `_start'
gpc_temp0.o:gpc_temp0.cpp:(.text+0x1680): first defined here
gpc_temp1.o:gpc_temp1.cpp:(.text+0x72c0): multiple definition of `_start2'
gpc_temp0.o:gpc_temp0.cpp:(.text+0x1690): first defined here
*** FATAL ERROR - Please post this output in the forum
Title: Re: Creating a DLL, question
Post by: Schranz0r on 2009-Oct-22
HEMLOS:

Quote from: MrTAToad on 2009-Oct-22
Try this :

DLL wrapper : --->>> (Project MakeDLL)
Code (glbasic) Select
// Wrapper DLL
INLINE
}// namespace
   #define DLL_NAME "tEST2.dll"
   DECLARE_C_ALIAS(dll_start, DLL_NAME, "start", (), DGNat);
   DECLARE_C_ALIAS(dll_start2, DLL_NAME, "start2", (), DGNat);
namespace __GLBASIC__ {
ENDINLINE


FUNCTION test1%:
   INLINE
      return dll_start();
   ENDINLINE
ENDFUNCTION

FUNCTION test2%:
   INLINE
      return dll_start2();
   ENDINLINE
ENDFUNCTION



DLL : ------->>>( Project DLLCall)

Code (glbasic) Select
GLOBAL A%=123

FUNCTION start%:
   RETURN 43
ENDFUNCTION

FUNCTION start2%:
   RETURN A%
ENDFUNCTION

EXPORT start
EXPORT start2


You need 2 different projects, one to create the DLL and one to load it... :)

see this:

multiple definition of `_start'
Title: Re: Creating a DLL, question
Post by: MrTAToad on 2009-Oct-23
Indeed - two separate files :)
Title: Re: Creating a DLL, question
Post by: Kuron on 2009-Oct-23
QuoteIt might be that it's buggy - noone ever used it AFAIK.

I haven't used it, but I would like to.  However, I have never seen any documentation on how to properly create a DLL with GLBasic that can be used with other languages.
Title: Re: Creating a DLL, question
Post by: Kitty Hello on 2009-Oct-23
You must put the dll in the same directory as the .exe
Here's a working exmple.

[attachment deleted by admin]