How can a DLL function return a "String value" to the calling program ?

33 views
Skip to first unread message

Skan

unread,
Apr 11, 2008, 5:48:06 PM4/11/08
to xblite
Dear friends,

It has been only a few hours since I discovered xblite. I have been
browsing through the fantastic examples bundled with this wonderful
program.

While there are many examples for w32 console, I could find only one
example ( api.x ) that demonstrates creation of DLL. Can somebody
point me how can a DLL function can return a "String value" to the
calling program ?


FUNCTION IntToBin ( num )
EXTERNAL STRING nu$
nu$ = BIN$(num)
RETURN nu$
END FUNCTION

Please help.

Best Regards, :)

D.

unread,
Apr 11, 2008, 8:01:28 PM4/11/08
to xbl...@googlegroups.com
> Can somebody point me how can a DLL function can return
> a "String value" to the calling program ?
>
>
> FUNCTION IntToBin ( num )
> EXTERNAL STRING nu$ ' <<< you don't need this

> nu$ = BIN$(num)
> RETURN nu$
> END FUNCTION

Try defining the function with $ suffix, eg, :

FUNCTION IntToBin$ (num)

or, define the function return type as STRING, eg :

FUNCTION STRING IntToBin (num)

Don't forget to change the function declaration in the
PROLOG as well.

You don't need the line EXTERNAL STRING nu$.

ciao,
D.

Skan

unread,
Apr 12, 2008, 3:12:59 AM4/12/08
to xblite
Many thanks D., :D

Skan

unread,
Apr 12, 2008, 5:44:48 AM4/12/08
to xblite
Dear D.

I have one more problem.. The following is the whole program:

PROGRAM "skan"
VERSION "0.0090"

EXPORT
DECLARE FUNCTION STRING toHex ( num )
DECLARE FUNCTION STRING AllTrim ( istr$ )
END EXPORT

FUNCTION STRING toHex ( num )
nu$ = HEX$( num )
RETURN nu$
END FUNCTION

FUNCTION STRING AllTrim ( istr$ )
nist$ = TRIM$( istr$ )
RETURN nist$
END FUNCTION
END PROGRAM

I guess I am missing something very simple. toHex() works fine but
AllTrim() results in "Access Violation" from the calling program.

Please help.

Best Regards. :)

D.

unread,
Apr 12, 2008, 8:21:17 AM4/12/08
to xbl...@googlegroups.com

I built your library. Both functions worked as expected.

How are you calling AllTrim? Can you provide your test code?

Also, you can shorten the above functions by using

RETURN HEX$ (num)

or

RETURN TRIM$ (istr$)

ciao,
D.

Skan

unread,
Apr 12, 2008, 9:37:16 AM4/12/08
to xblite
Dear D.

I am testing my DLL with AutoHotkey Scripting Language and the
following is the script:

Str0 := DllCall( "user32.dll\CharUpperA", Str,"skan", Str )
MsgBox, % Str0 "`nerrorlevel: " errorLevel

DllCall( "LoadLibrary", Str,"C:\xblite\Skan\skan.dll" )

Str1 := DllCall( "skan.dll\ToHex", UInt,1024, Str )
MsgBox, % Str1 "`nerrorlevel: " errorLevel

Str2 := Dllcall( "skan.dll\AllTrim", Str," sdsads ", Str )
MsgBox, % Str2 "`nerrorlevel: " errorLevel

If you like to test the above script:
No need to install AutoHotkey. Save the above script to a text file
( with .AHK extension ) and pass it as a parameter to AutoHotkey.exe
( which can be extracted from http://www.autohotkey.com/download/AutoHotkey.zip
).
AutoHotkey is an Interpreter and works similar to WSH

The call to User32.dll - CharUpperA() is to demonstrate that I am
using the parameters right. I am passing the string by value and
recieve the return value as String

Please help.

> Also, you can shorten the above functions by using
>
> RETURN HEX$ (num)
>
> or
>
> RETURN TRIM$ (istr$)

Thanks!

Best Regards.

Skan

unread,
Apr 12, 2008, 10:31:25 AM4/12/08
to xblite
.. and one more thing

the following works and return "Skan"

FUNCTION STRING AllTrim ( istr$ )
RETURN TRIM$ ( " Skan " )
END FUNCTION


.. but this returns only null

FUNCTION STRING AllTrim ( istr$ )
RETURN TRIM$ ( istr$ )
END FUNCTION

D.

unread,
Apr 12, 2008, 11:02:21 AM4/12/08
to xbl...@googlegroups.com
Ok, your use of autohotkey explains the problems you
are seeing.

Two important things:

1. Autohotkey expects to pass all string parameters by address.

So we need to modify the xblite function to accept a string address:

DECLARE FUNCTION STRING AllTrim ( saddr )

FUNCTION STRING AllTrim (saddr)
nist$ = CSTRING$ (saddr) ' convert address into string
RETURN TRIM$ (nist$) ' return nist$
END FUNCTION

2. When calling an external function from autohotkey which expects
a string returned, you need to allocate enough space for the returned
string by calling VarSetCapacity:

VarSetCapacity(Str2, 6)


Str2 := Dllcall( "skan.dll\AllTrim", Str, " sdsads ", Str)
MsgBox, % Str2 "`nerrorlevel: " errorLevel

ciao,
D.

Skan

unread,
Apr 12, 2008, 1:35:31 PM4/12/08
to xblite
Dear D., :)

That was it, though the VarSetCapacity() is not needed.
You have been very kind and have saved me many hours of newbie
frustation.

Many thanks and best regards.
Reply all
Reply to author
Forward
0 new messages