Thread overview
RedirectIOToConsole
May 28, 2008
steve
May 28, 2008
torhu
May 28, 2008
Extrawurst
May 28, 2008
Extrawurst
May 28, 2008
why does this code snippet refuse to work under D : http://www.halcyon.com/~ast/dload/guicon.htm ?

lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );

the last line wont give a result but null ? is there something to note when translating this to D ?

Thanks for any hints!
May 28, 2008
steve wrote:
> why does this code snippet refuse to work under D : http://www.halcyon.com/~ast/dload/guicon.htm ?
> 
> lStdHandle = (long)GetStdHandle(STD_OUTPUT_HANDLE);
> hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
> fp = _fdopen( hConHandle, "w" );
> 
> the last line wont give a result but null ? is there something to note when translating this to D ?
> 
> Thanks for any hints!

Thanks for the handy link.  The type long in D is 64 bits, so it won't be correct on a 32-bit system.  GetStdHandle's return type is HANDLE, so it's better to use that instead.

Something like this:

HANDLE lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
fp = _fdopen( hConHandle, "w" );

If that doesn't help, you need to post some more context, and maybe try to figure out on which line the error is.

Also make sure that GetStdHandle is declared with extern (Windows).  I'm not sure about _open_osfhandle, but msdn.com seems to say to it's extern (C).
May 28, 2008
torhu schrieb:
> HANDLE lStdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
> hConHandle = _open_osfhandle(lStdHandle, _O_TEXT);
> fp = _fdopen( hConHandle, "w" );

this wont work, either. i had the same problem. for a reason i dont understand _fdopen( or fdopen like it is called in D) always returns a null-pointer. in this case all u have to to is to create a _iobuf instance yourself and set the _flag property to 2 (for "w" access) and the _file property to hConHandle. thats pretty much everything that _fdopen does in this case and it works.
May 28, 2008
Extrawurst schrieb:
> this wont work, either. i had the same problem. for a reason i dont understand _fdopen( or fdopen like it is called in D) always returns a null-pointer. in this case all u have to to is to create a _iobuf instance yourself and set the _flag property to 2 (for "w" access) and the _file property to hConHandle. thats pretty much everything that _fdopen does in this case and it works.

here is the version that works for me ;)

http://paste.dprogramming.com/dpy9mgr9