Passing a pointer to a pointer in Smalltalk
In Smalltalk, you can pass a pointer created in C or COBOL to a Smalltalk pointer. Consider the following record definition:
void pointerToPointerToChar ( char ** ppChar );
The code below can be used to call the above function:
| func1 pChar ppChar theString |
func1 := PlatformFunction callingConvention: 'c'
function: 'pointerToPointerToChar'
library: 'mylib'
parameterTypes: #(pointer)
returnType: #none.
"Create a pointer to a String. Memory must be freed when finished"
theString := 'String to be passed'.
pChar := theString copyToOSMemory.
"Create a ByteArray to store the address of pChar. Passing a ByteArray
to a PlatformFunction expecting parameter with type 'pointer' will
result in a pointer to the ByteArray being passed to the external function"
ppChar := ( ByteArray new: 4 ) uint32At: 0 put: pChar asInteger.
func1 callWith: ppChar abtAsExternalPassedPointer.
"Extract the null-terminated string from the pointer address"
theString := pChar abrAsString.
"Free the memory allocated for pChar"
pChar free.
Last modified date: 01/29/2015