overriding C runtime functions with my own implementation

  • Thread starter Thread starter Jonathan Wilson
  • Start date Start date
J

Jonathan Wilson

I have the following:
1.A C++ dll (with no managed code whatsoever) compiled with visual C++ 2005
that links to libcmt.lib as its runtime library.
and 2.A piece of binary code (also compiled with visual C++ 2005) that I
cant change or have changed which calls a certain c runtime function (for
arguments sake, lets call it xyz)

What I want to do is to tell the linker "don't use the implementation of
xyz inside libcmt.lib, use the implementation of xyz inside myobj.obj
instead." Is this possible? If so, how?

Note that this has to be something that can be done automatically without
any extra human steps other than the usual "build-build solution" and it
must not require modifying the visual C++ install (e.g. recompiling or
modifying the CRT is out of the question) or the binary part mentioned above.
 
Jonathan said:
I have the following:

This doesn't appear to be a dotnet (e.g. C++/CLI or managed C++)
question, so would be better in microsoft.public.vc.language. Anyway:
1.A C++ dll (with no managed code whatsoever) compiled with visual C++
2005 that links to libcmt.lib as its runtime library.

libcmt.lib is the static multithreaded CRT, right?
and 2.A piece of binary code (also compiled with visual C++ 2005) that I
cant change or have changed which calls a certain c runtime function
(for arguments sake, lets call it xyz)

What do you mean by a 'piece of binary code'? Do you mean a VC++2005
..obj file? A static library? An exe or dll?

If it's an obj or lib, does you C++ dll have any relationship to it? For
example, does the DLL need to call functions in this binary code?
What I want to do is to tell the linker "don't use the implementation of
xyz inside libcmt.lib, use the implementation of xyz inside myobj.obj
instead." Is this possible? If so, how?

Note that this has to be something that can be done automatically
without any extra human steps other than the usual "build-build
solution" and it must not require modifying the visual C++ install (e.g.
recompiling or modifying the CRT is out of the question) or the binary
part mentioned above.

Ok, I think you have some compiled code (a .lib or .obj which I'll call
X) which contains some functions you need to call from your DLL. Right?
If that's the case, you can do this:

Create a second DLL which includes exported functions that are wrappers
for the required functions of X. This DLL links statically with X, and
includes myobj.c (or .obj). This DLL includes X in the linker settings
as well as libcmt.lib (using /MT). Finally, to resolve any multiple
definition errors, use /FORCE:MULTIPLE in the linker commandline - the
one defined in the project (myobj.c) should take precedence.

Now your DLL can just link to this, and is completely isolated from the
hacking that is used in the second DLL, but note that they will have
separate CRT heaps and other CRT state, so don't go allocating memory in
one DLL and freeing it in the other.

Tom
 
Jonathan said:
I have the following:
1.A C++ dll (with no managed code whatsoever) compiled with visual C++
2005 that links to libcmt.lib as its runtime library.
and 2.A piece of binary code (also compiled with visual C++ 2005) that I
cant change or have changed which calls a certain c runtime function
(for arguments sake, lets call it xyz)

What I want to do is to tell the linker "don't use the implementation of
xyz inside libcmt.lib, use the implementation of xyz inside myobj.obj
instead." Is this possible? If so, how?

Note that this has to be something that can be done automatically
without any extra human steps other than the usual "build-build
solution" and it must not require modifying the visual C++ install (e.g.
recompiling or modifying the CRT is out of the question) or the binary
part mentioned above.

If you define the function in your source files, they take precedence
over any same named library function.

Try this:

#include <stdio.h>

int printf (const char *format, ...)
{
// blank implementation
return 0;
}

int main (int argc, char *argv[])
{
print("Hello world!");
}

then compile it and see if it uses your function or library.

If you cannot relink the project, you should look into API hooking. It's
a hacker technique that relies on overwriting JMP instructions in DLL
import table. Google it up.

Roman
 
Back
Top