stupid dll's and ofstream

Started by
15 comments, last by zackriggle 20 years, 11 months ago
does the fstream class have any specific issues when used with a dll? here is some code that reproduces the error:
  
//

//:::::::::::::::::::::::::

//      TESTDLL.H

//:::::::::::::::::::::::::

//

#include <fstream.h>
#ifdef TESTDLL_EXPORTS
#define TESTDLL_API __declspec(dllexport)
#else
#define TESTDLL_API __declspec(dllimport)
#endif

// This class is exported from the TestDLL.dll

class Debugger
{
public:
	TESTDLL_API Debugger(void);
	TESTDLL_API ~Debugger(void);
	TESTDLL_API void AddError(char loc_error[], char error_message[], DWORD error_number);
	TESTDLL_API void AddError(char error_message[], DWORD error_number);
	TESTDLL_API void Add(char message[]);
	ofstream	debugfile;
};



//

//:::::::::::::::::::::::::

//     TESTDLL.CPP

//:::::::::::::::::::::::::

//

#define TESTDLL_EXPORTS
#include <fstream.h>
#include "testdll.h"
TESTDLL_API Debugger::Debugger()
{
	debugfile.open("test.txt");
};
TESTDLL_API Debugger::~Debugger()
{
	debugfile.close();
};
TESTDLL_API void Debugger::AddError( char loc_error[], char error_message[], DWORD error_number)
{
	debugfile << "### Error ###";
		debugfile << endl;
	debugfile << "Location: ";
		debugfile << loc_error;
		debugfile << endl;
	debugfile << "Description: ";
		debugfile << error_message ;
		debugfile << endl;
	debugfile << "Erro Number: " ;
		debugfile << error_number ;
		debugfile << endl;
	debugfile << endl;
};
TESTDLL_API void Debugger::AddError(char* error_message, DWORD error_number)
{
	debugfile << " ### Error ###\n";
	debugfile << "Description: " << error_message << endl;
	debugfile << "Erro Number: " << error_number << endl;
	debugfile << endl;
};
TESTDLL_API void Debugger::Add(char* message)
{
	debugfile << " ### Other ###\n" << message << endl;
	debugfile << endl;
};


/*
    Compile testdll.cpp as a DLL file.
*/



//

//:::::::::::::::::::::::::

//     TEST.CPP

//:::::::::::::::::::::::::

//  Compile this as an executeable file....

//  Include testdll.lib


#include <fstream.h>
int main()
{ 
     Debugger dbg;
     dbg.Add("TEST!");
};

  
And you get some bullshit error... I wrote this code on the fly so it may have some compile errors. I have it so that it compiles fine, but is part of a larger project so i did not include all of the code. If there are some compile errors, just fix them real quick... ================== My (soon-to-be) Cherished Cookie of Appreciation: -- MattB - for WinSock advice --
Advertisement
And the error is...?
Come on JuNC can''t you read? It''s some bullshit.
Ah! Sorry, I forgot I always use -DNO_BULL_WARN so I never get those.
When I run it, it gives me a BS runtime error (Kernel32.dll, offset is 0x00 ...) I have no idea what it is.
You''re probably using the wrong version of the runtime library. I think you need to link with the multithreaded dll version (someone correct me if I''m off here, I''m no dll/multithreading expert). Usually choosing the library is done via a compiler switch, or if you''re using an IDE like Visual C++ it''s probably an option somewhere in the project settings. The options will be like:

single threaded
multi threaded dll
single threaded debug
multi threaded dll debug
Use the standard #include <fstream> instead of the deprecated #include <fstream.h>.

You don''t need all those TESTDLL_API, you only need one at the top of the class:

  class TESTDLL_API Debugger{public:   Debugger();   ...};  


Check the runtime library you are using (as Dobbs said).

What _exactly_ are the error message you get?


Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
when using an fstream or stl vector/ect in a dll, you should still get a warning (4215 if im not mistaken).. but it should still compile fine. if im understanding you correctly, you''re getting a runtime error?

-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Do NOT let Dr. Mario touch your genitals. He is not a real doctor!

-eldee;another space monkey;[ Forced Evolution Studios ]
quote:Original post by eldee
you should still get a warning (4215 if im not mistaken

Compiler Warning (level 1) C4215 - nonstandard extension used : long float?


Update GameDev.net system time campaign - success at last
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
as Dobbs said, the usual cause of weird runtime errors when you move things to Dlls is that your Dlls and project are not set to use the same runtime library ... under Visual C++, go to project settings/properties for both your Dll and you Application and check that "C++/CodeGenerattion/Runtime Library" for both the Dll and App are the same (each debug should match, and each release version too ... and if you are using something like the SDL or Xerces-C binary release then they MUST be Multithreaded Dll versions).

This topic is closed to new replies.

Advertisement