DLL EXE Hybrid C++ Windows - c++

I am currently working with DLL injection and need to have a single hybrid binary that could act as both an executable and a DLL. I thought of maybe writing a DllMain and WinMain function and then compiling it as an executable but I don't know what would happen if I did that. I know that it is posssible to combine a dll and exe by using something like thinstall or extracting the dll to a temporary location then going from there but I don't want to mess with any of that stuff. So basically, is it possible to define a WinMain and Dll Main and then use the resulting executable as both, and if not, is this even possible? Thanks in advance!

No.
Both a DLL and an EXE have a PE (Portable Executable) header. That header has a field IMAGE_FILE_HEADER::Characteristics. Bit 14 of that field is either 0 (for an EXE) or 1 (for a DLL).

Why don't you put all the common code into a static library (.lib) and have both DLL project and EXE project as a very thin wrapper around the static library?

You could create a temporary copy of your executable, patch the PE header and inject this copy. Another way is to put the DLL as binary resource to the executable. On runtime you can write this binary resource to a temporary file and use this for injection.

It is possible to export functions from a exe too. So you should be able to LoadLibrary("foo.exe") followed by GetProcAddress(hFoo,"bar")

Related

Insert dll code inside executable to make it free from dependecies

I've an executable with a lot of dll from which it import function. I need to know if there's a way to insert the code of .dll inside the .exe to make it free from others file,so to have just the executable distributed.
Thanks to who'll answer.
The point of a DLL (Dynamic Link Library) is that it is separate from the executable. There's no conventional way to do so since the DLL is precompiled, meaning that a compiler cannot reference the source code and directly compile it to your executable.
If you have the source of the library, you can compile the library statically with your executable and then they will all be output into a single executable file.
Yes, that's possible, but you need 3rd party tools. You need so-called "exe bundle", just google it.
You need to check why did you created a dll in the first place . http://cs-fundamentals.com/tech-interview/c/difference-between-static-and-dynamic-linking.php . Changing from dll to static linking lib might defeat some basic design purpose .
Quinn Kirwan has provided the right answer, you need the source code of all the dll's that you have linked to your exe. Then you have to go to the project properties and liker properties and select the option to statically link the dll's. On successful steps you will get a single exe. Please note that the size of this exe will be much larger than the one you have now. Because all the functionality provided by the dll's will be part of the exe now.
Thanks

How do you combine a dll with your application?

Kind of a simple question, but when I compile an application I'd like to make it so the user doesn't need all the dll's/shared objects and can just have one standalone executable. How do I do this? Either in a Makefile or in CodeBlocks' settings.
If you have the source, or if you have the right kind of .lib file, you can statically link. (Note, however, that DLLs also sometimes come with a .lib file, and that file is just a shim for calling into the DLL.) If you can statically link, you'll have a single executable.
Other than that, make an install directory and put the DLL into the same directory as the executable. That's the easiest way.
There's probably a trick where you could inject the DLL into executable and write a custom loader that would act like a DLL, but you really don't want to go down that path.
You can use a static link library instead of a DLL if you have the source code.

How do I add some external executable to a Borland C++ Builder 2010 project?

So the question is how to add to c++ builder 2010 some external .exe file? Let's say that i made some program in visual basic and have the exe file, so that does not need to make the same code in c++ i want to just include that exe in my project? Is it possible to make portable application one exe that have inside him self another exe file (maybe in resource path)?
How to call it in code if it is one exe in other? I know to call it by system function, or other by putting direction to the exe, but how to do that if it is on same address as are main exe?
I don't understand exactly what you're trying to accomplish. If you want to use the functionality of a given program, you're going to have to know things about how that program works.
If you want to take a given executable, and call it as you would a shell script, then you would need to start the given program with it's standard input and standard output redirected to a pipe. An example of how to do that is available on MSDN. If you want to be able to just look at the Visual Basic classes and methods in the target EXE, as you could do with Visual Basic .NET, you are out of luck, as an arbitrary executable does not understand the concept of a class or method.
Use the Project > Resources dialog to add the VB .exe file to your project and give it an ID. At runtime, your C++ code can then extract the resource data for that ID to a temporary file, such as with TResourceStream and TFileStream, and then use CreateProcess() to run it. Don't forget to delete the file when you are done using it.
Otherwise, re-write the VB code into a DLL instead, and then the C++ app can simply call the DLL's exported functions when needed. If you want to ship a single self-contained .exe (which is generally not how DLLs are used), then you will have to use the same resource approach, just use LoadLibrary() and GetProcAddress(), instead of CreateProcess(), to access the DLL functions dynamically.

How to call a unmanaged C++ exe from managed c++ exe

Please tell me how to call a unmanaged c++ exe functions from managed c++ exe or dll? help with code example would be more useful.
Thank you
You need to link against the library that contains the function you want to call, include a header that defines the function and then just call it.
Without a more specific question you're not going to get much more than that.
I think you should have a look at P/Invoke. Using this tech you could call any unmanged function exported in a DLL or EXE from a managed function.
For example:
http://www.codeproject.com/KB/cs/essentialpinvoke.aspx
The managed/unmanaged is a red herring. When you have some code you want to call, it should be in a lib, a DLL, or a COM exe. A regular double-click-it-to-run-it exe that doesn't implement any COM interfaces doesn't expose any of its code to outside callers. If you just want to run it, you can use Process.Start to launch the whole exe. Otherwise you're going to need to re-architect a bit (this will involve having the source code to the other exe.) Generally I pull most of the functionality into a lib or dll, have the original exe call into that library to get its work done, and have the new exe also call into the same library.
Since you're in C++/CLI, do not go COM Interop or P/Invoke. IJW is way easier (it just works, right?) Include the header, link to the lib. Done! But as you now see, getting the lib can be the big first step.

Windows: Changing the DLL search order for an Exe

I have a C++ Exe in an application directory which contains the DLLs used by it. Now, for some testing purpose I need to modify an existing DLL and use that instead of the original one. But in order to not modify the existing installation I cannot backup the existing DLL and replace it with the modified one or move the existing one elsewhere. I also cannot change the Exe. The 2 DLLs need to exist side by side. The only change should be that the Exe should transparently load the modified DLL which is in some other folder rather than the existing DLL which is in the same folder as the Exe. Is there some elegant way of doing it?
I looked at some MSDN articles but could not find a way of doing this. The solution should work on Windows XP and up.
Windows will load at most one version of each DLL name per process. If it loads a DLL listed in HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\Windows\AppInit_DLLs, it won't load a similarly-named DLL later. But in AppInit_DLLs you can list a DLL with an explicit path, overriding the normal LoadLibrary() order.
Hence, temporarily put your test DLL in AppInit_DLLs and it will override any other DLL with the same name.
According to MSDN, it will always start by the application directory (unless you modify it with the alternate search order method...) so it seems to be difficult. You can still copy the executable and its other dependencies elsewhere. It is not that elegant though.
Or you can launch the executable that you have copied elsewhere along with the new DLL, from the original directory. According to the search order it should work too, though I must admit I have never tried.
You can hook LoadLibrary() calls for your process from the beginning. When your patched version of LoadLibrary() sees your DLL's it calls original LoadLibrary() with modified DLL's path.Even if you don't use LoadLibrary() call to load your DLLs, Windows CRT does. So this technique must work.
The only way I know would use LoadLibrary API including the path, but you say you can not change the exe.