Windows has an utility called rundll32.exe that can execute native dynamic link libraries as applications.
Say I have a piece of code that prints “Hello World!” to the console. Is it possible to write a library in C++ (preferably Visual C++) that can be executed using rundll32.exe and will run this code? If so, how?
Googling “rundll32”, the 3rd hit was a link to documentation,
http://support.microsoft.com/kb/164787
According to that documentation,
rundll32calls a user-specified function with signature likewWinMain(except the first argument here is a window handle instead of an instance handle),So, trying this out:
Building & running:
The output is presented in its own console window, created via
AllocConsole, which is generally necessary since rundll32 is a GUI subsystem program (this is also the reason for thefreopencalls).To present the output in an existing console window one can just omit the calls to
AllocConsoleandfreopen, and redirect standard output ofrundll32to a pipe. E.g. standard output can be piped through Windows’morewhen the output is just a few lines, or through some *nix-utilitycatfor more lines. However, in the standard command interpreter [cmd.exe] it doesn’t work to just redirect the output tocon.