Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. FOPEN Redirection

FOPEN Redirection

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestionhardwaretutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I've got a piece of software written by someone in my department years ago. (IE, I have no access to the source nor do I have a developer I can contact.) The problem is that this piece of software outputs information to a log.txt file that I would like to read in real time (in a console window) because when the program crashes the file stream gets closed before it can write its buffer to the actual file leaving me with 0kb log files, awesome. My initial thought on how to do this is to first inject my own .dll in to the program (using InjLib). I could then use my own .dll to redirect any calls to CreateFile where the filename is log.txt to a new file stream. I thought at first I could just pass back a pointer to stdout, this obviously resulted in whatever was going to log.txt to write to the console window that the program already has open. This might work if the program wasn't already dumping alot of info. to that console. So I thought maybe I could create a second console window. Knowing that each .exe file can only have 1 open console I searched around code project and found a multiple console opening program that uses pipes. Here is where I am stuck. I know that fprintf writes to a buffer. The only way my 2nd console gets updated is when the original program closes the file stream. I've tried created a pipe with no buffer and hardware write through but to no avail. I guess my question is: Is there a simpler way of redirecting a file stream to console window? Or, if not, is there some way for me to read from the buffer fprintf uses? I've tried using PeekPipe on my pipe but it doesn't see anything. I'm not exactly sure what code snippets would help in my explanation.

    C B J 3 Replies Last reply
    0
    • L Lost User

      I've got a piece of software written by someone in my department years ago. (IE, I have no access to the source nor do I have a developer I can contact.) The problem is that this piece of software outputs information to a log.txt file that I would like to read in real time (in a console window) because when the program crashes the file stream gets closed before it can write its buffer to the actual file leaving me with 0kb log files, awesome. My initial thought on how to do this is to first inject my own .dll in to the program (using InjLib). I could then use my own .dll to redirect any calls to CreateFile where the filename is log.txt to a new file stream. I thought at first I could just pass back a pointer to stdout, this obviously resulted in whatever was going to log.txt to write to the console window that the program already has open. This might work if the program wasn't already dumping alot of info. to that console. So I thought maybe I could create a second console window. Knowing that each .exe file can only have 1 open console I searched around code project and found a multiple console opening program that uses pipes. Here is where I am stuck. I know that fprintf writes to a buffer. The only way my 2nd console gets updated is when the original program closes the file stream. I've tried created a pipe with no buffer and hardware write through but to no avail. I guess my question is: Is there a simpler way of redirecting a file stream to console window? Or, if not, is there some way for me to read from the buffer fprintf uses? I've tried using PeekPipe on my pipe but it doesn't see anything. I'm not exactly sure what code snippets would help in my explanation.

      C Offline
      C Offline
      Chris Meech
      wrote on last edited by
      #2

      How about trying to trap the file write operations and instead of redirecting them to a console window, calling flush to force the buffer to the file. :)

      Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

      L 1 Reply Last reply
      0
      • C Chris Meech

        How about trying to trap the file write operations and instead of redirecting them to a console window, calling flush to force the buffer to the file. :)

        Chris Meech I am Canadian. [heard in a local bar] Donate to help Conquer Cancer[^]

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #3

        That's an idea but real-time access to the debugging information would be more helpful.

        1 Reply Last reply
        0
        • L Lost User

          I've got a piece of software written by someone in my department years ago. (IE, I have no access to the source nor do I have a developer I can contact.) The problem is that this piece of software outputs information to a log.txt file that I would like to read in real time (in a console window) because when the program crashes the file stream gets closed before it can write its buffer to the actual file leaving me with 0kb log files, awesome. My initial thought on how to do this is to first inject my own .dll in to the program (using InjLib). I could then use my own .dll to redirect any calls to CreateFile where the filename is log.txt to a new file stream. I thought at first I could just pass back a pointer to stdout, this obviously resulted in whatever was going to log.txt to write to the console window that the program already has open. This might work if the program wasn't already dumping alot of info. to that console. So I thought maybe I could create a second console window. Knowing that each .exe file can only have 1 open console I searched around code project and found a multiple console opening program that uses pipes. Here is where I am stuck. I know that fprintf writes to a buffer. The only way my 2nd console gets updated is when the original program closes the file stream. I've tried created a pipe with no buffer and hardware write through but to no avail. I guess my question is: Is there a simpler way of redirecting a file stream to console window? Or, if not, is there some way for me to read from the buffer fprintf uses? I've tried using PeekPipe on my pipe but it doesn't see anything. I'm not exactly sure what code snippets would help in my explanation.

          B Offline
          B Offline
          Bram van Kampen
          wrote on last edited by
          #4

          Tyfane wrote:

          when the program crashes

          I detect a sense of inevitibility here. Sounds as if you have a tired piece of buggy software here. And you have no source code. Far be it from me to tell you what to do, but speaking as one who has struggled in the past with such things, I would say that if I were you I'd byte the bullet and write a spanking new version in windows, using the latest ideas on the particular issues the software is supposed to address. That approach is probably less time consuming, and will probably give you more brownie points with management, not in the least because your name will be on the new version. Regards, and may the Power be with you! (You'l Need it Eitherway) :)

          Bram van Kampen

          1 Reply Last reply
          0
          • L Lost User

            I've got a piece of software written by someone in my department years ago. (IE, I have no access to the source nor do I have a developer I can contact.) The problem is that this piece of software outputs information to a log.txt file that I would like to read in real time (in a console window) because when the program crashes the file stream gets closed before it can write its buffer to the actual file leaving me with 0kb log files, awesome. My initial thought on how to do this is to first inject my own .dll in to the program (using InjLib). I could then use my own .dll to redirect any calls to CreateFile where the filename is log.txt to a new file stream. I thought at first I could just pass back a pointer to stdout, this obviously resulted in whatever was going to log.txt to write to the console window that the program already has open. This might work if the program wasn't already dumping alot of info. to that console. So I thought maybe I could create a second console window. Knowing that each .exe file can only have 1 open console I searched around code project and found a multiple console opening program that uses pipes. Here is where I am stuck. I know that fprintf writes to a buffer. The only way my 2nd console gets updated is when the original program closes the file stream. I've tried created a pipe with no buffer and hardware write through but to no avail. I guess my question is: Is there a simpler way of redirecting a file stream to console window? Or, if not, is there some way for me to read from the buffer fprintf uses? I've tried using PeekPipe on my pipe but it doesn't see anything. I'm not exactly sure what code snippets would help in my explanation.

            J Offline
            J Offline
            Jude Deng
            wrote on last edited by
            #5

            The function 'freopen' is about redirecting. But I don`t know whether it can help you; because I don`t know that [1]the detail of your method of to open second console window and [2]how to get handle of file stream of log.txt in your .dll. Could you detailedly describe you method which how to open second Console window and how to get handle of file stream???

            L 1 Reply Last reply
            0
            • J Jude Deng

              The function 'freopen' is about redirecting. But I don`t know whether it can help you; because I don`t know that [1]the detail of your method of to open second console window and [2]how to get handle of file stream of log.txt in your .dll. Could you detailedly describe you method which how to open second Console window and how to get handle of file stream???

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              @Bram: :) Believe me the idea of rewriting this pile of garbage has been bandied about for almost a year now. The major hold up, as always, is cost and time. I can spare a month or 2 to test some theories to see if I can shore up some of the minor glitches. Barring that we'd have to hire a contractor to finish the product, our own programming staff is swamped. @deng0jun: [1] The multiple consoles concept comes directly from: http://www.codeproject.com/cpp/MultipleConsoles.asp [2] Getting a handle to the file uses the InjLib for .dll injection and a method similar to this gentleman's article: http://www.codeproject.com/system/Paladin.asp 1. Program Foo runs 2. I use InjDemo.exe to insert HookApi.dll in to Foo. 3. HookApi loads MyDll.dll to overload CreateFile 4. When CreateFile is called I check the incoming name for "log.txt", when found I return a pointer to my pipe for multiple consoles. Even if I simplify the problem and remove multiple consoles I have a problem dumping whatever data is stored by Foo to a console window. The dump happens if Foo calls flush which apparently it does for some errors (this is only an assumption based on the log sometimes containing data). After taking a second look at my problem it now seems to me that I somehow need access whatever buffer is created by WriteFile... perhaps a custom StreamBuf? Maybe what I'm trying to do is impossible... I have no idea.

              J 1 Reply Last reply
              0
              • L Lost User

                @Bram: :) Believe me the idea of rewriting this pile of garbage has been bandied about for almost a year now. The major hold up, as always, is cost and time. I can spare a month or 2 to test some theories to see if I can shore up some of the minor glitches. Barring that we'd have to hire a contractor to finish the product, our own programming staff is swamped. @deng0jun: [1] The multiple consoles concept comes directly from: http://www.codeproject.com/cpp/MultipleConsoles.asp [2] Getting a handle to the file uses the InjLib for .dll injection and a method similar to this gentleman's article: http://www.codeproject.com/system/Paladin.asp 1. Program Foo runs 2. I use InjDemo.exe to insert HookApi.dll in to Foo. 3. HookApi loads MyDll.dll to overload CreateFile 4. When CreateFile is called I check the incoming name for "log.txt", when found I return a pointer to my pipe for multiple consoles. Even if I simplify the problem and remove multiple consoles I have a problem dumping whatever data is stored by Foo to a console window. The dump happens if Foo calls flush which apparently it does for some errors (this is only an assumption based on the log sometimes containing data). After taking a second look at my problem it now seems to me that I somehow need access whatever buffer is created by WriteFile... perhaps a custom StreamBuf? Maybe what I'm trying to do is impossible... I have no idea.

                J Offline
                J Offline
                Jude Deng
                wrote on last edited by
                #7

                Dear Tyfane, Now I have a ideal which may solve your problem; perhaps it`s too late. My ideal is that you hook CreateFile and WriteFile at the same time. In this way, you can get the HANDLE of log.txt firstly; then monitor the WriteFille. My means is following: BOOL MyWriteFile(HANDLE hFile, ......) { BOOL res = OriginalWriteFile(HANDLE hFile, ......); if(hFile belong to Log.txt) { **FlushFileBuffers**(hFile);//you can get information from log.txt! } return res; } If writing file is not the cause which result in to crash, I think it is a way to solve your problem. Good Luck! JuneDeng

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups