Using file streams in DLL

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

    Using file streams in DLL

    Hello,
    I am experiencing a strange problem using file streams in a DLL
    library. I am using Borland C++ Builder 5.0 Professional and I am
    trying to simply read a file using ifstream inside a DLL. However,
    after creating the ifstream object I am losing all stack values,
    including arguments to the current function and earlier function
    calls. The very simple DLL file listing is below:

    #include <vcl.h>
    #include <windows.h>
    #include <iostream.h>
    #include <fstream.h>
    #pragma hdrstop
    #pragma argsused
    using namespace std;
    int WINAPI DllEntryPoint(H INSTANCE hinst, unsigned long reason, void*
    lpReserved)
    {return 1;}

    extern "C" __declspec(dlle xport)
    void DllRunTest(int a, int b, int c){
    int d;
    d = a + b + c; // all parameters are correct here
    ifstream ifs("C:\\Test.t xt"); //try to open file...
    d = a + b + c; // random values for a,b,c here

    char buffer[100];
    ifs >> buffer; // file input works as expected here
    // but we can't return to the calling function
    // as all function calls that were recorded in the stack are now
    gone
    // so the program crashes here
    }

    I attach the generated lib file to a simple application and simply
    call the function in DLL when a button is pressed:
    extern "C" __declspec(dlli mport)
    void DllRunTest(int a, int b, int c);
    void __fastcall TForm1::Button1 Click(TObject *Sender){
    DllRunTest(1, 2, 3);
    }

    Annyone has any ideas how to make ifstreams work correctly in the DLL?
    Naturally, the above code works fine when used in a single
    application. Any help would be greatly appreciated.

    Saulius
  • Gianni Mariani

    #2
    Re: Using file streams in DLL

    Saulius wrote:[color=blue][color=green][color=darkred]
    >>> char buffer[100];
    >>> ifs >> buffer; // file input works as expected here[/color]
    >>
    >>What do you think this is supposed to do ?[/color]
    >
    >
    > This is just a demonstration of file input (read a string of chars
    > from text file until delimiter and write them to the buffer). This
    > works perfectly and I can see the characters read in the debugger.
    > If my problem has nothing to do with DLL, then why this code crashes
    > in DLL and works correctly in a single application without DLL?
    >
    > Saulius[/color]

    Ask yourself how many bytes will be copied into buffer.


    "ifs >> buffer;" is calling

    istream& operator>> (istream& is, char* str );

    extract from:


    Extracts characters and stores them in succesive locations starting at
    location pointed by str. Extraction ends when the next element is either
    a valid whitespace or a null character, or if the End-Of-File is reached.

    A null character is automatically appended after the extracted characters.

    The extraction operation can be limited to a certain number of
    characters (thus avoiding the possibility of buffer overflow) if the
    field width inherited member (ios_base::widt h) is set to a value greater
    than 0. In this case, the extraction ends one character before the count
    of characters extracted reaches the value of field width leaving space
    for the ending null character. After a call to this extraction operation
    the value of the field width is reset to 0.


    Comment

    • Saulius

      #3
      Re: Using file streams in DLL

      > Ask yourself how many bytes will be copied into buffer.[color=blue]
      > "ifs >> buffer;" is calling
      > istream& operator>> (istream& is, char* str );[/color]

      As I said before, it copies one word from the text file until space is
      reached. I KNOW for sure that my input file does not contain any words
      longer than 100 characters, so we NEVER exceed the capacity of the
      char buffer (as you are trying to point out). Moreover, I can even
      comment out these two lines:
      char buffer[100];
      ifs >> buffer;
      and the program still crashes. (These two lines were included for
      demonstration only). After erasing the two lines, the function looks
      like this:

      extern "C" __declspec(dlle xport)
      void DllRunTest(int a, int b, int c){
      int d;
      d = a + b + c;
      ifstream ifs("C:\\test.t xt");
      d = a + b + c; //corrupt values for a,b,c
      } // program crashes upon exit from this function

      And the stack (as seen in the debugger) becomes corrupt after the line
      ifstream ifs("C:\\test.t xt");
      is executed. Therefore we lose values for parameters a,b,c and cannot
      return to the calling function. So the problem lies in the creating of
      the ifstream object or opening the file, not in the file reading
      operation.
      Any ideas?

      Comment

      • Gianni Mariani

        #4
        Re: Using file streams in DLL

        Saulius wrote:[color=blue][color=green]
        >>Ask yourself how many bytes will be copied into buffer.
        >>"ifs >> buffer;" is calling
        >>istream& operator>> (istream& is, char* str );[/color]
        >
        >
        > As I said before, it copies one word from the text file until space is
        > reached. I KNOW for sure that my input file does not contain any words
        > longer than 100 characters, so we NEVER exceed the capacity of the
        > char buffer (as you are trying to point out). Moreover, I can even
        > comment out these two lines:
        > char buffer[100];
        > ifs >> buffer;
        > and the program still crashes. (These two lines were included for
        > demonstration only). After erasing the two lines, the function looks
        > like this:
        >
        > extern "C" __declspec(dlle xport)
        > void DllRunTest(int a, int b, int c){
        > int d;
        > d = a + b + c;
        > ifstream ifs("C:\\test.t xt");
        > d = a + b + c; //corrupt values for a,b,c
        > } // program crashes upon exit from this function
        >
        > And the stack (as seen in the debugger) becomes corrupt after the line
        > ifstream ifs("C:\\test.t xt");
        > is executed. Therefore we lose values for parameters a,b,c and cannot
        > return to the calling function. So the problem lies in the creating of
        > the ifstream object or opening the file, not in the file reading
        > operation.
        > Any ideas?[/color]

        Versionitis.

        You're probably linking with a version of the constructor for ifstream
        that does not match the one in the header you're including.

        I have experienced this kind of problem with my own code but not with
        the standard library.

        Comment

        Working...