Popup YouTube Video
Sheerpower Logo

Calling Procedures Written in Other Languages


Sheerpower can call procedures written in other programming languages as long as they are contained in a DLL as a global routine. An example of this is calling routines in the Microsoft provided msvcrt.dll library. The call statement is used to call and execute library routines. These routines can perform procedures so the developer does not have to write the code from scratch.
Note: When calling a routine, you must enclose its name in quotes. For example: "strlen"(). This ensures the routine's upper and lower case formatting is preserved.
The library to which the routine belongs must have been specified in a library statement.

Routines within a DLL are called using the call statement, followed by a quoted string which contains the name of the routine to be called:
Some routines take arguments. By default, most arguments are passed by their value. Strings and arrays are always passed by reference. If you want to explicitly pass an argument by reference (the pointer to the argument), use the by ref option:
myvar% = 45 call "my_routine" (myvar% by ref, myarray%() by ref)

When a routine is called, it may also return a value. The variable _integer will hold this value if the routine returns an integer. If the routine returns a pointer to a null-terminated character string (char*), you can call the routine with the prefix char*. After the routine completes, _string$ will contain a copy of the returned string.

For example:

myvar% = 45 call "char* my_routine" (myvar% by ref, myarray%() by ref) print 'Returned string was: '; _string$

This demonstrates how _string$ automatically holds the returned string when the routine is called with the char* prefix.

CSTRING$ is the other way to the same text: after a call made WITHOUT the char* prefix, _integer holds the returned pointer as a number, and cstring$ (no arguments) returns the null-terminated string at that address. Use it when you want the address as well as the text, or decided on the prefix too late. A null pointer (0) gives an empty string; a value that is not the address of readable memory -- _integer after strlen, say -- raises the catchable exception Bad CHAR* pointer (4011) rather than reading garbage.

library 'msvcrt.dll' call 'getenv' ('SystemRoot') print 'the address: '; _integer > 0; ' the text: '; cstring$ call 'char* getenv' ('SystemRoot') print 'the same through the prefix: '; _string$ // the address: 1 the text: C:\WINDOWS // the same through the prefix: C:\WINDOWS
The examples below use the Microsoft-provided msvcrt.dll library and calls the strlen and _getcwd functions/routines.
library 'msvcrt.dll' text$ = 'Hello there!' call 'strlen' (text$) print 'The length of text$ is: '; _integer call 'char* _getcwd'() print 'The current working directory is: '; _string$
While Sheerpower prevents common failure modes such as stack overflows and memory corruption within your business logic, DLL routines execute as native code and can still fail or crash if implemented incorrectly. Use only well-tested system libraries (like msvcrt.dll) or thoroughly validated custom DLLs.

(Show/Hide Sheerpower External Calls Takeaways)
Hide Description

    

       


      

Enter or modify the code below, and then click on RUN

Looking for the full power of Sheerpower?
Check out the Sheerpower website. Free to download. Free to use.