linking to dll's [unofficial]

Archives Forums/Win32 Discussion/linking to dll's [unofficial]

skidracer(Posted 2004) [#1]
This needs some work and I'm not sure if it trashes the stack but I think the theory is sound. And no I'm not going to try implementing a com interface so the DirectDraw handle shown here is a pretty useless example but the call does seems to be working.

' testdll.bmx

strict

Import pub.win32

Extern "win32"
Global DirectDrawCreate(guid:Byte Ptr,IDirectDraw Ptr,pUnkOuter)="bb__DirectDrawCreate"
End Extern

Function DllLink(dll$,name$,func Ptr)
	Local lib,proc:Byte Ptr
	lib=LoadLibraryA(dll$)
	proc=GetProcAddress(lib,name)
	func[0]=Int(proc)
End Function

Global _DirectDrawCreate

DllLink("ddraw","DirectDrawCreate",Varptr _DirectDrawCreate)

Function InitDDraw()
	Local res,ddraw
	res=DirectDrawCreate(Byte Ptr(0),Varptr ddraw,0)
	Print "res="+res
	Print "ddraw="+ddraw
End Function

InitDDraw



Kanati(Posted 2004) [#2]
interesting... confusing but interesting.

I'll have to run through this after I fully wake up. :)


Drago(Posted 2004) [#3]
an easier way to do this, if you have minGW is to use 2 little tools that comes with minGW *note* still needs the DLL though.

"pexports"
and
"dlltool"

for example you have a dll called mydll.dll
the first step is to go, (from a command window)
pexports mydll.dll >mydll.def

then
dlltool -d mydll.def -l libmydll.a

then you have a libmydll.a file you can put into the blitzmax lib dir.

and can make a module out of it, but REMEMBER it still requires you to have the dll though.

*edit* better mention how to make a mod
make a new dir in the mod directory
for example dragos.mod
in there you can create another dir called for example mymod.mod MAKE SURE IT IS IN LOWERCASE or it wont build it fully.
inside there you create a file called mymod.bmx
which could look like

Import "-lmydll" ' this tells the linker to link with libmydll.a

extern "win32"
   Function MyFunction:int(myvar:int)
end extern



Bot Builder(Posted 2004) [#4]
Oh, and a little add on to drago's thing - he's been helping me on irc with using a dll, you need to have MingW for compiling modules.

Then, you've gotta set your enviornment paths for it. Go to control panel, System, Advanced tab, click "Enviornment variables", in the new window that pops up there is a listbox labeled "System Variables". Within this, scroll down to a variable named "Path". Select it, click edit. Move to the end of the text, and add a semicolon (;), and paste in the path to your mingw bin directory.

I added ;C:\MinGW\bin to the end of my path variable. Then, create a new dos prompt (not an old... this was one of my probs for a while), change your dir to bmax bin dir, and call:

BMK makemods brl

If for instance you want to recompile blitz research modules.


Drago(Posted 2004) [#5]
heh, yeah, that sort of helps if you want to actually compile and use the module :)

I should have mentioned that.


Hotcakes(Posted 2004) [#6]
Environment paths... pah! -I- use Protean =]


Dreamora(Posted 2004) [#7]
the environment path still needs to be set or your module can't be build! ( Protean isn't god as well, it just calls the BlitzMax compiler which relates to the path )


Bot Builder(Posted 2004) [#8]
Hmm. I'm getting problems with my mod - here's an verview of what i did

Ok, I used drago's procedure to produce a .a file and put it in my lib directory. Then, I made my module folder in mod:

C:\BlitzMaxBeta101\mod\bot.mod

Then I made a folder within that for the mod I'm going to make:

C:\BlitzMaxBeta101\mod\bot.mod\p5.mod

Here's my bmx file:



I save this in my mod directory as:

C:\BlitzMaxBeta101\mod\bot.mod\p5.mod\p5.bmx

And then compile, which works.

Upon attempting to compile a program, however, I get:

Linking:Test.exe
C:\BlitzMaxBeta101\bin\ld: cannot find C:/BlitzMaxBeta101/mod/bot.mod/p5.mod/p5.a
Build Error: Failed to link C:/BlitzMaxBeta101/Projects/P5/Test.exe


This is no bigie, I copy the .a file, and retry:

Linking:Test.exe
C:/BlitzMaxBeta101/Projects/P5/.bmx/Test.bmx.o(code+0x23): undefined reference to `__bb_p5_p5'
Build Error: Failed to link C:/BlitzMaxBeta101/Projects/P5/Test.exe

And this is where I'm stuck now


[edit]Oh, and I have the dll in the directory of the program.


Bot Builder(Posted 2004) [#9]
Riot - yeah, err, i do too. That doesnt mean you dont have to set your mingw enviornment paths.

Oh, and the dll comes with a .def file thats alot different from the one produced by pexports:

.def file that comes with it:


Def file produced:


Not sure which ones right.

Oh, and for reference,

p5.d.i produced by blitzmax


The p5.d.a file has a few references to __bb_p5_p5.


skidracer(Posted 2004) [#10]
You need an import blah.a in your bmx file.


Bot Builder(Posted 2004) [#11]
lol. Well, I've added that. Still doesnt work

Here's a zip of my bot.mod directory:

http://blitzstuff.250free.com/zips/botmod.zip

I took out the compiled files so you'll have to bmk makemods bot or a windows comp.


skidracer(Posted 2004) [#12]
take out the "win32" and you're away!

I also suggest renaming your p5.a file p5lib.a to avoid any unnecesary confusion, although it seems to be working as is.


Hotcakes(Posted 2004) [#13]
Well, don't I look like a silly sausage.


Bot Builder(Posted 2004) [#14]
Ok, well, that didnt work. did you get it working over there?

I noticed I had quotes aroung my inclusion of p5lib.a, I took those out but now i'm getting this from the module compilation:

C:\BLITZM~1\bin>bmk makemods bot
Compiling:p5.bmx
Compile Error:Can't find interface for module 'p5lib.a'
[C:\BLITZM~1\mod\bot.mod\p5.mod/p5.bmx;1;1]
Build Error: failed to compile C:/BLITZM~1/mod/bot.mod/p5.mod/p5.bmx


Maybe I can make a tutorial about my "experience" when I'm done.

[edit] Oh, also, you may notice I'm attempting to do ByRef values with Ptr appended to the ByRef parameters of Extern functions. I just noticed that this is Var in max2d module for internal byref. Which do I use?


Drago(Posted 2004) [#15]
you want to name it
libp5.a
and in the p5.bmx file to include it just have import "-lp5"
which should fix your problem.


AntonyWells(Posted 2004) [#16]
Off topicish,
but drago/SKID(Fix it fix it fix it), tt's a bug in the compiler.

Currently, I have trinity set up as a series of modules within a dsi.mod folder, all compiled automatically using Bmk MakeMods -v DSI - Does so OK.


Now, actually compiling an example app using it,

WITH Debug on : Compiles fine.
With debug off : Linker can not find texture.a or etc.
So bot, try putting the debugger on please and let me know if does the same. Well let mark know, not like I can do anything about it :)


And bmk has a few annoying bugs as well, relating to compiling.(reporting missing references once, then mysteriously compiling ok simply by recompiling.)

(fix it)


BlitzSupport(Posted 2004) [#17]
Antony, have you done "makemods" plus "makemods -r" (for release)?


AntonyWells(Posted 2004) [#18]
Tried it and it works fine after building releases modules. Thanks.


Bot Builder(Posted 2004) [#19]
:O Holy crap. That's what was wrong. I put it in release mode and it worked fine. This is very, very odd BRL.... Ohwell. Least I can compile stuff now. ;)

Now to see if the module actually does what it says on the box...


Bot Builder(Posted 2004) [#20]
Well it sorta works... I can init it and get the number of gloves I have but I'm having problems with Ptr.

Line of the C Header for DLL:
P5DLL_API void __stdcall P5_GetAbsolutePos(int P5Id, float *x, float *y, float *z);

My version in .bmx:
Function P5_GetAbsolutePos(P5Id:Int, x:Float Ptr, y:Float Ptr, z:Float Ptr)


ExampleCode:
If P5_Init()=0 Then RuntimeError("Could not init P5")
Print "You have "+P5_GetCount()+" gloves."

While Not KeyHit(KEY_ESCAPE)
	Local x:Float, y:Float,z:Float
	P5_GetAbsolutePos(0,Varptr x,Varptr y,Varptr z)
	Print x+", "+y+", "+z
Wend


This will print the number of gloves, and then prints out one x,y,z like:

6.86116086e-039, 3.21402937e-039, 0.000000000

One prob with this is that it might not even be my fault, as all the examples use the new way of getting data, which is to recieve a pointer to a structure and then read in data from the structure whenever you want it. I'll try reading raw stuff from the memory of the struct next.


Drago(Posted 2004) [#21]
you can make a type that is a clone of the structure, and pass it. and it should be filled with the info :)


Bot Builder(Posted 2004) [#22]
Ugg. dunno. I think I'm gonna write a wrapper file and see if I can include that, and get bmax to autocompile it for me.

I've blitzified the types, the problem is converting a pointer to a blitz type:

If P5_Init()=0 Then RuntimeError("Could not init P5")

Local State:P5State Ptr
State=P5_GetStatePointer(0)

Print "You have "+P5_GetCount()+" gloves."

While Not KeyHit(KEY_ESCAPE)
	Print State[0].x#+", "+State[0].y#+", "+State[0].z#
	FlushMem
Wend


:( MEES (Memory Exception Error)

I'm gonna try writing some C code to convert all these structs ByRefs, etc into straight function returns.


Bot Builder(Posted 2004) [#23]
Ok, I've figured out I can get a pointer to the structure containing the data, and read raw stuff from it :) It works!


TartanTangerine (was Indiepath)(Posted 2005) [#24]
How did you manage to pass the structure?


LAB[au](Posted 2007) [#25]
Is there an "Official" linking to dll guide one year later??


Trader3564(Posted 2008) [#26]
Maybe now? I seriously need help!