168. Calling the strcat() foreign function
The strcat()
foreign function is part of the C standard library and has the following signature (https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strcat-wcscat-mbscat):
char *strcat(char *strDestination, const char *strSource);
This function appends the strSource
at the end of the strDestination
. The function doesn’t get these strings. It gets two pointers to these strings (so, two ADDRESS
) and doesn’t return a value, so we rely on FunctionDescriptor.ofVoid()
, as follows:
Linker linker = Linker.nativeLinker();
SymbolLookup libLookup = linker.defaultLookup();
try (Arena arena = Arena.ofConfined()) {
MemorySegment segmentStrcat
= libLookup.find("strcat").get();
MethodHandle func = linker.downcallHandle(
segmentStrcat, FunctionDescriptor.ofVoid(
ValueLayout.ADDRESS, ValueLayout.ADDRESS));
...
Since the arguments of strcat()
are two pointers (ADDRESS
), we have to create...