Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

GetModuleFileName equivalent?

0 views
Skip to first unread message

Petteri Heiskari (petteri at timehouse.fi)

unread,
Mar 12, 2003, 11:46:20 AM3/12/03
to
Hi!

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

Basile STARYNKEVITCH

unread,
Mar 12, 2003, 1:00:45 PM3/12/03
to
>>>>> "Petteri" == Petteri Heiskari (petteri at timehouse fi) <nos...@nospam.net> writes:

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

Eric P. McCoy

unread,
Mar 12, 2003, 1:21:18 PM3/12/03
to
Basile STARYNKEVITCH <basile+NO@SPAM+starynkevitch.net.invalid> writes:

> 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

John Reiser

unread,
Mar 12, 2003, 1:43:25 PM3/12/03
to
> Is there a way to get my shared library's full path name as
> GetModuleFileName does in Windows?

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;
}
-----

Petteri Heiskari (petteri at timehouse.fi)

unread,
Mar 13, 2003, 1:26:08 PM3/13/03
to
Thanks!

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

0 new messages