Oxygen Basic

Programming => Problems & Solutions => Topic started by: Emil_halim on April 19, 2013, 12:43:53 AM

Title: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 12:43:53 AM
Hi Charles,

I was declare a function in Ziron in easy way like that
Code: [Select]
imports('MagicPro.dll')
{
      procedure Graphics3D = ('_Graphics3D@20')(Dword Width,Height,depth=32; boolean fullScrn=false,VSync=false);  stdcall;
      function  make_hgeSprite=('_make_hgeSprite@20')(int32 image_id; single texx,texy, w, h):Dword; stdcall;
       procedure DB_LoadMusic=('?DB_LoadMusic@@YGXPADH@Z')(char* szFilename; int32 iID); stdcall;
}


So i tried to convert it like this
Code: [Select]
extern lib "MagicPro.dll"
    !  Graphics3D alias _Graphics3D@20(Dword Width,Height,depth=32, boolean fullScrn=false,VSync=false) 
    !  make_hgeSprite alisa  _make_hgeSprite@20(int32 image_id; single texx,texy, w, h) as Dword
    !  DB_LoadMusic alisa  ?DB_LoadMusic@@YGXPADH@Z


is this correct and if there is a simpler other method please tell me.
Title: Re: DLL function Declaration
Post by: Peter on April 19, 2013, 02:13:28 AM
Hi Emil

Quote
extern lib "MagicPro.dll"
!  Graphics3D alias _Graphics3D@20(Dword Width,Height,depth=32, boolean fullScrn=false,VSync=false) 
!  make_hgeSprite alisa  _make_hgeSprite@20(int32 image_id; single texx,texy, w, h) as Dword
!  DB_LoadMusic alisa  ?DB_LoadMusic@@YGXPADH@Z

Everything is wrong!

extern lib "MagicPro.dll"
  !  Graphics3D         Alias " _Graphics3D@20"   
  !  make_hgeSprite  Alias " _make_hgeSprite@20"
  !  DB_LoadMusic    Alias  " ?DB_LoadMusic@@YGXPADH@Z"
end extern
Title: Re: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 03:54:18 AM
Hi Peter,

thank you very much.

i have collect many different method to import Dll function. need some one to comment it
and demonstrate each case please.
Code: [Select]

=======================================================

! IupOpen             Lib "iup.dll" (sys a,b) as sys  
! IupCreate           Lib "iup.dll" (string cname) as sys
! iupDrawRectangle    Lib "iup.dll" (sys *dc, x1, y1, x2, y2, byte r, g, b, sys style)
! IupMainLoopLevel    Lib "iup.dll" () as sys
! IupClose            Lib "iup.dll" ()

' Example
=========
   IupOpen(0,0)
   win = iupCreate "dialog"
   iupDrawRectangle *canvas, 0,0, 244,238, 254,72,170, 1  
   IupClose()
===========================================================================


Declare Function DragQueryFile Lib "shell32" Alias "DragQueryFileA" ( _
    ByVal wHandle As SYS, _
    ByVal NumFiles As SYS, _
    ByVal NameBuffer As SYS, _
    ByVal BufferLen As Long) As SYS

DECLARE SUB DragAcceptFiles LIB "SHELL32.DLL" ALIAS "DragAcceptFiles" ( _
   BYVAL hWnd AS SYS _                                ' __in HWND hWnd
 , BYVAL fAccept AS SYS _                              ' __in BOOL fAccept
 )                                                      ' void

declare sub trial alias "trial" lib "mdlt"
declare QueryPerformanceCounter   lib "kernel32.dll" (quad *c)  
===========================================================================

gdi32    = LoadLibrary "gdi32.dll"

Bind gdi32
(
  ChoosePixelFormat ChoosePixelFormat
  SetPixelFormat    SetPixelFormat
  GetStockObject    GetStockObject
  SwapBuffers       SwapBuffers
)



============================================================================

lib "gdi32.dll"
Declare Polygon (sys hdc, int *lpPoints, nCount) as bool
Declare ...
lib ""

Library "user32.dll"
! MessageBox alias "MessageBoxA"
Library ""

=============================================================================

extern lib "gdi32.dll"
! CreateFont  Alias "CreateFontA" (long  H,W,E,O,W,I,U,S,C,OP,CP,Q,PAF,string F) As sys
! alias FreeImage_OutputMessageProc
! alias _FreeImage_GetVersion@0() as char*
end extern

extern lib "sqlite3.dll"  
  
  sys   sqlite3_open (char*name,sys*db)  
  sys   sqlite3_exec (sys db,char* s, sys p1, sys p2, sys*dberr)  
  sys   sqlite3_prepare_v2 (sys db, char*s, sys p1, sys*stmt, sys p2)  
  sys   sqlite3_step (sys n)  
  char* sqlite3_column_text (sys row, sys col)  
  sys   sqlite3_close (sys db)  
  '  
end extern  
================================================================================

