One of my colleagues posed an interesting question the other day over an excellent Thai lunch (after we got tired enough walking through booths at RSA 2008) - how does one export and use stuff from an exe ?

The short answer I gave was yes you can. A not-so-short answer could have been I do not know why you would do such a thing but you can. After all .exe is not very different from a .dll as far as exporting and importing is concerned. I remembered having done it, for reasons unknown, in win16 days. But I could not recollect all the details of the whole deal so it was time to create some sample apps to see how things go.

Exporting functions and variables is no different from exporting in a dll or library. I used __declspec (dllexport) to export a variable and a function from my console exe app (named expexe).

Exporting a function and variable from .exe

Then I created a dll project (named expdll) and declared expexe_func as extern and in linker input I specified expexe.lib, the import library that got generated when I built expexe project. And the dll built. Because I did not want to create yet another exe to test calling into expdll, I made the export so that it can be called by rundll32.

 Dll Importing a function exported from exe

Now it was time to check what happens when we load this dll. After copying expexe.exe to the same directory, I gave the following command

rundll32 calling an export from a dll

rundll32 ran showing me a message box with 42 (which is what is returned from expexe_func) in it. So there is nothing special about exporting a function from exe and calling it from dll so far. Well what about dynamic linking, will that work ? Yes it does. Well what if the exe was dependent on the dll as well ? In other words exe imports a function from dll which imports from the exe. That is no different from a circular dependency between 2 dlls, so nothing different there.

Now I know why nothing came to mind when I pondered over my colleague's question - it must have been a totally forgettable nothing-special-to-be-noted type of experience back then as it is now.

Ciao.

Tagged with →  
Share →

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop us a note so we can take care of it!

Visit our friends!

A few highly recommended friends...

Set your Twitter account name in your settings to use the TwitterBar Section.