Hi Alin,

I didn’t know that I had to get file handle from _get_osfhandle. 
I have tried out the code, it works. I will ack the patch.

Thanks,
Anand Kumar

On 6/28/17, 10:30 AM, "Alin Serdean" <[email protected]> wrote:

    Thanks for the review Anand!
    
    
    
    There are a few issues with your snippet. You need to get the windows 
`handle` from the `FILE *` (_get_osfhandle), the return type of ` 
GetConsoleMode` is BOOL.
    
    
https://urldefense.proofpoint.com/v2/url?u=https-3A__msdn.microsoft.com_en-2Dus_library_windows_desktop_ms683167-28v-3Dvs.85-29.aspx&d=DwIGaQ&c=uilaK90D4TOVoH58JNXRgQ&r=Q5z9tBe-nAOpE7LIHSPV8uy5-437agMXvkeHHMkR8Us&m=icoV05_0sevqQRfuMDq67aESd35mY04D4mA-76nVCXU&s=UcA0XH2xX9T-r77TlYwtxdZiDN2KC4MADZYxFpEhWag&e=
 
    
    If you look at the return codes:
    
    " If the function succeeds, the return value is nonzero.
    
    
    
    If the function fails, the return value is zero. To get extended error 
information, call GetLastError."
    
    
    
    I made the following code snippet if you want to try it out.
    
    
    
    #include "stdafx.h"
    
    #include <Windows.h>
    
    #include <stdio.h>
    
    #include <io.h>
    
    
    
    int main()
    
    {
    
        FILE *fp;
    
        fopen_s(&fp, "c:\\test.txt", "w");
    
        if (fp) {
    
                fprintf(fp, "Testing...\n");
    
                DWORD st;
    
                HANDLE h = (HANDLE)_get_osfhandle(_fileno(fp));
    
                BOOL test = GetConsoleMode(h, &st);
    
                if (test) {
    
                        printf("fp is a console device\n");
    
                } else {
    
                        printf("fp is not a console device\n");
    
                }
    
                h = (HANDLE)_get_osfhandle(1);
    
                test = GetConsoleMode(h, &st);
    
                if (test) {
    
                        printf("stdout is a console device\n");
    
                }
    
                else {
    
                        printf("stdout is not a console device\n");
    
                }
    
        }
    
        return 1;
    
    }
    
    
    
    Feel free to redirect/or not to redirect the standard output when running 
the binary.
    
    
    
    Thanks,
    
    Alin.
    
    
    
    > -----Original Message-----
    
    > From: Anand Kumar [mailto:[email protected]]
    
    > Sent: Wednesday, June 28, 2017 8:06 PM
    
    > To: Alin Serdean <[email protected]>;
    
    > [email protected]
    
    > Subject: Re: [ovs-dev] [PATCH v2] Fix nonstandard isatty on Windows
    
    > 
    
    > Hi Alin,
    
    > 
    
    > Thanks for the patch. The patch looks good, I have one comment regarding
    
    > the GetConsoldeMode function.  I’m not sure if it works.
    
    > 
    
    > I have tried with the below snippet, where I create a new file but the
    
    > windows api returns saying it’s a console device.
    
    > int main()
    
    > {
    
    >     FILE *fp;
    
    >     fopen_s(&fp, "c:\\test.txt", "w");
    
    >     if (fp) {
    
    >         fprintf(fp, "Testing...\n");
    
    >         DWORD st;
    
    >         HANDLE h = (HANDLE)GetConsoleMode(fp, &st);
    
    >         if (!h) {
    
    >             cout << "fp is a console device" << endl;
    
    >         }
    
    >     }
    
    >     return 1;
    
    > }
    
    > 
    
    

_______________________________________________
dev mailing list
[email protected]
https://mail.openvswitch.org/mailman/listinfo/ovs-dev

Reply via email to