Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Two functions, one name

0 views
Skip to first unread message

lotu...@yahoo.com

unread,
Apr 9, 2005, 6:49:37 PM4/9/05
to
Is there a way to link to two different functions with the same name
and signature?

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! ]

Ernst Murnleitner

unread,
Apr 10, 2005, 9:59:04 AM4/10/05
to
>
> //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;
> }
>

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

Kim, Seungtai

unread,
Apr 10, 2005, 3:14:14 PM4/10/05
to
"Ernst Murnleitner"

> > //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;
> > }
> >
>
> I think, you can use a namespace for this:
>
> namespace library {
> #include "library.h"
> }
>
> void LibraryFunction(string str)
> {
> library::LibraryFunction(str);
> cout << str << endl;
> }

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>

Vinzenz Feenstra

unread,
Apr 10, 2005, 3:16:03 PM4/10/05
to
> I think, you can use a namespace for this:
>
> namespace library {
> #include "library.h"
> }
>
> void LibraryFunction(string str)
> {
> library::LibraryFunction(str);
> cout << str << endl;
> }
>
This won't work.
This will cause a lot of compile errors and can't be linked correctly, imo

BR

Ernst Murnleitner

unread,
Apr 10, 2005, 6:40:46 PM4/10/05
to

>
> It dose not the solution. See the OP's constraint 1).
> It will make the unresolved linking error.
>

Yes, you are right. I thought about functions which are only in the
header files. I didn't think enough.


Greetings

Ernst

ka...@gabi-soft.fr

unread,
Apr 12, 2005, 4:52:48 PM4/12/05
to
lotu...@yahoo.com wrote:
> Is there a way to link to two different functions with the
> same name and signature?

> 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

Allan W

unread,
Apr 13, 2005, 2:41:18 AM4/13/05
to
lotu...@yahoo.com wrote:
> Is there a way to link to two different functions with the same name
> and signature?

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.

Allan W

unread,
Apr 13, 2005, 2:42:50 AM4/13/05
to
lotu...@yahoo.com wrote:
> Is there a way to link to two different functions with the same name
> and signature?

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).

Eugene Gershnik

unread,
Apr 13, 2005, 2:53:34 AM4/13/05
to
If you are on Windows check
http://research.microsoft.com/sn/detours/

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

lotu...@yahoo.com

unread,
Apr 13, 2005, 4:03:20 PM4/13/05
to
Thanks everyone,
I wrapped all the library functions with differently named
functions in a DLL.
i.e.
//Library.h
void LibraryFunction();

//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).

0 new messages