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://msdn.microsoft.com/en-us/library/windows/desktop/ms683167(v=vs.85).aspx
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