Author Topic: Using a O2 DLL with apps generated with other compilers  (Read 18539 times)

0 Members and 1 Guest are viewing this topic.

jack

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #30 on: January 12, 2011, 12:22:36 PM »
here's working PureBasic version

Code: [Select]
Import "dll_math_o2.lib"
  IsInitialized()
  MathAdd(*n1.i, *n2.i)
  MathDiv(*n1.i, *n2.i)
  MathMul(*n1.i, *n2.i)
  MathSub(*n1.i, *n2.i)
EndImport
x.i = 4
y.i = 2
Debug IsInitialized()
Debug MathMul(@x,@y)
Debug MathDiv(@x,@y)
Debug MathAdd(@x,@y)
Debug MathSub(@x,@y)

have to use variables, can't use constants when calling the functions.
« Last Edit: January 12, 2011, 12:25:49 PM by jack »

Charles Pegge

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #31 on: January 12, 2011, 01:46:21 PM »
Well done jack. And welcome to the Forum :)

FreeBasic and PureBasic make it quite complicated to hook up to a DLL. I am very familiar with FreeBasic which ships with a large number of .A (import library) files for all the commonly used DLLs. This is a requirement of the GNU linker. It's all very arcane.

Creating the .A file from the def file
Code: [Select]
dlltool -k -d Bah.def -l libBah.a

Incidently. If you want to create DLLs in FreeBasic without the @ name decoration, you define all the export subs and functions between:

Code: [Select]
    Extern "Windows-MS"
...
...
   End Extern

Charles

efgee

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #32 on: January 12, 2011, 02:08:17 PM »
here's the FreeBasic Program with run-time loading, it works as expected.

...

here's working PureBasic version


Outstanding job!

 8)

Thank you very much.

jack

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #33 on: January 12, 2011, 02:27:19 PM »
thanks Charles, efgee :)
I remember now that you mention it seeing a topic on the FreeBasic forum about Extern "Windows-MS" but completely forgot about it.
in my first FreeBasic attempt, I was declaring the functions as cdecl and it almost seemed to work, am sure had more functions been called
that eventually the program would have crashed due to stack corruption
but when I tried to use the stdcall method the linker complained that it coud not find IsInitialized@0 and similarly for the rest of the functions
when I added the @size to the functioncs in the def file and rebuilt the lib, it compiled OK, but when run it would give the error
IsInitialized@0 not found in the dll, so I am at a loss for the moment on how to use o2 dll's using an import lib in FB.

efgee

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #34 on: January 12, 2011, 02:40:59 PM »
Another PureBasic approach using PureBasic's prototypes:

Code: [Select]
; using prototypes
#Library = 1

Prototype.i mathFunc(*val1.l, *val2.l)

Define a.i = 4
Define b.i = 2

If OpenLibrary(#Library, "dll_math_o2.dll")
 
  If ExamineLibraryFunctions(#Library)
   
    While NextLibraryFunction()
     
      func.mathFunc = GetFunction(#Library, LibraryFunctionName())
      Debug LibraryFunctionName() + " : $" + Hex(LibraryFunctionAddress())
      Debug func(@a, @b)
     
    Wend
   
  EndIf
 
EndIf

CloseLibrary(#Library)

I like Prototypes (as PureBasic calls them) very much; maybe Charles too...
...and writes a reminder in his OxygenBasic to-do list  ;D

.

Charles Pegge

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #35 on: January 12, 2011, 02:51:40 PM »
Well thanks for leading the topic efgee. Do you prefer prototypes to declarations ?  "Declare" reminds me of form filling and taxes but could also be declaration of human rights :)

Just to clarify: in Oxygen you can specify aliases for both imported and exported functions. If the alias is unspecified then the external name is derived from the function/sub name without any alteration (no case conversion either). Internally, Oxygen is not normally sensitive to the case of function names.

The default calling convention is stdcall in 32 bit MS and will be cdecl in 32 bit Linux. In the 64 bit modes it is totally weird and warped by the contortions of the Pentium architecture, and devised exclusively for the convenience of kernel writers. (I am not enthusiastic as you might guess).

Charles

jack

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #36 on: January 12, 2011, 03:42:14 PM »
using alias to decorate the names

o2 code
Code: [Select]
$ dll
  $ FileName dll_math_o2
  includepath "..\inc\"
  include "RTL32.inc"

function IsInitialized alias "IsInitialized@0"() as long export
function = (1)
end function

function MathAdd alias "MathAdd@8"(N1 as long, N2 as long) as long export
function = (N1 + N2)
end function

function MathDiv alias "MathDiv@8"(N1 as long, N2 as long) as long export
function = (N1 / N2)
end function

function MathMul alias "MathMul@8"(N1 as long, N2 as long) as long export
function = (N1 * N2)
end function

function MathSub alias "MathSub@8"(N1 as long, N2 as long) as long export
function = (N1 - N2)
end function


the following FreeBasic code works as expected

FreeBasic code

Code: [Select]
#inclib "dll_math_o2"

declare function IsInitialized alias "IsInitialized" () as integer
declare function MathAdd alias "MathAdd" (byref as integer, byref as integer) as integer
declare function MathDiv alias "MathDiv" (byref as integer, byref as integer) as integer
declare function MathMul alias "MathMul" (byref as integer, byref as integer) as integer
declare function MathSub alias "MathSub" (byref as integer, byref as integer) as integer

Print IsInitialized()
Print MathMul(4,2)
Print MathDiv(4,2)
Print MathAdd(4,2)
Print MathSub(4,2)

print "press return to end ";
sleep
[edit]  unsing alias to decorate the name is a bad idea, better example by Charles below this post.
« Last Edit: January 12, 2011, 05:09:47 PM by jack »

Charles Pegge

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #37 on: January 12, 2011, 04:01:20 PM »
jack,

I was able to avoid alias name decorations in the O2 DLL. I've put all the bits and pieces into the zip below. It's all very complicated!

Charles

.
« Last Edit: January 12, 2011, 04:05:22 PM by Charles Pegge »

jack

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #38 on: January 12, 2011, 04:44:52 PM »
Charles, I had tried something very similar but it did not work, while your example does work
I used pexports to make the def file
pexports -o dll_math_o2.dll >dll_math_o2.def
what I got was
Quote
LIBRARY dll_math_o2.dll
EXPORTS
IsInitialized           @1
MathAdd                  @2
MathDiv                  @3
MathMul                  @4
MathSub                  @5

I then added the @size
Quote

LIBRARY dll_math_o2.dll
EXPORTS
IsInitialized@0            @1
MathAdd@8                  @2
MathDiv@8                  @3
MathMul@8                  @4
MathSub@8                  @5

recreating the lib with that def file did not work
I also made def file with gendef with the command
gendef dll_math_o2.dll
and got
Quote
;
; Definition file of dll_math_o2.dll
; Automatic generated by gendef
; written by Kai Tietz 2008
;
LIBRARY "dll_math_o2.dll"
EXPORTS
IsInitialized
MathAdd@8
MathDiv@8
MathMul@8
MathSub@8
but did not recreate the lib with that.

[edit] adding the k flag to dlltool as in your example did the trick
« Last Edit: January 12, 2011, 04:56:32 PM by jack »

Charles Pegge

  • Guest
Re: Using a O2 DLL with apps generated with other compilers
« Reply #39 on: January 13, 2011, 12:35:07 PM »
If found a couple of useful links on DEF files as used in Visual C


DEF Files in Visual Studio

2010

http://msdn.microsoft.com/en-us/library/28d6s79h.aspx

2008 forum

http://stackoverflow.com/questions/26098/overloaded-functions-in-c-dll-def-file

Charles