module o2  
  declare sub  ::message alias "message" lib "sbo2"  
  declare sub  ::compile alias "compile" lib "sbo2"  
  declare sub  ::start   alias "start"   lib "sbo2"  
  declare sub  ::fun     alias "fun"     lib "sbo2"  
end module  

================================================================================


     

Also , what about optional param in first function
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 19, 2013, 06:12:25 AM
Hi Emil,

The final on your list is for ScriptBasic :)

'!' is the same as declare

You can use the word optional for optional parameters. The compiler pushes nulls onto the stack wherever the optional param is not specified in a call.

Any param specified to the right of optional is optional.

! IupOpen             Lib "iup.dll" (optional sys a,b) as sys 


Charles



Title: Re: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 06:23:58 AM
Hi Charles,

for the sake of simplicity.

can you allow C++ style instead of Optional keyword

E.G
  
! IupOpen             Lib "iup.dll" (sys a=0,b) as sys  
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 19, 2013, 07:13:11 AM
Use of the default equate is subtly different, you will still need the word optional

function f(sys optional a=1,b=0)
print  a "   " b
end function

f          ' 1 0
f 48       ' 48 0
f 48,96    ' 48 96
f a=12     ' 12 0
f b=24     ' 1 24
f a=6,b=12 ' 6 12
Title: Re: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 07:16:53 AM

yes but i need it with Dll import function
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 19, 2013, 07:47:35 AM
It should still work with a DLL import

Emulated here:

function g(sys optional a=1,b=0) external
print  a "   " b
end function

declare function f(sys optional a=1,b=0) at g

f          ' 1 0
f 48       ' 48 0
f 48,96    ' 48 96
f a=12     ' 12 0
f b=24     ' 1 24
f a=6,b=12 ' 6 12
Title: Re: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 08:01:49 AM

ok , but i did not like it in this way.

can you support it with any method of declaration tha i listed above.

also con you make  optional keyword optional here so default equate will tell the compile that it is optional.

Quote
Any param specified to the right of optional is optional.

hum... suppose that i want b param is required and keeping the order of parameters , i think default equate
is the good solution here . for Exam
Code: [Select]
! IupOpen  Lib "iup.dll" (sys a=0,b) as sys 
 
Title: Re: DLL function Declaration
Post by: Aurel on April 19, 2013, 10:43:29 AM
Quote
ok , but i did not like it in this way.
heh ..that is the main trick here  ;)
I don't like some things in o2 -> but most of things is great
even better than in some other compilers... :)
Title: Re: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 10:56:05 AM
Quote
I don't like some things in o2 -> but most of things is great
even better than in some other compilers

workaround solution and achieving a certain thing by many ways is the best advantage of OxyGen Basic.   
Title: Re: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 11:52:54 AM

Hi Charles,

deos the latest update applies to import function?
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 19, 2013, 12:58:51 PM
Sure, here is another function vector declaration

function g(sys a,b) external
print  a "   " b
end function

! f(sys a=1,b) at g

f            ' 1 0
f 48         ' 48 0
f 48,96      ' 48 96
f a=12       ' 12 0
Title: Re: DLL function Declaration
Post by: Emil_halim on April 19, 2013, 01:07:30 PM

ok Charles ,

but i want it just like that
Code: [Select]
! f(sys a=1,b)

because i have many imported functions those have default equate , so for each function i have to declare this
Code: [Select]
function g(sys a,b) external
print  a "   " b
end function

hope you get me well.

thanks you.
 
Title: Re: DLL function Declaration
Post by: Aurel on April 19, 2013, 01:23:14 PM
hmm...
in which another language you can define such a construction  ???
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 19, 2013, 05:15:00 PM
You can use extern blocks for all the imported functions. Then no need to repeat lib, calling-convention, external.

extern lib "user32.dll"
! Sleep(int ms)
 ...
extern lib cdecl "mylib.dll"
! f(sys a=1,b)
...
end extern

Title: Re: DLL function Declaration
Post by: Emil_halim on April 20, 2013, 07:05:51 AM
Quote
Then no need to repeat lib, calling-convention, external.

is that mean there is no need to declare the following dummy function to allow optional parameters of
my imported dll functions when using extern blocks.
Code: [Select]
function g(sys a,b) external
print  a "   " b
end function
Title: Re: DLL function Declaration
Post by: Emil_halim on April 21, 2013, 07:38:44 AM
Charles ,

con you confirmed that please.
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 21, 2013, 01:41:58 PM
Yes Emil, it will give you optional param with the syntax used above. However during the course of my checks, I found some anomalies, with mixed optional and default value calls, which I will need to fix. But for your optional params, it should be okay.

extern lib "tdll.dll"
! ff1(sys a=0,b,c,d) as sys1
end extern

ff1 2,3

------->

includepath "$/inc/"
$filename "tdll.dll"
$dll
include "RTL32.inc"

extern export
function ff1(sys a,b,c,d) as sys
string cm=","
print a cm b cm c cm d
end function


