DLL's - HELP ME

Blitz3D Forums/Blitz3D Beginners Area/DLL's - HELP ME

Dr. Wildrick(Posted 2005) [#1]
I am very confised, while I have seen a number of examples of code that call windows API function and custom DLL's as well, I cant get any of my own code to work.
Can someone- please - explain to me how you set up a DLL call, pass parameters to it and get them back. Please keet the example SIMPLE - Im a newbie to this language.
Thanks!


fraggle(Posted 2005) [#2]
1.: You should declare your function in your dll with declspec like this:
__declspec (dllexport) int initjava(LPCTSTR jvmpath, LPCTSTR classpath, LPCTSTR classname);
2.: You have to write a .decls file:
blitzjava.decls:
.lib "blitzjava.dll"
initjava%(jvmpath$, classpath$, classname$)

place this file in your userlibs directory (default c:\Program Files\Blitz3d\userlibs)

3. restart Blitz3d editor and call your function with:
result%=initjava("path", "classpath","classname")


As you see, you declare function prototypes in the decls file with " functionname[returntype%/$/#](params[%/$/#])"
If you want to return a string or other sort of array/buffer you should use blitz-buffers.
E.g.:
.c:
__declspec (dllexport) void convertName(char*buffer,int size);

.decls:
convertName(bank,bankSize)

Blitz:
bankSize=128
bank=CreateBank(bankSize)
Poke...(bank)
convertName(bank,bankSize)
Peek...(bank)

P.S. not doing this a long time. if your computer explodes, it's not my fault. I warned you *g*


BlackJumper(Posted 2005) [#3]
You should also be aware that some compilers may 'mangle' your function name (e.g. truncating it to 8 characters, etc.) so you need to know what function name was actually exported in the compiled dll, rather than just assume that it remains what you named it in your code.

Many compilers will have 'switches' that may allow you to switch off name-mangling. Others have it off by default.


Lane(Posted 2005) [#4]
What kind of Doctor are you?


Dr. Wildrick(Posted 2005) [#5]
My PhD is in imaging sciences.

So let me see if I have this straint then? I put my DLL in the userlibs folder, create a function declaration in the same name containg the function call(s) starting the line ,lib "mydll.dll" and then follow that with the declarations of the functions in the DLL.
am I on track so far?
So then in my main Blitz program, how do I tell it what I want to call or will it just do the function if I call it and it finds it is a Decls file?
So for example if I tried to call the windows API of Setprocesspriority(processid#,value#)
and it should do it finne as long as I declared the Decls file with the .lib the DLL name in quotes and then a function declaration in it.
That right?
Its important - very omportant that I get this working. Does anyone have anouther simple example - I aleady have the DLL's that I need.
Please give me an exampe of how I would set this up:
My DLL name is say "convolve.dll"
and my function is filter$(a#,B#) where to long int's are expected as params and a string is returned. How do I set this sample up. I'm not sure I follow the need to create banks and peek or poke to them. Is the no "Pass by value"????
So the above example is just a made up one but if I have a refrence starting point, I can figure out why what is being done needs to be. Please tell me how, if I just get code thrown at me its the same as lookig at other programs. I need the "WHY" as much as the HOW"
Any suggestions??? Please????
I appreciate your example Fraggle, Thanks, Could you toss in some more whys in there?
Thanks in advance all.
-T


BlackJumper(Posted 2005) [#6]
Have a look at the specs and utils section.

decls should be...

.lib "convolve.dll"
filter$(a%,b%)


the "convolve.dll" file should then reside in the same folder as the .bb file you are calling it from. {It can live elsewhere, but it is safest to avoid path problems if your code can find it locally. }

Note the "two long int parameters" are specified using the '%' tag... If you use the '#' tag Blitz will expect floats... although I would guess that automatic conversion may occur.
All integer values in your program must be in the range -2147483648 to +2147483647

...can't remember offhand if this equates to a long int!


fraggle(Posted 2005) [#7]
To clarify the use of banks: you create a string in your dll dynamically with malloc or in the data/code segment of your dll with char buffer[255]. This allocation can not be freed. Thats why you call windows functions like GetUserName (example in Protean, code archives,userlibs) with a pointer to your buffer and the buffer size. The function then fills the buffer and it is your responsibility to free the buffer then. If you return strings in your dll you will have a memory leak, if you do not reuse your buffer. With memory banks, you give the responsibility to the executing program.
If blitz can't find the function, I use the program "nm" from the cygwin tools (www.cygwin.com). Then you are able to use even a c++ dll.
Another problem you could get: the dll has to be in the same directory as your workingdir or in windows\system32 folder. You could get problems, starting your application from an ide. If, e.g. you create your executable from Protean and let Protean start your exe after creation, the workdir is set to protean home dir. That means, you can't load your resources with a relative path and neither a dll.


Dr. Wildrick(Posted 2005) [#8]
Thanks Blackjumper and Fraggle. I think I got it now.
Very much apreciated.
Oh, and blackjumper... I find a few good grumbles a day to be quite refreshing. lol
I run a film studio so you can imiagine the characters I have to deal with on a daily basis. When I see contracts with clauses like "And the party of the second part shall, at all times, be within 10 feet of a half full 1 LB bag of M&M plain candies while on any hot set." I just hit my head and say "Oy!"
So grumble away my friend, im right there with ya.
lol
thanks all for the help, you all have been invaluable.
But, im out of M&M's. (Sorry)
Peace
-T

PS - Hey Fraggle - it that a refrence to Fraggle Rock? or a refrence to the Techno/ neo-punk band?