473,395 Members | 1,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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(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
3 6978
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
> 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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: reddy | last post by:
I am trying to insert a node into an XMLFile. using XMLTextwriter. My Question is Is it possible to do without using XMLDocument. Because its loading all the the file into memory. I just want to...
3
by: sb | last post by:
I think streams are nice, but what do you do when you have to write to or, even worse, read from a FILE*, for example a UNIX stream? C++ streams can not be created from FILE*'s or have them...
3
by: Tron Thomas | last post by:
What does binary mode for an ofstream object do anyway? Despite which mode the stream uses, operator << writes numeric value as their ASCII representation. I read on the Internet that it is...
6
by: radnoraj | last post by:
Hi, I am sucessfull in redirecting console output to a file. but in this case nothing is displayed on the console, cout output is written to file without display. how do write the output to...
8
by: Alex | last post by:
Hi all, can someone please show me how to get the size of a file with ANSI C? both in text mode and binary mode. thanks in advance.
47
by: Bonj | last post by:
I downloaded the gzlib library from zlib in order to do compression. (http://www.gzip.org/zlib) The prototype of the compression function seems to be int compress (Bytef *dest, uLongf *destLen,...
5
by: Just Me | last post by:
Using streams how do I write and then read a set of variables? For example, suppose I want to write into a text file: string1,string2,string3 Then read them later. Suppose I want to write...
0
by: iwdu15 | last post by:
hi, im creating an instant messenger using TCP sockets, and everythings been fine until now. i want to be able to send rich text, but im hitting a wall here. what i want to do is send the font...
1
by: gangs | last post by:
I need to know how can I open a file which is named using UTF-16 encoding in unicode. I am desperately trying to open the file using the wofstream typedef, but even it takes the parameter as const...
0
by: Christian Heimes | last post by:
Grant Edwards wrote: It's dangerous to mix streams and descriptors. Traditionally freopen() is used to redirect a standard stream to a file: ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.