Title: Re: DLL function Declaration
Post by: Emil_halim on April 22, 2013, 07:58:18 AM
Hi Charles,

i have an imported function declare as following
Quote
! CreateCamera alias "_CreateCamera@4" (sys EntityParent=0) as sys
inside extern block.

and this is a simple program that using the above declaration
Code: [Select]
//****************************************//
//           ************                 //
//      ***MagicPro library***            //
//           ************                 //
//****************************************//

includepath "$/inc/"
$ filename "Scaling.exe"
#include "RTL32.inc"

#include 'MagicPro.inc';

sys cam

    Init("MagicPro Test #1")
    Graphics3D(800,600)

    setMediaDir("ZipMedia.zip")
    setMediaDir("media")
    
    setAmbientLight(30.0,30.0,50.0)

    cam = CreateCamera
  
    while(!OIS_isKeyDown(KC_ESCAPE))
     {
        RenderWorld()  

     Sync()
     }
    
    
    EndGraphics()

when compiling got that funny errer
Quote
Unidentified operand
WORD: createcamera
Line: 23

so what is that.
Title: Re: DLL function Declaration
Post by: Emil_halim on April 22, 2013, 08:16:48 AM
ok i mead this change
Code: [Select]
//includepath "$/inc/"
$ filename "MagicProTest1.exe"
#include "..\..\inc\RTL32.inc"

#include "MagicPro.inc"

it give an other error
Quote
Linker found unidentified name:
!: level 0
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 22, 2013, 08:18:08 AM
Spotted 2 errors

Need to replace single quotes with double quotes?

#include 'MagicPro.inc';

also:

while(!OIS_isKeyDown(KC_ESCAPE))

while not OIS_isKeyDown(KC_ESCAPE)
Title: Re: DLL function Declaration
Post by: Emil_halim on April 22, 2013, 08:25:38 AM
thanks Charles ,

works now .

but i have an idea about includepath , noticed that when using it , any file in the o2bas folder did not seen
by Oxygen , SO you can use array of include paths and searching will be one after one.
Title: Re: DLL function Declaration
Post by: Peter on April 22, 2013, 08:34:08 AM
Quote
while not OIS_isKeyDown(KC_ESCAPE)

Or: 
while  OIS_isKeyDown(KC_ESCAPE) =0

Title: Re: DLL function Declaration
Post by: Peter on April 22, 2013, 08:37:11 AM
Emil,

MagicPro ?

What is it for?
Title: Re: DLL function Declaration
Post by: Emil_halim on April 22, 2013, 08:43:53 AM
Hi Peter,

see my last post in this page   http://www.ogre3d.org/forums/viewtopic.php?f=11&t=25924&start=375 (http://www.ogre3d.org/forums/viewtopic.php?f=11&t=25924&start=375) 
Title: Re: DLL function Declaration
Post by: Emil_halim on April 22, 2013, 09:19:37 AM
Charles,

I have this declaration
Quote
! LoadMesh alias "_LoadMesh@12"(char* file, sys EntityParent=0, char* meshName="") as sys


SO when calling it like this the program crash
Code: [Select]
ogrehead = LoadMesh("ogrehead.mesh")

but this works
Code: [Select]
ogrehead = LoadMesh("ogrehead.mesh",0,"")

any help , how to allow empty string "" to be as a default equat?
Title: Re: DLL function Declaration
Post by: Aurel on April 22, 2013, 12:58:31 PM
maybe :
ogrehead = LoadMesh("ogrehead.mesh",0,NULL)

by the way  i read somewhere that char cannot have 0 length so probably must
be string with zero length.
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 22, 2013, 01:43:09 PM
Hi Emil, Altering the compiler for this one, creates logistical nightmares so the best solution is:


LoadMesh alias "_LoadMesh@12"(char* file="", sys EntityParent=0, char* meshName="") as sys
...
ogrehead = LoadMesh(file="ogrehead.mesh")



PS:

Another way is to check for null strings within the procedure you are calling (if this is your own library):

if not strptr MeshName then ...
Title: Re: DLL function Declaration
Post by: Emil_halim on April 22, 2013, 10:02:49 PM
thanks Aurel,

thanks Charles ,

ok i got it.
Title: Re: DLL function Declaration
Post by: Emil_halim on April 24, 2013, 08:22:34 AM
Chales ,

how to declare an import function as a c function "cdecl" ?
Title: Re: DLL function Declaration
Post by: Charles Pegge on April 24, 2013, 10:57:06 PM
cdecl can go into the xtern statement, if it applies to several functions, or it can be applied to individual functions:

extern lib "mylib.dll" cdecl
! myproc alias "MyProc" (..)
end extern

extern lib "mylib.dll"
! myproc cdecl alias "MyProc" (..)
end extern

Title: Re: DLL function Declaration
Post by: Emil_halim on April 25, 2013, 07:19:52 AM

yes that will help me a lot, thanks Charles.