Fhiannan McIvor
Fhiannan McIvor

Reputation: 31

call function in unrelated executable linux

If I have a pointer to a function and I give that to another (Unrelated/Child) Executable, how can I call that function without making a segfault?

At the moment I can create a function and assign it this memory adress:

Dim As Function (ByRef As String) As Integer MyFunction
' get pointer...
MyFunction = FunctionPointer

But then calling MyFunction I get a segfault (Obviously because the function I am calling is in another executables adress space whitch I am not allowed to access)

How can I fix this/Work around it?

Upvotes: 2

Views: 1677

Answers (2)

Employed Russian
Employed Russian

Reputation: 213877

Update:

Note: this solution no longer works for newer GLIBC versions: dlopen refuses to open PIE binaries.


If you have control over the other executable (the one you want to call a function from), then build it as a PIE (position-independent executable), and load it into the first executable's address space.

In C, you would build with -fPIC -pie, then use dlopen(3) and dlsym(3).

In BASIC, I have no clue ;-(

Upvotes: 9

Kerrek SB
Kerrek SB

Reputation: 477484

It's not so much that you're "not allowed to access" the address space of the other function, but rather that that space is a totally different and unrelated address space. Each process has its own virtual address space, so the numeric value of your pointer has no meaning inside another functions address space, even if you were able to exchange it somehow.

For general inter-process communication you usually request shared memory explicitly from the system, but I'm not sure if FreeBasic exposes such functionality. Why not look up some existing remote procedure call libraries?

Upvotes: 2

Related Questions