www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - RedirectIOToConsole

reply steve <steve steve.com> writes:
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
parent reply torhu <no spam.invalid> writes:
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
parent reply Extrawurst <spam extrawurst.org> writes:
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
parent Extrawurst <spam extrawurst.org> writes:
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
May 28 2008