?
Solved

Override windows function in mixed c and c++ program

Posted on 2008-10-13
4
Medium Priority
?
411 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 2000 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

765 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