Using file streams in DLL

Discussion in 'C++' started by Saulius, Aug 16, 2003.

  1. Saulius

    Saulius Guest

    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(HINSTANCE hinst, unsigned long reason, void*
    lpReserved)
    {return 1;}

    extern "C" __declspec(dllexport)
    void DllRunTest(int a, int b, int c){
    int d;
    d = a + b + c; // all parameters are correct here
    ifstream ifs("C:\\Test.txt"); //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(dllimport)
    void DllRunTest(int a, int b, int c);
    void __fastcall TForm1::Button1Click(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
     
    Saulius, Aug 16, 2003
    #1
    1. Advertising

  2. Saulius wrote:
    >>> char buffer[100];
    >>> ifs >> buffer; // file input works as expected here

    >>
    >>What do you think this is supposed to do ?

    >
    >
    > 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


    Ask yourself how many bytes will be copied into buffer.


    "ifs >> buffer;" is calling

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

    extract from:
    http://www.cplusplus.com/ref/iostream/istream/operatorgtgt.html

    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::width) 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.
     
    Gianni Mariani, Aug 17, 2003
    #2
    1. Advertising

  3. Saulius

    Saulius Guest

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


    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(dllexport)
    void DllRunTest(int a, int b, int c){
    int d;
    d = a + b + c;
    ifstream ifs("C:\\test.txt");
    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.txt");
    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?
     
    Saulius, Aug 17, 2003
    #3
  4. Saulius wrote:
    >>Ask yourself how many bytes will be copied into buffer.
    >>"ifs >> buffer;" is calling
    >>istream& operator>> (istream& is, char* str );

    >
    >
    > 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(dllexport)
    > void DllRunTest(int a, int b, int c){
    > int d;
    > d = a + b + c;
    > ifstream ifs("C:\\test.txt");
    > 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.txt");
    > 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?


    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.
     
    Gianni Mariani, Aug 17, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anushi
    Replies:
    5
    Views:
    8,769
  2. Marc Cromme
    Replies:
    1
    Views:
    2,565
    Jorge L Rivera
    Dec 2, 2003
  3. Krzysiek
    Replies:
    2
    Views:
    528
    Krzysiek
    Jul 5, 2004
  4. Replies:
    2
    Views:
    516
    Larry Bates
    Mar 9, 2007
  5. H. Simpson
    Replies:
    4
    Views:
    281
    H. Simpson
    Aug 3, 2004
Loading...

Share This Page