Using CallDLL function

BlitzPlus Forums/BlitzPlus Programming/Using CallDLL function

DrFate(Posted 2012) [#1]
Hi all! Recently i've found function CallDLL(), which can greatly extend compillator's abilities, but... I have some problems with using it. It's not very difficult if function from library has not parameters and it's result can be taken as result of the CallDLL() function like here:

res = CallDLL("Kernel32.dll", "GetVersion")


But what should I do if there are some parameters which library function gets? I know, CallDLL() can use banks: in_bank for translation information into library function and out_bank for back process. But how can I translate parameters into a library function using this banks? For example,
GetUserName(LPTSTR lpBuffer, LPDWORD lpnSize)
function. I'm glad to read your ideas, thanks :)


Kryzon(Posted 2012) [#2]
CallDLL is the old way of using dynamic libraries. You have a much better way which is through userlibs.
It consists of placing a DLL and an accompanying DECLS file in the "userlibs" folder of your blitz installation.

Read more here: http://blitzbasic.com/sdkspecs/sdkspecs/userlibs_specs.txt

For getting that user name you can make a bank of 257 bytes (standard maximum string size, based on UNLEN + 1).

-----------------------------------------------------------

DECLS:
.lib "Advapi32.dll"

GetUserName( lpBuffer*, lpnSize*)

No need to put that DLL in \userlibs. When you're using API, they're part of the OS anyway.

And the Blitz code that uses it could be something like...
Const UNLEN% = 256

Local nameBank = CreateBank(UNLEN+1)

Local sizeBank = CreateBank(4)
PokeInt sizeBank, (UNLEN+1)

GetUserName(nameBank, sizeBank)

Local textName$ = ""
Local tempChar$
For i = 1 To (UNLEN+1)
	tempChar = Chr( PeekByte(nameBank,i) )
	If tempChar = Chr(0) Then ;Null-terminated string.
		Exit
	Else
		textName = textName + tempChar
	EndIf
Next

Print textName
WaitKey()

End
Don't even need to tell you the above is untested and will probably not work properly without some tweaks.