how do I use my DLL in BMax?

BlitzMax Forums/BlitzMax Programming/how do I use my DLL in BMax?

Gillissie(Posted 2008) [#1]
Ok, I have searched the forums, and the best I could come up with still doesn't work. I know the DLL works because it works in my Blitz3D program. What am I doing wrong in BlitzMax? (I'm new to it)

This works:
; Blitz3D

Local sErr$

Local sConStr$ = "provider=Microsoft.Jet.OLEDB.4.0;data source=Data.mdb;Jet OLEDB:Database Password=;"

sErr = GGDB_Connect(sConStr, "", "")
If sErr <> "" Then RuntimeError(sErr)

sErr = GGDB_RSOpen("SELECT Animation_ID,Animation FROM Animation ORDER BY Animation")
If sErr <> "" Then RuntimeError(sErr)

While Not GGDB_RSEOF()
	Print GGDB_RSValue("Animation_ID") + ", " + GGDB_RSValue("Animation")
	GGDB_RSMoveNext()
Wend

GGDB_Disconnect()

WaitKey
End


This doesn't work:
' BlitzMax

Global dll% = LoadLibraryA("GG_DB.dll")

Global GGDB_Connect$(ConnStr$, UserID$, Password$)
Global GGDB_Disconnect$()
Global GGDB_Exec$(SQL$)
Global GGDB_RSOpen$(SQL$)
Global GGDB_RSClose$()
Global GGDB_RSValue$(DBField$)
Global GGDB_RSEOF%()
Global GGDB_RSBOF%()
Global GGDB_RSMoveNext%()
Global GGDB_RSMovePrevious%()
Global GGDB_RSMoveFirst%()
Global GGDB_RSMoveLast%()
Global GGDB_RSRecordCount%()

GGDB_Connect = GetProcAddress(dll, "_GGDB_Connect@12")
GGDB_Disconnect = GetProcAddress(dll, "_GGDB_Disconnect@0")
GGDB_Exec = GetProcAddress(dll, "_GGDB_Exec@4")
GGDB_RSOpen = GetProcAddress(dll, "_GGDB_RSOpen@4")
GGDB_RSClose = GetProcAddress(dll, "_GGDB_RSClose@0")
GGDB_RSValue = GetProcAddress(dll, "_GGDB_RSValue@4")
GGDB_RSEOF = GetProcAddress(dll, "_GGDB_RSEOF@0")
GGDB_RSBOF = GetProcAddress(dll, "_GGDB_RSBOF@0")
GGDB_RSMoveNext = GetProcAddress(dll, "_GGDB_RSMoveNext@0")
GGDB_RSMovePrevious = GetProcAddress(dll, "_GGDB_RSMovePrevious@0")
GGDB_RSMoveFirst = GetProcAddress(dll, "_GGDB_RSMoveFirst@0")
GGDB_RSMoveLast = GetProcAddress(dll, "_GGDB_RSMoveLast@0")
GGDB_RSRecordCount = GetProcAddress(dll, "_GGDB_RSRecordCount@0")


Local sErr$

Local sConStr$ = "provider=Microsoft.Jet.OLEDB.4.0;data source=Data.mdb;Jet OLEDB:Database Password=;"

sErr = GGDB_Connect(sConStr, "", "")
If sErr <> "" Then RuntimeError(sErr)

sErr = GGDB_RSOpen("SELECT Animation_ID,Animation FROM Animation ORDER BY Animation")
If sErr <> "" Then RuntimeError(sErr)

While Not GGDB_RSEOF()
	Print GGDB_RSValue("Animation_ID") + ", " + GGDB_RSValue("Animation")
	GGDB_RSMoveNext()
Wend

GGDB_Disconnect()

WaitKey
End



Here is the contents of the decls file for Blitz3D:
.lib "GG_DB.dll"

GGDB_Connect$(ConnStr$,UserID$,Password$):"_GGDB_Connect@12"
GGDB_Disconnect$():"_GGDB_Disconnect@0"
GGDB_Exec$(SQL$):"_GGDB_Exec@4"
GGDB_RSOpen$(SQL$):"_GGDB_RSOpen@4"
GGDB_RSClose$():"_GGDB_RSClose@0"
GGDB_RSValue$(Field$):"_GGDB_RSValue@4"
GGDB_RSEOF%():"_GGDB_RSEOF@0"
GGDB_RSBOF%():"_GGDB_RSBOF@0"
GGDB_RSMoveNext%():"_GGDB_RSMoveNext@0"
GGDB_RSMovePrevious%():"_GGDB_RSMovePrevious@0"
GGDB_RSMoveFirst%():"_GGDB_RSMoveFirst@0"
GGDB_RSMoveLast%():"_GGDB_RSMoveLast@0"
GGDB_RSRecordCount%():"_GGDB_RSRecordCount@0"



You can download all of the files and try it yourself if you want:
http://www.gilligames.com/DB_Test.rar
(27 kb download)


