Using stdin/out in a windows application bugs only when compiled to 64bit.

Steven Schveighoffer schveiguy at yahoo.com
Thu Jun 7 19:42:05 UTC 2018


On 6/7/18 3:19 PM, realhet wrote:
> Hi,
> 
> The following narrow test program works fine when compiled with DMD to 
> 32bit target:
> 
> import std.stdio, core.sys.windows.windows, core.runtime;
> extern(Windows) int WinMain(HINSTANCE hInstance, HINSTANCE 
> hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
> {
>      Runtime.initialize;
>      writeln("Hello");
>      stdout.flush; //exception
>          readln; //exception
>      return 0;
> }
> 
> It shows the console window and waits for user input at readln.
> 
> If I try to compile this to 64bit target or with LDC (both 32 and 64) it 
> gets privileged instruction exception at stdout.flush. If I comment out 
> flush then it fails at readln.
> 
> Is there a way to fix this? I don't wanna lose the console window even 
> on 64bit.
> 
> Thanks!
> 

Are you just compiling the 32-bit dmd version with default flags?

If so, it's likely an issue with MSVC runtime library not being properly 
set up. DMD 32 bit by default uses DMC runtime.

To verify, use the switch -m32mscoff to do 32-bit result but link 
against MSVC library.

I know this isn't really an answer, but it at least narrows it down. 
Been a while since I did windows development, but it sounds like you are 
trying to print to or read from a console that isn't open. A windows 
program that has gui-only flag doesn't by default allocate a console. 
Most likely dmc just does it for you.

-Steve


More information about the Digitalmars-d-learn mailing list