SetStdHandle

This is a discussion on SetStdHandle within the Windows Programming forums, part of the Platform Specific Boards category; I searched the board for redirecting cout and all the topic said was it can be done using SetStdHandle. Well ...

  1. #1
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630

    SetStdHandle

    I searched the board for redirecting cout and all the topic said was it can be done using SetStdHandle. Well what would you pass to it for the HANDLE param? The buffer of a ofstream file? some could would be nice Basically what im trying to do is get output from a library that outputs to STD_OUTPUT_HANDLE through cout, and since i dont have access to the librarys code i just want to switch the handles so i can get the output. Thanx in advance for some help.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Not sure about STL, but this would probably work:

    FILE * file = fopen("r.txt", "w");

    if(file)SetStdHandle( STD_OUTPUT_HANDLE, file );



    ITSA
    Socket Library!

  3. #3
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Before I try im going to ask, all I would have to do is read in the file after that and anything that was output to cout would be in the file?

    [edit]
    What part of Dallas are you in? I have some family out there, my sister was born there, I was born in Lubbock but only lived there 9 months

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Before I try im going to ask, all I would have to do is read in the file after that and anything that was output to cout would be in the file?
    ...read in the file? No, just cout to it.

    I live about 15 minutes east of downtown. And your family? Where do you live, Atlanta, right?



    ITSA
    Socket Library!

  5. #5
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    lol no im saying that after the using the library that uses cout, and having redirectd it. I would just read the file and all the info put to cout would be in it?

    And yes im in atlanta now, california after tx, here in '89, hopefully to co soon :-p

  6. #6
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Honestly, I've never tried it, but I'm 90% sure.



    ITSA
    Socket Library!

  7. #7
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    didnt work id post the code but its not mine its my teachers and the lib is his im just messing around with it. I can put up a zip with the code and lib in it.

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Hold on, lemme do a little research here...



    ITSA
    Socket Library!

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    I found something that sort of works. Heres an example:
    Code:
    ofstream outfile;
    outfile.open("stdout.txt", ios::out);
    streambuf* psbuf = outfile.rdbuf();
    cout.rdbuf(psbuf);
    
    cout <<"This goes to a file!";
    but it doesnt catch the calls the library makes to cout only the file that uses the librarys output is caught.

  10. #10
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Ok. Try this:

    Code:
    
    HANDLE old_handle = GetStdHandle(STD_OUPUT_HANDLE);
    
    HANDLE redirect = CreateFile(
     "r.txt",
     GENERIC_WRITE | GENERIC_READ,
     FILE_SHARE_READ,
     NULL,
     OPEN_ALWAYS,
     FILE_ATTRIBUTE_NORMAL,
     FILE_FLAG_WRITE_THROUGH);
    if(redirect) SetStdHandle(STD_OUPUT_HANDLE, redirect);
    
    //...
    
    CloseHandle(redirect);
    
    SetStdHandle(STD_OUPUT_HANDLE, old_handle);



    ITSA
    Socket Library!

  11. #11
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Sebastiani, thanks for that snipet but it still doesnt work. The above method i posted works but it doesnt get me the output from the library that i need.

  12. #12
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Hello anyone? I can redirect cout but I cant catch anything that the lib outputs using cout.

  13. #13
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Havent tested it with external libs, but this might work

    Code:
    #include <iostream>
    #include <cstdio>
    
    int main()
    {
        std::FILE* fptr = freopen("myfile.txt","w",stdout);
        if(!fptr){
        	std::cout << "Could not open file";
        	return 1;
        }    
        
        std::cout << "Hello World - sent to file"; 
     	
     	freopen("CON","w",stdout);    
       
        std::cout << "Hello World - sent to console";    
       
    }

  14. #14
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Fordy, once again you prove to be da man! But one problem now. When i run my teachers prog which uses his lib in a console program it outputs everything fine. When i just tried doing what you said it output all the output from the prog, then the output from the lib at the end of the file.

    [EDIT]
    Just wondering, is there a way to just have it write the output to a buffer and not a file? I dont want to have to read in the file in to acomplish what I need to do.
    Last edited by xds4lx; 11-23-2002 at 06:08 PM.

  15. #15
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    Ran into a problem now When I put the code you gave into the DLL which holds the model it doenst write to the file.

    [Edit]
    Fixed it all with a call to cout.sync_with_stdio() (apparently my teacher used must of used iostream.h to compile his lib
    Last edited by xds4lx; 11-23-2002 at 08:28 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21