Link to home
Start Free TrialLog in
Avatar of atomicgs12
atomicgs12Flag for United States of America

asked on

Override windows function in mixed c and c++ program

I have an application that has both c and c++ files. I have written some functions that I want to over ride some windows functions, namely Registry calls. For example i have a function called my_RegCreateEx and what I want is that when my code calls RegCreateEx I want the call to go to my_RegCreateEx and not the windows default RegCreateEx found in winreg.h.

I have created my_RegCreateEx() function call in a cpp file and used the extern "c" in the my header file, call it my_reg.h. Also in another header file, call it my_functions.h I have a defined as follows:
#define my_RegCreateEx RegCreateEx. Now in any cpp file that I have:
#include "my_functions.h"
#include "my_reg.h"
where I have a call to RegCreateEx() the code will go to my function my_RegCreateEx() as expected but in any C files where I have a RegCreateEx() call it goes to the windows function, which I do not want.

I need help where no matter what type of file, C or C++, the RegCreateEx call will always go to my_RegCreateEx call.

Thanks for you help
Avatar of Infinity08
Infinity08
Flag of Belgium image

Can you show the exact header files you are using ?

Didn't you also mean :

        #define RegCreateEx my_RegCreateEx

instead of :

        #define my_RegCreateEx RegCreateEx

?
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
you need to define a new function as RegCreateExA
//include both windows header file and your header file.
#include ....

#ifdef OWN_REGCREATE
#define RegCreateExA my_RegCreateEx
#else
#define RegCreateExA RegCreateEx
#endif

In your codes, call RegCreateExA directly and do not care about which one is called.
Hi atomicgs12,
                            this is one general idea that whenever you are using some c language function with some C++ type function you need to define like this:

#ifdef __cplusplus
extern "C"
{
#endif
           //C type function
           int add(int,int);
           int multiply(int,int);
#ifdef __cplusplus
};
#endif