setmode again

Brian Myers bmyers at harryanddavid.com
Fri Aug 8 12:37:31 PDT 2008


No, I haven't because I thought that would result in a compiler or linker error. What happens is the function is executed at run time, but has no effect. I think I have a better idea now thought:

Check out this C code:

#include <fcntl.h>
#include <io.h>
#include <string.h>
#include <stdio.h>

void main() {
    char *tmp = "Test line1\r\ntest line2\n";
    _setmode(_fileno(stdout), _O_BINARY);
    fwrite(tmp, strlen(tmp), sizeof(char), stdout);
}

When compiled and linked with MSVC, it works properly. When compiled with DMC, it doesn't. Could there be something wrong with the DMC CRT? I would suspect the D runtime library is built on the same code, and that's why it's not working properly.

OTOH, then why didn't it work properly when I loaded the _setmode function out of MSVCRT directly? I'm sure that's what the python code is doing. Well, pretty sure anyway.

Errrrgh! This is one of the many reasons I hate M$.

Steven Schveighoffer Wrote:

> 
> "Brian Myers" wrote
> > Ugh,
> >
> > By the rush to respond I'm getting so far, I'm guessing nobody does these 
> > kind of programs these days. Can't say I'm surprised, but I'm stuck 
> > working with legacy data.
> >
> > Anyway, for everyone's reference, the following python code works 
> > perfectly:
> >
> > import os, msvcrt
> > import sys
> >
> > msvcrt.setmode(sys.stdout.fileno(), os.O_BINARY)
> > print "test line1\r\ntestline2"
> >
> > Brian Myers Wrote:
> >
> >> Hi all,
> >>
> >> Seems this gets revisited every so often on the forums, so I guess it's 
> >> time to visit it again...
> >>
> >> I need to set stdout to binary mode in Windows. I've tried the following:
> >>
> >> import std.string;
> >> import std.stdio;
> >> import std.c.windows.windows;
> >>
> >> extern (C)
> >> alias int function(int,int) setmode_f;
> >>
> >> void main(char[][] args)
> >> {
> >>     int O_BINARY = 0x8000;
> >>     setmode_f f;
> >>     HMODULE m = cast(HMODULE) LoadLibraryA(toStringz("msvcrt.dll"));
> >>     f = cast(setmode_f) GetProcAddress(m, toStringz("_setmode"));
> >>     f(fileno(stdout), O_BINARY);
> >>
> >>     writef("test line1\r\ntestline2");
> >> }
> >>
> >> and
> >>
> >> import std.string;
> >> import std.stdio;
> >>
> >> extern (C)
> >> alias int function(int,int) setmode_f;
> >>
> >> extern ( C ) int setmode(int,int);
> >>
> >> void main(char[][] args)
> >> {
> >>     int O_BINARY = 0x8000;
> >>
> >>     if (setmode(fileno(stdout) ,O_BINARY) < 0)
> >>         writefln("setmode failed.");
> >>     writef("test line1\r\ntest line2");
> >> }
> >>
> >> No luck. Both compile and run without error, but Windows is still doing 
> >> LF->CRLF translation.
> >>
> >> I know I need a version statement for a cross platform compile, but this 
> >> is just example code.
> >>
> >> Brian
> >>
> >
> 
> Have you tried changing extern(C) to extern(Windows)?
> 
> -Steve 
> 
> 




More information about the Digitalmars-d mailing list