There is nothing in standard C that will do what you're trying to do.
The C standard doesn't even mention DLLs. The closest thing to what
you're looking for is probably argv[0], which *usually* points to a
string containing the name of the program.
<--
Thanks for reply!
I will ask the windows group, but I guess the windows way is to call
an Win32 API function. But I don't want to #include <windows.h> for
that. Though, just off the top of my head: I can probably declare
needed API function, and link with one of windows' dll's (kernel32.dll
or user32.dll or whatever) and make sure calling conventions
difference between my c dll and the API function is the same. And name
mangling might be the issue...
But, I don't want to deal with Windows API here.
-->
use it or don't use it...
ugly hackery like this is ill-advised...
note that some common ways to deal with portability issues are:
put OS-specific code in separate files, and only include them in the build
for a particular OS (downside: this complicates building the project);
make use of the preprocessor ("#ifdef", "#ifndef", "#endif", ...) to mark
off OS-specific sections (nearly all major compilers for major OS's use
special #define's to indicate the OS, CPU arch, compiler, ...);
....
the preprocessor option can be used by itself, or mixed with the
OS-specific-file strategy, to good effect. this can allow essentially
leaving out all the OS-specific stuff when not building for the OS for which
it applies.
admittedly, it is not clean, and hence it is a good idea to partition off
non-portable code into its own areas.
in some cases, I use a name suffix such as 'OS' or 'W32' or 'Lnx' to mark
functions which are inherently non-portable ('OS' mostly for ones where the
internal logic has to be changed for each OS, and 'W32' or 'Lnx' for
functions which are only valid on a particular OS).
if possible, with 'OS' functions, if no good options exist (such as on an
unknown OS) then it may make sense to try to fall back to faking the
facility (if possible), or failing gracefully.
elsewhere, code can then try to be generic and call into these OS-specific
sections with caution.
<--
Well, if there is no way to get the dll's path (and/or .o .so file for
Linux), what can I learn about the dll (.so) using CRT? nothing?
-->
pretty much...
within the bounds of standard C, one can do almost nothing of the sort.
(one may soon find that for nearly much of anything "non-trivial" they will
have to go outside the standard, but this does not necessarily rule out
being able to write portable code).
as an example, consider Quake2:
the code is generally terrible;
it uses things outside the C standard all over the place;
it makes heavy use of OS-specific facilities;
....
yet, it is still relatively easy to port:
it builds on Linux, and it builds on Windows, and it builds on OSX;
it can be built fairly easily with Cygwin, with MinGW, or with MSVC;
people have gone as far as to get it to build on .NET and Mono (but I guess
this was more of a "port" though);
....
and, it does all this without resorting to any of the usual "portability
tools" (there is no "configure" in the mix, ...).
this is in contrast to many claimed "cross platform" apps, which are very
difficult to get to build outside of the narrow OS(s) they are designed to
work with (like an app which claims to be cross-platform, but uses
"configure", tightly depends on GTK, ... and hence is very difficult to
build on Windows, so help you if you want to use MSVC...).
IMO, "configure" is a false sense of security, as many using it I guess
expecting it to "magically" make their code portable.
<--
What about main program folder? Can I get the it from the dll without
passing argv[0] to it? Is it stored in an environment variable
accessible for the current process?
-->
once again, there is no way to do this in the C standard, but the problem is
much easier with the process described earlier.
so, maybe rather than thinking of it as "all or nothing", instead consider
it more like this:
stategy A on Windows;
strategy B on Linux;
stategy C on ...;
fail gracefully if unknown (such as returning NULL for the image name, ...),
and have the caller willing to accept NULL as a valid answer.
or such...