Call a foreign-language function: Difference between revisions

Content deleted Content added
m →‎{{header|Fortran}}: some precisions
PureFox (talk | contribs)
Added FreeBASIC
Line 614: Line 614:
call free(ptr)
call free(ptr)
end program</lang>
end program</lang>

=={{header|FreeBASIC}}==
Normally it's an easy matter to call a function in the C Standard Library, statically, from FreeBASIC.
However, 'strdup' isn't in the Standard Library so instead we will call the version in the Windows Shell, dynamically.
As this uses LocalAlloc in kernel32.dll internally to allocate memory for the duplicated string, we need to call
LocalFree to free this memory using the pointer returned by strdup.
<lang freebasic>' FB 1.05.0 Win64

'Using StrDup function in Shlwapi.dll
Dim As Any Ptr library = DyLibLoad("Shlwapi")
Dim strdup As Function (ByVal As Const ZString Ptr) As ZString Ptr
strdup = DyLibSymbol(library, "StrDupA")

'Using LocalFree function in kernel32.dll
Dim As Any Ptr library2 = DyLibLoad("kernel32")
Dim localfree As Function (ByVal As Any Ptr) As Any Ptr
localfree = DyLibSymbol(library2, "LocalFree")

Dim As ZString * 10 z = "duplicate" '' 10 characters including final zero byte
Dim As Zstring Ptr pcz = strdup(@z) '' pointer to the duplicate string
Print *pcz '' print duplicate string by dereferencing pointer
localfree(pcz) '' free the memory which StrDup allocated internally
pcz = 0 '' set pointer to null
DyLibFree(library) '' unload first dll
DyLibFree(library2) '' unload second fll
End</lang>

{{out}}
<pre>
duplicate
</pre>


=={{header|Go}}==
=={{header|Go}}==