replacing function name

Sep 21, 2021 at 9:16pm
Hi all -

This question is equally applicable to C and C++, I believe.

I'm working on porting an application. I'd like to replace some function names without actually changing the code, so when the compiler see something like this:

OLD_COMPANY_function(a, b, c);

it will instead call:

NEW_COMPANY_function(a, b, c);

which I have defined elsewhere.

I tried a #define for this, but that didn't work. Any creative ideas?

Thanks...
Sep 21, 2021 at 9:38pm
So you want to redirect the call while leaving the old function intact? I don't think there's any way to do that with macros and without changing the code.
It may be possible with hooking, but I really don't recommend that.
Sep 21, 2021 at 9:47pm
Yeah, I had a feeling...thanks.
Sep 21, 2021 at 10:23pm
#define Token pasting comes to mind.

However this would would involve recoding the OLD_COMPANY function calls slightly, which I suspect is something your don't want to do.

Also, this will only work for C functions since C++ function names are mangled.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

void NEW_COMPANY_function(int a, int b)
{
	std::cout << "NEW_COMPANY_function called" << std::endl;
}

//  Token pasting macro
#define OLD_COMPANY(func) NEW_COMPANY##func 

int main()
{
	OLD_COMPANY(_function) (1, 2);
}
NEW_COMPANY_function called


edit: Simplified the example
I previously said this would not work with C++ names, but I believe it would since the token pasting is done at preprocessor time.
Last edited on Sep 22, 2021 at 1:23am
Sep 21, 2021 at 10:39pm
you can do it several ways.
1) function pointer.
2) function overload.
3) macro
4) wrapper (eg foo calls bar, and does nothing else)
probably a few more as well... surely some new tricks too, auto and function name (is it just a function pointer, though?) or maybe a reference with some voodoo syntax?
Last edited on Sep 21, 2021 at 10:41pm
Sep 22, 2021 at 12:26am
random guy who doesnt know c++ here, I think you can just redefine old_company by just going

void new_company(blah blah blah) {

old_company_function()

}
Sep 22, 2021 at 12:45am
yea that was the 'wrapper' idea :)
but you are totally correct.
Last edited on Sep 22, 2021 at 12:46am
Topic archived. No new replies allowed.