Comment #0 by alphaglosined — 2015-03-22T13:06:35Z
Compiler: dmd 2.067-rc1 but present in 2.066.1, I expect it to be present in basically every D compiler as it is an edge case.
Basically (haven't tested on posix ext.) on Windows if you load a D shared library it will have not loaded stdin/stdout/stderr.
Here is my "fix" for it. Really if the host binary is also D it should auto set these values. Otherwise this should be the default behaviour.
void fixStandardIO() {
import std.stdio: stdin, stdout, stderr, File;
// Loads up new instances of stdin/stdout/stderr if they have not been properly created
if (stdin.error || stdout.error || stderr.error) {
version(Windows) {
import core.sys.windows.windows: GetStdHandle, STD_INPUT_HANDLE, STD_OUTPUT_HANDLE, STD_ERROR_HANDLE;
File nstdin;
nstdin.windowsHandleOpen(GetStdHandle(STD_INPUT_HANDLE), ['r']);
stdin = nstdin;
File nstdout;
nstdout.windowsHandleOpen(GetStdHandle(STD_OUTPUT_HANDLE), ['w']);
stdout = nstdout;
File nstderr;
nstderr.windowsHandleOpen(GetStdHandle(STD_ERROR_HANDLE), ['w']);
stderr = nstderr;
}
}
}
Comment #1 by dlang-bugzilla — 2015-03-30T19:27:03Z
Possibly related to issue 14327?
Comment #2 by toop — 2016-03-17T21:48:17Z
I get error: privileged instruction at windowsHandleOpen line because there is no console.
if (!AttachConsole(ATTACH_PARENT_PROCESS))
{
AllocConsole();
}
After this there is still no output from writeln, writefln and printf in my app calling DLL and in DLL itself but writefln no longer fails with std.exception.ErrnoException (Bad file descriptor). flush() doesn't do anything but writeln/fln works after you call stdout.setvbuf(1, _IONBF); with any value above 0. printf doesn't work at all.
Comment #3 by toop — 2016-03-22T13:54:23Z
(In reply to toop from comment #2)
> I get error: privileged instruction at windowsHandleOpen line because there
> is no console.
>
> if (!AttachConsole(ATTACH_PARENT_PROCESS))
> {
> AllocConsole();
> }
>
> After this there is still no output from writeln, writefln and printf in my
> app calling DLL and in DLL itself but writefln no longer fails with
> std.exception.ErrnoException (Bad file descriptor). flush() doesn't do
> anything but writeln/fln works after you call stdout.setvbuf(1, _IONBF);
> with any value above 0. printf doesn't work at all.
Sorry, it turned out to be my fault. dll_process_attach( hInstance, true ); call was missing in the dll and it caused all sorts of problems.