3

Is there an equivalent of the following on Windows?

#include <dlfcn.h>
#include <stdio.h>

void main_greeting(void)
{
    printf("%s\n", "hello world");
}

void lib_func(void)
{
    void (*greeting)(void) = dlsym(RTLD_MAIN_ONLY, "main_greeting");

    greeting ? greeting() : printf("%s\n", dlerror());
}

int main(void)
{
    lib_func();

    return 0;
}

This is a short snippet, the real purpose is to call a function know to exist at a main process (main_greeting), from inside a function (lib_func) from a dynamic loaded library. The main process is not modifiable, and so cannot be rewritten to pass callbacks.

oblitum
  • 11,380
  • 6
  • 54
  • 120
  • I don't understand, there are lots of C compilers for Windows. – Hogan Jan 26 '13 at 14:53
  • @Hogan, this is not much about compilers, it's more about platform process/library tooling. – oblitum Jan 26 '13 at 14:55
  • I doubt it. If there's no debugging information either in the executable itself or in the symbol file (e.g. .pdb), you can't locate a function by its name in the .exe because they are not supposed to export functions. You most likely would need to find the function relative to the base of the .exe, find the base at run time, add the offset and only then call. – Alexey Frunze Jan 26 '13 at 14:55
  • "call a function know to exist at a main process from inside a function" is difficult to understand, please clarify – CharlesB Jan 26 '13 at 14:56
  • @chico - See Anton Kovalenko's answer below -- clearly this is simple via gcc (for example). Thus the real question was not as above as pointed out by Alexy Frunze. The real question was "I can't get this to work with MS's compiler. (Maybe the phrasing of my comment failed to get this across :) – Hogan Jan 26 '13 at 15:11
  • @Hogan, sorry, I'm being generalist here. The question is really toward platform/library tooling. I want to know whether the Windows OS allows this for `.exe's`. Knowing MinGW provides exporting from executables through a simple option is nice to know. I'm not asking specific for msvc, I want to know about its options too. – oblitum Jan 26 '13 at 15:17
  • @AlexeyFrunze, Thanks, I know about the hacking way when the PE don't have exports, but I'm not really toward using this option. I wanted to know two things, "Is it possible to have executable exports?" and "How to call them?" AntonKovalenko had figured it out. – oblitum Jan 26 '13 at 15:32

1 Answers1

5

On Windows, executables and DLLs are of the same format (PE nowadays), so an executable can export functions too. GetProcAddress(GetModuleHandle(NULL),TEXT("main_greeting")) will do what you want if the function is exported from the executable. It's done by -Wl,-export-all-symbols for mingw GCC.

I believe there is no equivalent option for Microsoft's linker, so if you use their toolchain, you have to:

Community
  • 1
  • 1
Anton Kovalenko
  • 20,999
  • 2
  • 37
  • 69
  • Thanks, I'd like to know whether msvc has such a option tough. – oblitum Jan 26 '13 at 14:57
  • 1
    @chico Then add that to the question and retag it. – Alexey Frunze Jan 26 '13 at 14:59
  • @chico see http://stackoverflow.com/questions/225432/export-all-symbols-when-creating-a-dll for a two-stage solution (involving autogenerated .def file) – Anton Kovalenko Jan 26 '13 at 14:59
  • @AntonKovalenko Looking at http://msdn.microsoft.com/en-us/library/28d6s79h.aspx I see, for msvc, it boils down to using Module-Definition Files for defining the exports of .exe's. – oblitum Jan 26 '13 at 15:27
  • @chico There's no problem if you don't mind writing .def file manually, listing all exported functions. The link above was for *automatic* generation of a .def file to export *everything*. – Anton Kovalenko Jan 26 '13 at 15:30
  • @AntonKovalenko could you replace "(don't know of msvc equivalent)" by mentioning it boils down to that? (`/DEF` and Module-Definition Files) – oblitum Jan 26 '13 at 15:35
  • @AntonKovalenko and also stating that, as such, Windows `.exe's` can have exports too as dynamic libraries have. (I guess I'm building an answer of my own :P) – oblitum Jan 26 '13 at 15:38