Overriding function and calling the overriden functions 
Author Message
 Overriding function and calling the overriden functions

Does anyone have any idea how I could over-ride a function, such as fseek,
but then call the original fseek function in the new fseek function I have
writen?
Unfortunately, the easy way would be to go through the code I have writen
and change all calls to fseek to something else and call my new function
that, but for reason I can't go into I can't so I need to do what is
described above.
Can anyone help?

Andrew



Tue, 01 Apr 2003 03:00:00 GMT  
 Overriding function and calling the overriden functions


Quote:
> Does anyone have any idea how I could over-ride a function, such
> as fseek, but then call the original fseek function in the new fseek
> function I have writen?
> Unfortunately, the easy way would be to go through the code I have
> writen and change all calls to fseek to something else and call my
> new function that, but for reason I can't go into I can't so I need
> to do what is described above.
> Can anyone help?

C doesn't allow you to dynamically alter functions. However there are
horrible ways of doing it which will work on some machines.

Using the identifer _fseek_ you get the address of the function. Almsot
certainly this just the location the code jumps to when you call the
function. With a spot of assembly, over-write that location with a JUMP
myfunction. This will take a few bytes - store them somewhere.

In function myfunction, patch the bytes you have overwritten back into
fseek(), and call the function in the normal way. Then put it back to
your indirected version.

I cannot promise that this will work - modern machines often have
address protection or dynamic libraries it is hard to circumvent.

Sent via Deja.com http://www.deja.com/
Before you buy.



Tue, 01 Apr 2003 03:00:00 GMT  
 Overriding function and calling the overriden functions


Quote:
>Does anyone have any idea how I could over-ride a function, such as fseek,
>but then call the original fseek function in the new fseek function I have
>writen?
>Unfortunately, the easy way would be to go through the code I have writen
>and change all calls to fseek to something else and call my new function
>that, but for reason I can't go into I can't so I need to do what is
>described above.
>Can anyone help?

There are probably many ways to solve this; here's one

In the compilation unit that you wish your replacement function to be
called from, #define the function with a replacement name, i.e.
  #undefine fseek
  #define fseek MyReplacementFseek
and recompile this compilation unit

In a seperate compilation unit, write the code that implements your
replacement function, but do not 'redefine' the base function. I.e.
  int MyReplacementFseek(....)
  {
     ...
     fseek(...)
     ...
  }

You then link the object results of the two compilation units
together, and you have effectively replaced the core function.

Caveat: I haven't done this, and it may not work in your environment.
It does depend on how compilation/linking/execution is performed in
your environment, which is something off-topic for this forum.

Lew Pitcher
Information Technology Consultant
Toronto Dominion Bank Financial Group


