istream::good() in a dll

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andreas Hildebrandt

    istream::good() in a dll

    Hi all,

    we are trying to port a fairly large c++ library to windows using
    Visual Studio.net 2003, and are running into some unexpected problems
    with the stream handling. Since our development is more or less
    Unix-based, I am afraid I am quite a novice to all Windows - specific
    stuff, so please excuse my naive terminology. :-)

    In our library, we have a string handling class, which contains the
    following piece of code:

    inline
    std::istream& String::getline (std::istream& s, char delimiter)
    throw()
    {
    static char line_buffer[8192];
    s.getline(line_ buffer, 8191, delimiter);
    set(line_buffer );

    return s;
    }

    If I compile the library as a static .lib, and write a program using the
    following code:

    int main(int argc, char **argv)
    {
    std::ifstream f("bla", std::ios::in);
    String line;

    while (f.good())
    {
    line.getline(f) ;
    std::cout << line << std::endl;
    }

    return 0;
    }

    everything works perfectly. But as soon as I compile the library as a dll,
    f.good() does nothing. It always returns true, no matter where in the stream
    I am. Leading, of course, to abnormal program termination since it reads one
    line past the end. The same happens with f.eof(), f.fail(), or f.bad()

    I have been told that strange things might happen if you compile the library
    and the executable with different versions of the CRT, but as far as I can
    see, both use the same: both compilations set a compiler swith /MD

    Anyone got any idea what is happening here? And why it works when using a
    static lib but not when using a dll?

    Thanks a lot,

    Andreas Hildebrandt
  • William DePalo [MVP VC++]

    #2
    Re: istream::good() in a dll

    "Andreas Hildebrandt" <a.hildebrandt@ handshake.de> wrote in message
    news:d0asvs$pjh $01$1@news.t-online.com...[color=blue]
    > I am afraid I am quite a novice to all Windows - specific
    > stuff, so please excuse my naive terminology. :-)[/color]

    Roger that.
    [color=blue]
    >
    > In our library, we have a string handling class, which contains the
    > following piece of code:
    > ...
    > everything works perfectly. But as soon as I compile the library as a dll,
    > f.good() does nothing.[/color]

    What is probably happening is that the execuatble and the DLL are built
    against different instances of the runtime. If you want to share runtime
    "widgets" across a DLL boundary then the executable and DLL must be built
    against the same "flavor" of the DLL BASED runtime. By "flavor" I mean that
    you must use the same debug/nodebug and single/multithreaded version in
    each.

    Regards,
    Will


    Comment

    • Carl Daniel [VC++ MVP]

      #3
      Re: istream::good() in a dll

      William DePalo [MVP VC++] wrote:[color=blue]
      > "Andreas Hildebrandt" <a.hildebrandt@ handshake.de> wrote in message
      > news:d0asvs$pjh $01$1@news.t-online.com...[color=green]
      >> I am afraid I am quite a novice to all Windows - specific
      >> stuff, so please excuse my naive terminology. :-)[/color]
      >
      > Roger that.
      >[color=green]
      >>
      >> In our library, we have a string handling class, which contains the
      >> following piece of code:
      >> ...
      >> everything works perfectly. But as soon as I compile the library as
      >> a dll, f.good() does nothing.[/color]
      >
      > What is probably happening is that the execuatble and the DLL are
      > built against different instances of the runtime. If you want to
      > share runtime "widgets" across a DLL boundary then the executable and
      > DLL must be built against the same "flavor" of the DLL BASED runtime.
      > By "flavor" I mean that you must use the same debug/nodebug and
      > single/multithreaded version in each.[/color]

      He's already building everything with /MD (he thinks). Must be something
      else (unless he's not really building everything with /MD).

      -cd


      Comment

      • William DePalo [MVP VC++]

        #4
        Re: istream::good() in a dll

        "Carl Daniel [VC++ MVP]" <cpdaniel_remov e_this_and_nosp am@mvps.org.nos pam>
        wrote in message news:eSXTaoRIFH A.904@tk2msftng p13.phx.gbl...[color=blue]
        > He's already building everything with /MD (he thinks). Must be something
        > else (unless he's not really building everything with /MD).[/color]

        My bad.

        Andreas: as pennance for not reading your message all the way through <g>,
        and if you have checked the settings and _are_ really building with the
        proper options, then if you like you can send me a the source to the
        function that you are trying to export. I'll try building the DLL and its
        executable client here with VS.Net 2003.

        Regards,
        Will

        P.S. You can delete the obvious attempt at obfuscation from my email address
        but please don't post it here. I have all the offers for Viagra and Cialis I
        could ever want. ;-)


        Comment

        Working...