LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   help translating pascal's libc "dlopen , dlsym and dlclose" into c source ... (https://www.linuxquestions.org/questions/programming-9/help-translating-pascals-libc-dlopen-dlsym-and-dlclose-into-c-source-440172/)

alred 04-29-2006 10:47 PM

help translating pascal's libc "dlopen , dlsym and dlclose" into c source ...
 
i have encountered problems when translating a pascal procedure (which uses libc functions :: dlopen , dlsym and dlclose) into c source ... hint and direct me to the solution(preferably with smaller simpler c code examples) ...

below is the pascal code , i try to implement it in a self-contained pascal procedure with only one "includes"("uses" in pascal) libc at somewhere else ... it might not be the "correct" way of coding it but i managed to just compile and run the whole app , just run ...

Code:

procedure TForm1.Button3Click(Sender: TObject);
type
TGTKModuleHandle=Pointer;
const
NIL_MODULEHANDLE=TGTKModuleHandle(NIL);
var
s:string;
lib_name,func_name:pchar;
call_func: function(pass_strings:Pchar):Pchar;cdecl ;
GTKModuleHandle:TGTKModuleHandle;
begin

if GTKModuleHandle <> NIL_MODULEHANDLE then
dlclose(GTKModuleHandle);
   
GTKModuleHandle:=NIL_MODULEHANDLE;

lib_name:='libcallgtk02.so';
GTKModuleHandle:=dlopen(lib_name,RTLD_NOW);

try
if GTKModuleHandle <> NIL_MODULEHANDLE then
begin
//  char *call_gtk02(char *pass_strings);  in  libcallgtk02.so
func_name:='call_gtk02';
call_func:= dlsym(GTKModuleHandle,func_name);
s:=call_func('dlopen strings to here ...');
memo1.lines.add('return from dlopen ----> '+s);
end else
 begin
 showmessage('cant locate your function ...');
 exit;
 end ;
finally
if GTKModuleHandle <> NIL_MODULEHANDLE then
dlclose(GTKModuleHandle);
end ;//end try

end;

note :: its a kylix/pascal gui with a button and a text-memo gadget and by clicking the button , it call a function came "packaged" in a shared library which run a gtk app(but modal window) , the name(in strings) of the library and the function have been known beforehand , can have a look at the shared library here :: HERE ...



//thanks in advance ...


.

paulsm4 04-30-2006 04:49 PM

Hi -

I noticed this has been here for awhile...

I'm really not sure what your question is:

1. You're porting an application from Kylix/Pascal (essentially, a Linux version of Borland Pascal) into C. Is this correct?

2. You've encountered places in the Pascal code where it calls dlopen, etc.

These Pascal codes (as it looks like you already know) are simple wrappers around the
standard libc calls. In other words, "dlopen()" and friends are *already* in C.

3. If you want to use dlopen() etc from your C program, you can simply call them.

Most programs don't even need explicitly do a "dlopen(): everything's automagically
taken care of when the C/C++ program is linked, and the "shared libraries" are com-
pletely transparent to the developer).

4. Finally, if you're interested in how dlopen() etc are linked: just look in the kernel source (the C library function is, like your Kylix counterpart, just a thin wrapper).

'Hope that helps .. PSM


All times are GMT -5. The time now is 03:28 PM.