Liberty BASIC Help Online

Passing strings into API/DLL calls
 
Liberty BASIC provides two ways to pass strings as parameters into API/DLL calls. By default when passing a string as a parameter, Liberty BASIC copies the string and adds an ASCII zero to the end of the copy.  This ASCII zero is called a "null terminator."  Most Windows API calls expect this kind of zero-terminated string. This technique provides a copy of the original string to be passed to the API function. If the function then modifies that string directly (as in the kernel API function GetProfileString and a few others), then the Liberty BASIC application calling the function cannot access the modified string because only the copy is modified.
 
The way to fix this is for the code to include the ASCII zero onto the end of the string that will be used as a parameter in the API call.  Liberty BASIC checks for the ASCII zero, and if it finds it, passes the memory address of original string and does not make a copy.  This is only necessary if an application passes a string, expecting to get it back modified by the API or DLL function called.
 
Here is a code segment to get printer info using the GetProfileString API call:
 
    'getpstr.bas - Get information using the GetProfileString API call
 
    open "kernel32.dll" for dll as #kernel
 
    'Notice that no ASCII zero characters are added to these strings
    'because the program will not need to read any results out of the call
    'to GetProfileString.
    appName$ = "windows"
    keyName$ = "device"
    default$ = ""
 
    'add an ASCII zero so Liberty BASIC will not pass a copy of result$
    'into the API call, but the actual contents of result$
    result$ = space$(49)+chr$(0)
    size = 50 '49 spaces plus 1 ASCII zero character
 
    calldll #kernel, "GetProfileStringA",_
        appName$ as ptr,_
        keyName$ as ptr,_
        default$ as ptr,_
        result$ as ptr,_
        size as long,_
        result as long
 
    close #kernel
 
    'display the retrieved information up to but not including the
    'terminating ASCII zero
    print left$(result$, instr(result$, chr$(0)) - 1)
 
    WAIT
 


Copyright (C) 2003 Shoptalk Systems
Liberty BASIC - http://www.libertybasic.com/