plash(Posted 2008) [#2]
What are the errors you're getting?

The strings may be W/C Strings..


Gillissie(Posted 2008) [#3]
The BMax program just flat out crashes, Windows style. The crash error window says "Microsoft Visual C++ Runtime Library" at the top, then "Runtime Error!" and "abnormal program termination".

I should mention that the dll was written in C++ in a slightly older version of Visual Studio. I don't remember which version specifically.


Gillissie(Posted 2008) [#4]
Nobody knows how to do this?


plash(Posted 2008) [#5]
Do you have the MS VCC runtime library in your program directory?


Brucey(Posted 2008) [#6]
I don't usually runtime-load DLLs, preferring to plug them in at link-time, so my way requires a .def and a .a for MinGW. (both of which can be generated from the dll).
So I can't be of much help in this case.

However, I *can* access your mdb file via ODBC in BlitzMax using my own module - not that that helps you here of course....


Gillissie(Posted 2008) [#7]
@Plash: No. I don't even know what files that would be, and I'm pretty sure it's installed in Windows and isn't required in a program directory.

@Brucy: I don't know what any of that means. All I want to know is how to get the same functionality from a dll that I used to get in Blitz3D. I don't see any documentation on it, and only partial descriptions in the forums. I created the dll myself because I couldn't find one that fit my needs before, but if you have a module that I could use too, then it would solve my issue (for now). Does BlitzMax support ODBC natively? How did you write your module?


plash(Posted 2008) [#8]
This may not be relevant to the issue, but you should definitely free the library once you're done with it.

No. I don't even know what files that would be, and I'm pretty sure it's installed in Windows and isn't required in a program directory.
I would assume it to be one of the C++ redistributables. What programming language and what compiler was used to make the dll?


Gillissie(Posted 2008) [#9]
Like I mentioned above, the dll was written in C++ in a slightly older version of Visual Studio. I don't remember which version specifically.

But that shouldn't be the issue, since it works perfectly from Blitz3D. That's what I don't understand. It must be something about how to do it with BlitzMax.


plash(Posted 2008) [#10]
Do you still have the working version in Blitz3D? Check for dlls in or near the source/binary directory.

The only thing I can think of is not having one of the redistributable's..
It might be worth a try to download a bunch of msvc redistributable's, starting from like msvc60 and going up (a Google search should get you what you're looking for).


GaryV(Posted 2008) [#11]
The only thing I can think of is not having one of the redistributable's..
He makes a point. Keep in mind that VC++ does not always create standalone EXEs/DLLs, they often depend on the VC++ runtimes.


Brucey(Posted 2008) [#12]
That's not the issue.
It only depends on kernel32.dll, ole32.dll and oleaut32.dll - according to Dependency Walker.

I'm guessing it's just a wrapper for accessing ODBC, given the value of sConStr, which is a typical DSN-type string.

As for why it breaks when Connect is called, I can't help I'm afraid.

Does BlitzMax support ODBC natively?

No. But through my BaH.Database and BaH.DBODBC modules, it can. There's an Access test app included with the odbc module.
The downloads page is here : http://code.google.com/p/maxmods/downloads/list
But you'll need to be setup with MinGW to build it, etc...


Gillissie(Posted 2008) [#13]
Brucey,
Thanks, I'll check it out. Do you have any docs for your module?


Gillissie(Posted 2008) [#14]
Brucey,
I created a folder named BaH.mod, then put the dbodbc.mod folder inside that. Then I called:
bmk makemods BaH.dbodbc

but there was no response by the compiler so I don't know if it did anything. I loaded up the access_test.bmx program and tried to run it, but I get a compile error that it can't find the interface for module "bah.dbodbc". I have a feeling it didn't compile, but I don't know why.

I do have mingw installed, and was able to compile a small test module of my own.


plash(Posted 2008) [#15]
A simple Build Modules should do what you need.


Gabriel(Posted 2008) [#16]
Compiling a module is a good way to test things out, but unless the module has any C or C++ code in it, it won't require MinGW, so it doesn't necessarily mean that is correctly installed.

Open a command prompt and Type :

gcc

If MinGW is installed correctly you should see a message something like:

gcc: no input files.


Gillissie(Posted 2008) [#17]
It's correctly installed. I did what you suggested and it returned what you said.


Brucey(Posted 2008) [#18]
Make sure all the folder names are lowercase to begin with.
You will also need the bah.database module, which is the base for the specific database drivers.

With both installed, things would look like this :
...BlitzMax/mod/bah.mod/database.mod
...BlitzMax/mod/bah.mod/dbodbc.mod

, and in each of those folders are all the files, like for example, dodbc.bmx.

On the commandline you can also do this :
bmk makemods -a bah

which will have it re-build all modules under the bah.mod folder.

If you run that command and nothing appears to happen, then either :
1) MinGW isn't properly installed.
2) You have a path wrong.

HTH


skidracer(Posted 2008) [#19]
in regards to original question I think your function pointers need the "C" calling convention for blitz3d style and string suffix needs to be changed such as:

Global GGDB_Connect$z(ConnStr$z, UserID$z, Password$z) "C"


Gillissie(Posted 2008) [#20]
Thanks Brucey for the additional tips. I did not download the database module yet, so that's part of the problem. Still, do you have docs on usage?

Skidracer, thanks for that tip. I'll also try that with my own DLL, which I'd prefer to use since my Blitz3D code is already using it and that's what I'm converting.