Computer Chess Club Archives


Search

Terms

Messages

Subject: OT: VC6, fstream, and .dll

Author: Brian Kostick

Date: 04:10:34 10/06/01


Someone asked me this question and I could not answer him. With the insightful
programmers here maybe help is not so far away...

In short, when an program is made as one unit, integral variable i can be read
off fstream s:  s >> i;

If reading the stream in this way is moved into a .dll then it crashes. Keep in
mind that maybe the problem is compiler specific. I don't know. Insights and/or
a proper solution are much appreciated. BK

Example that demonstrates the problem:
1. Create a workspace.

2. Add a Win32 DLL project (not MFC), and include the following files.

cls.h :
#include <fstream>

class CLS {

public :
	__declspec(dllexport) CLS(std::fstream &) ;

	__declspec(dllexport) int BigFunc() ;
} ;

cls.cpp :
#include "cls.h"
#include <iostream>
#include <fstream>

using namespace std ;

__declspec(dllexport) CLS::CLS(fstream &s) {
	int i ;
	cout << "Constructor" << endl ;
	s >> i ;
	cout << i ;
}

__declspec(dllexport) int CLS::BigFunc() {
	cout << "BigFunc" << endl ;
	return 0 ;
}

3. Add a Win32 Console project, and include the following file.

app.cpp :
#include "cls.h"
#include <fstream>

using namespace std ;

int main() {
	fstream s ;
	s.open("test.txt", ios::in) ;
	CLS c(s) ;
	c.BigFunc() ;
	return 0 ;
}

4. Go to Project->Settings->Link, and add the LIB file produced by the DLL
project to the "Object/Library Modules" list.  For example, I added
"Debug/cls.lib".  You choose whichever path is appropriate, depending on the
output directory of your DLL project.

5. Create a text file called test.txt and put the ascii text representing an
integer into it. For example : 123



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.