By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,238 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,238 IT Pros & Developers. It's quick & easy.

Using file streams in DLL

P: n/a
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
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
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/iostrea...ratorgtgt.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.
Jul 19 '05 #2

P: n/a
> 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?
Jul 19 '05 #3

P: n/a
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.

Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.