e.g.
//library.h - provides headers, source linked in from library
void LibraryFunction(string str);
//main.cpp
#include library.h
if(whatever)
LibraryFunction("john");
//other.cpp - file is part of program
#include library.h
//this definition will bind to the library declaration
void LibraryFunction(string str)
{
//I want to call the library function here
----->LibraryFunction(str);
cout << str << endl;
}
Constraints:
1) library.h is an outside library, which can not be changed
2) main.cpp is also part of another program and can not be changed
The only things I have access to are other.cpp, the compiler and
linker. I don't care how ugly other.cpp gets, as long as I get the
desired behavior.
Thanks everyone.
- sean
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
I think, you can use a namespace for this:
namespace library {
#include "library.h"
}
void LibraryFunction(string str)
{
library::LibraryFunction(str);
cout << str << endl;
}
Greetings,
Ernst
It dose not the solution. See the OP's constraint 1).
It will make the unresolved linking error.
How do you can make the library::LibraryFunction's definition part
move into the library namespace? Even you could not touch it.
I think that OP should make the rapper namespace on his
main.cpp's binding entities, rather than the outside library.
--
S Kim <st...@yujinrobot.com>
BR
Yes, you are right. I thought about functions which are only in the
header files. I didn't think enough.
Greetings
Ernst
> e.g.
> //library.h - provides headers, source linked in from library
> void LibraryFunction(string str);
> //main.cpp
> #include library.h
> if(whatever)
> LibraryFunction("john");
> //other.cpp - file is part of program
> #include library.h
> //this definition will bind to the library declaration
> void LibraryFunction(string str)
> {
> //I want to call the library function here
> ----->LibraryFunction(str);
> cout << str << endl;
> }
> Constraints:
> 1) library.h is an outside library, which can not be changed
> 2) main.cpp is also part of another program and can not be changed
> The only things I have access to are other.cpp, the compiler
> and linker. I don't care how ugly other.cpp gets, as long as
> I get the desired behavior.
I'm not sure I understand the problem. Do you want to intercept
all calls to the library function, in existing code, and call
the library function in your wrapper? If so, I don't think it
is possible within the framework of standard C++. At least
under Unix, however, you can do it if you use dynamically loaded
objects; you will probably have to wrap all of the functions in
library.h, however. (Basically, you link your functions
statically with the application; the first time one of your
functions is called, it uses dlopen to load the actual library,
and dlsym to find the functions in it. I don't know Windows
well enough to be able to say if the same thing is possible with
a DLL, but I suspect that it is.)
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Not as such, no.
> //library.h - provides headers, source linked in from library
> void LibraryFunction(string str);
>
> //main.cpp
> #include library.h
> if(whatever)
> LibraryFunction("john");
>
> //other.cpp - file is part of program
> #include library.h
>
> //this definition will bind to the library declaration
> void LibraryFunction(string str)
> {
> //I want to call the library function here
> ----->LibraryFunction(str);
> cout << str << endl;
> }
>
> Constraints:
> 1) library.h is an outside library, which can not be changed
> 2) main.cpp is also part of another program and can not be changed
>
> The only things I have access to are other.cpp, the compiler and
> linker. I don't care how ugly other.cpp gets, as long as I get the
> desired behavior.
If LibraryFunction is in a DLL, this should be possible.
1. Other.CPP has it's own version of LibraryFunction.
2. Other.CPP uses LoadLibrary (or whatever your OS calls it)
to load Library.DLL *AT RUN TIME* and find the address of
LoadLibrary in that DLL. This is the heart of the technique.
3. Link your program with Other.o (or Other.obj or whatever). Do
NOT link to Library.DLL.
When your code runs, all calls to LibraryFunction go to the version
you wrote. The first time your function is called, it loads
Library.DLL and saves the address of that version of LibraryFunction.
(After that, it uses the saved address.)
If LibraryFunction is NOT in a DLL -- I don't think it's possible
without resorting to extreme techniques such as altering the object
file you got from LibraryCo.
Not as such, no.
> //library.h - provides headers, source linked in from library
> void LibraryFunction(string str);
>
> //main.cpp
> #include library.h
> if(whatever)
> LibraryFunction("john");
>
> //other.cpp - file is part of program
> #include library.h
>
> //this definition will bind to the library declaration
> void LibraryFunction(string str)
> {
> //I want to call the library function here
> ----->LibraryFunction(str);
> cout << str << endl;
> }
>
> Constraints:
> 1) library.h is an outside library, which can not be changed
> 2) main.cpp is also part of another program and can not be changed
>
> The only things I have access to are other.cpp, the compiler and
> linker. I don't care how ugly other.cpp gets, as long as I get the
> desired behavior.
If LibraryFunction is in a DLL, this should be possible.
1. Other.CPP has it's own version of LibraryFunction.
2. Other.CPP uses LoadLibrary (or whatever your OS calls it)
to load Library.DLL *AT RUN TIME* and find the address of
LoadLibrary in that DLL. This is the heart of the technique.
3. Link your program with Other.o (or Other.obj or whatever). Do
NOT link to Library.DLL.
When your code runs, all calls to LibraryFunction go to the version
you wrote. The first time your function is called, it loads
Library.DLL and saves the address of that version of LibraryFunction.
(After that, it uses the saved address.)
If LibraryFunction is NOT in a DLL -- I think that the only way to
do this is to use the MACRO facility to turn all calls to
LibraryFunction into calls to MyLibraryFunction (yeech). Or there's
extreme techniques such as altering the object file you got from
LibraryCo (super-yeech).
You cannot have two functions with the same signature but you can intercept
(a.k.a. hook) a call to any function and call (or not) the original.
--
Eugene
//main.cpp
#include Library.h
...
LibraryFunction();
//MyDll.h
#include Library.h
//Export this function; wraps library.h function with different name
void MyLibraryFunction() { LibraryFunction(); }
//other.cpp
#include Library.h
#include MyDll.h
void LibraryFunction() //binds to library.h declaration
{
...
MyLibraryFunction(); //calls real library function through wrapper
}
This gives the desired behavior: when other.cpp is included in the
build during testing, the custom function is called which can then call
the real function if necessary; if other.cpp is excluded from the
build, the real function is called directly. This is accomplished
without any changes to the production code (main.cpp).