(Opinions expressed are my own, not my employer's.)



Tue, 01 Apr 2003 03:00:00 GMT  
 Overriding function and calling the overriden functions

Quote:
>In the compilation unit that you wish your replacement function to be
>called from, #define the function with a replacement name, i.e.
>  #undefine fseek
>  #define fseek MyReplacementFseek
>and recompile this compilation unit
>In the compilation unit that you wish your replacement function to be
>called from, #define the function with a replacement name, i.e.
>  #undefine fseek
>  #define fseek MyReplacementFseek
>and recompile this compilation unit

Why so complicated?

for i in (list of c-files)  
   do
          sed -e 's/fseek/Myfseek/g' $i > tmp$$
          mv tmp$$ $i
   done # Not testet!

on all c-files except the one, where Myfseek is defined. All other stuff
is simply obfuscating and unnessecary. But to be honest, i dont't
understand the problem here. What hinders the OP to directly name his
own function Myfseek (instead of driving from New Jersey to NY via
Orlando)?

NoKo

"Close To The Edge"



Tue, 01 Apr 2003 03:00:00 GMT  
 Overriding function and calling the overriden functions

Quote:
>>In the compilation unit that you wish your replacement function to be
>>called from, #define the function with a replacement name, i.e.
>>  #undefine fseek
>>  #define fseek MyReplacementFseek
>>and recompile this compilation unit

>>In the compilation unit that you wish your replacement function to be
>>called from, #define the function with a replacement name, i.e.
>>  #undefine fseek
>>  #define fseek MyReplacementFseek
>>and recompile this compilation unit

>Why so complicated?

<snip some sort of unix script>

Now do this again in a portable way....

--
Mark McIntyre
C- FAQ: http://www.eskimo.com/~scs/C-faq/top.html



Wed, 02 Apr 2003 09:03:26 GMT  
 Overriding function and calling the overriden functions

wrote in comp.lang.c:

Quote:


> > Does anyone have any idea how I could over-ride a function, such
> > as fseek, but then call the original fseek function in the new fseek
> > function I have writen?
> > Unfortunately, the easy way would be to go through the code I have
> > writen and change all calls to fseek to something else and call my
> > new function that, but for reason I can't go into I can't so I need
> > to do what is described above.
> > Can anyone help?

> C doesn't allow you to dynamically alter functions. However there are
> horrible ways of doing it which will work on some machines.

> Using the identifer _fseek_ you get the address of the function. Almsot

Just because the symbol is translated to "_fseek_" on your compiler,
what makes you so sure that Andrew's compiler does the same?

Jack Klein
--
Home: http://jackklein.home.att.net



Wed, 02 Apr 2003 11:34:02 GMT  
 Overriding function and calling the overriden functions

Quote:

> Does anyone have any idea how I could over-ride a function, such as fseek,
> but then call the original fseek function in the new fseek function I have
> writen?

You can't over-ride a function in Standard C.

Quote:
> Unfortunately, the easy way would be to go through the code I have writen
> and change all calls to fseek to something else and call my new function
> that, but for reason I can't go into I can't so I need to do what is
> described above.

Do you mean you have a function called fseek() that you defined? If
so your invoking undefined behavor.  Change the name of your function
and then use some preprocesser tricks.

Quote:
> Can anyone help?

#include <stdio.h>
#undef fseek  /* some compiles may #define fseek */
#define fseek andrew_fseek

int andrew_fseek(FILE *stream, long offset, int where)
{
    int result;

    /* ... */
    /* I use ()'s to prevent the preprocesser from replacing it,
     * but you could also use a function pointer poiting to fseek
     * before the #undef and #define above and call it here or
     * #undef fseek, then call fseek then #define fseek, as above
     * again. - DarkCode
     */
    result = (fseek)(stream, offset, where);
    /* ... */
    return result;

Quote:
}
> Andrew

--
struct DarkCode;
// "Note that this code is untested and I'm rather tired, so
// there's probably a bug." -- Ben Pfaff


Thu, 03 Apr 2003 03:00:00 GMT  
 Overriding function and calling the overriden functions

Quote:

> Does anyone have any idea how I could over-ride a function, such as fseek,
> but then call the original fseek function in the new fseek function I have
> writen?
> Unfortunately, the easy way would be to go through the code I have writen
> and change all calls to fseek to something else and call my new function
> that, but for reason I can't go into I can't so I need to do what is
> described above.
> Can anyone help?

> Andrew

Are you trying to replace the standard fseek with one of your own, which
in turn calls the standard function?

If you are doing this on a UNIX platform, and that platform supports
dlopen(), you could try using dlopen() on libc followed by a call to
dlsym()
to get the address in libc for the standard fseek. Use this address to
call
the standard fseek. I have not tried this, it's just a suggestion.

Certainly not a portable, or standard, solution.

--
-----------------------------------------------------------
Nigel Wade, System Administrator, Space Plasma Physics Group,
            University of Leicester, Leicester, LE1 7RH, UK

Phone :     +44 (0)116 2523568, Fax : +44 (0)116 2523555



Fri, 04 Apr 2003 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. overriding virtual function return type or calling convention error

2. Call a function in a function with name of a function as parameter

3. Condition breakpoints and function calls and function calls in Immediate window

4. calling overriden method not possible?

5. Call C++ member function from C function ?

6. Question : A generic function to retrieve the call parameters of a function

7. Calling C++ Functions from C functions under HP and Linux

8. C function calling function in C++ library

9. function that call function

10. One Problem in Calling Static function from Non static function

11. Declaration of functions called via function pointers

12. Validity of simplified notation for calling functions from function pointers

 

 
Powered by phpBB® Forum Software