Using file streams in DLL

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

  1. Saulius

    Saulius Guest

    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*
    {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
    // 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, Aug 16, 2003
    1. Advertisements

  2. 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::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
    1. Advertisements

  3. Saulius

    Saulius Guest

    Ask yourself how many bytes will be copied into buffer.
    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
    Any ideas?
    Saulius, Aug 17, 2003
  4. 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
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.