这是我测试的代码
library DllTest;
{ Important note about exception handling across multiple
binary modules (EXEs and shared objects):
All projects must be built with the same version of the
baseclx runtime package for exception handling to work.
If this is not the case, exceptions raised in one module
may cause unintended side-effects in other modules. }
uses SysUtils;
// stdcall: with c call, must user "cdecl"
function Test(X: Integer;
var Y: integer;
str: PChar): Integer;
cdecl;
//stdcall;
begin
Y:= X+1;
StrCopy(str, 'HELLO Linux String argument');
Result:= X;
end;
exports
Test;
begin
end.
以下用kylix调用
program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;
function Test(X: Integer;
var Y: integer;
str: PChar): Integer;
cdecl;
external 'libDllTest.so';
var
X, Y: integer;
str: array [0..1024] of Char;
begin
X:= 100;
X:= Test(X, Y, str);
writeln(Format('X=%d, Y=%d, str=%s', [X, Y, str]));
readln;
end.
以下用c调用
// Load the math library, and print the cosine of 2.0:
/* If this program were in a file named "foo.c", you would
build the program with the following command:
gcc -rdynamic -o CTest CTest.c -ldl
*/
#include <stdio.h>
#include <stdlib.h>
#include <dlfcn.h>
int main(int argc, char **argv) {
void *handle;
int (*Test)(int, int *, char*);
char *error;
int X, Y;
char pstr[1024];
handle = dlopen ("./libDllTest.so", RTLD_LAZY);
if (!handle) {
fputs (dlerror(), stderr);
exit(1);
}
Test = dlsym(handle, "Test");
if ((error = dlerror()) != NULL) {
fprintf (stderr, "%s/n", error);
exit(1);
}
X = (*Test)(300, &Y, pstr);
printf ("Test()=%d Y=%d STR=%s/n", X, Y, pstr);
dlclose(handle);
getchar();
}