Solved

Override windows function in mixed c and c++ program

Posted on 2008-10-13
4
404 Views
Last Modified: 2013-12-14
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
0
Comment
Question by:atomicgs12
4 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 22709340
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

?
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 22709359
Hi atomicgs12,

I don't know if I really understand, but at least I think this is wrong:

#define my_RegCreateEx RegCreateEx

I would think you need something like this, best in the header where my_RegCreateEx is declared:

// after declaration of my_RegCreateEx
#undef RegCreateEx
#define RegCreateEx my_RegCreateEx

Hope that helps,

if not please post more of the code, including the declaration of my_RegCreateEx.

ZOPPO
0
 
LVL 5

Expert Comment

by:rendaduiyan
ID: 22709362
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.
0
 
LVL 8

Expert Comment

by:Pramod Bugudai
ID: 22783533
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

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question