Solved

Override windows function in mixed c and c++ program

Posted on 2008-10-13
4
393 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 30

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
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 member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

706 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now