Is there a way to get my shared library's full path name as
GetModuleFileName does in Windows? Actually I could do with only path of
the library. My library will be loaded as dynamic library with dlopen().
Regards, Petteri Heiskari
Petteri> Hi! Is there a way to get my shared library's full path
Petteri> name as GetModuleFileName does in Windows? Actually I
Petteri> could do with only path of the library. My library will
Petteri> be loaded as dynamic library with dlopen().
I suggest to use the dladdr call of the libld library. The dladdr call
is not as portable as dlsym (it might be Linux specific, or at least
not in latest Posix spec). From the include file <dlfcn.h> on my Linux
Debian/Sid box:
typedef struct
{
__const char *dli_fname; /* File name of defining object. */
void *dli_fbase; /* Load address of that object. */
__const char *dli_sname; /* Name of nearest symbol. */
void *dli_saddr; /* Exact value of nearest symbol. */
} Dl_info;
/* Fill in *INFO with the following information about ADDRESS.
Returns 0 iff no shared object's segments contain that address. */
extern int dladdr (__const void *__address, Dl_info *__info) __THROW;
Regards
--
Basile STARYNKEVITCH http://starynkevitch.net/Basile/
email: basile<at>starynkevitch<dot>net
aliases: basile<at>tunes<dot>org = bstarynk<at>nerim<dot>net
8, rue de la Faïencerie, 92340 Bourg La Reine, France
> I suggest to use the dladdr call of the libld library. The dladdr call
> is not as portable as dlsym (it might be Linux specific, or at least
> not in latest Posix spec).
From the FreeBSD dladdr(3) man page:
HISTORY
The dladdr function first appeared in the Solaris operating system.
But the function itself doesn't seem very portable, as the FreeBSD man
page explicitly notes that its implementation is "bug-compatible" with
Solaris's. And then goes on to list a full page of bugs.
--
Eric McCoy (reverse "ten.xoc@mpe", mail to "ctr2sprt" is filtered)
"Last I checked, it wasn't the power cord for the Clue Generator that
was sticking up your ass." - John Novak, rasfwrj
The actual pathname used is .l_name of one of the struct link_map
on the chain of .l_next (or .l_prev):
-----
#include <link.h>
#include <dlfcn.h>
main()
{
struct link_map const *const lm = dlopen(0, RTLD_LAZY);
return 0;
}
-----
This did it perfectly:
char *GetModuleFileName( char *pDest, int nDestLen )
{
Dl_info rInfo;
char *pPos;
*pDest = 0;
memset( &rInfo, 0, sizeof(rInfo) );
if ( !dladdr(GetModuleFileName, &rInfo) ||
!rInfo.dli_fname ) {
// Return empty string if failed
return( pDest );
}
lstrcpyn( pDest, rInfo.dli_fname, nDestLen );
return( pDest );
}
Petteri