Replace fopen/fwrite

Posted on 2007-08-06
Last Modified: 2008-02-20
I would like to insert my own code each time a third party application writes to the disk. I have the source code and I thought the easiest way to do this is simply to replace fopen/fwrite with a function of my own that internally calls the real fopen/fwrite. How can I do this?

Question by:Sandra-24
    LVL 14

    Expert Comment

    by:Farzad Akbarnejad
    I think thaty you must write a resident in memory program that listen to int 13h for writing and reading disk.

    LVL 8

    Expert Comment

    Do you have the source code of the third party's fopen/fwrite? If you do, have you tried to call the ::fopen and ::fwrite functions?  If you just rename their fopen/fwrite functions so that their names are slightly different, then the next time you build the third party library it should pull in the standard c-runtime library version of fopen/fwrite.
    LVL 9

    Expert Comment

    An alternative might be to use something like the .NET FileSystemWatcher class - this raises events when specified I/O events take place (like changes to a particular directory or file).   There is a Win32 equivalent - ReadDirectoryChanges - but this is probably harder to use than the .NET class.
    LVL 22

    Accepted Solution


    int My_fwrite( void * Buf, size_t Len, size_t Items, FILE * f ) {
    // do whatever you want then:
       return  fwrite( Buf, Len, Items, f );

    #define fopen  My_fopen
    #define fclose  My_fclose
    #define fread   My_fread
    #define fwrite  My_fwrite

    LVL 3

    Author Comment

    Thanks grg99, that's exactly what I wanted.

    LVL 22

    Expert Comment

    Note that this trick will work but only for sources you compile with this stuff included.

    It won't change any fwrite's being done in linked in libraries.

    Ands it won't trap any other kinds of writes, like with write()  and the other variations.


    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    Join & Write a Comment

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
    The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
    The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

    731 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

    15 Experts available now in Live!

    Get 1:1 Help Now