Sign in to follow this  

[C++/Win32 API] Problem with MessageBox

This topic is 4762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I made a small class for exceptions in my program, but when I try to show the error with a MessageBox, the text just consists of like 50 Ý's. The text is just printed to the file so I could see if the string was right and it was.
class DXError
{
public:
	DXError(const std::string& errorMessage, const std::string& fileName, 
			const std::string& function, int line) 
		: mErrorMessage(errorMessage), mFileName(fileName), mFunction(function),
		  mLine(line) {}

	const char* ToString() const
	{
		std::string error;
		error += "In file: " + mFileName + "\r\n";
		error += "In function: " + mFunction + "\r\n";
		error += "On line: " + boost::lexical_cast<std::string>(mLine) + "\r\n";
		error += "\r\n" + mErrorMessage;

		std::ofstream file("output.txt");
		file << error;
		return error.c_str();
	}

private:
	std::string mErrorMessage;
	std::string mFileName;
	std::string mFunction;
	int mLine;
};

#define DX_ERROR(errorMessage) DXError(errorMessage, __FILE__, __FUNCTION__, __LINE__)


// later, catching the exception
catch(const DXError& e)
{
	MessageBox(0, e.ToString(), "Error", 0);
	return 0;
}

Btw, if I have overloaded a function, __FUNCTION__ just give the name so I don't know which of the functions who threw the exception. Ofcourse I could check the line number, but is the a way to get more that the name with some other macro? [Edited by - Eriond on November 29, 2004 12:32:58 PM]

Share this post


Link to post
Share on other sites
You're returning a pointer to a local variable.
class DXError
{
public:
DXError(const std::string& errorMessage, const std::string& fileName,
const std::string& function, int line)
: mErrorMessage(errorMessage), mFileName(fileName), mFunction(function),
mLine(line) {}

const char* ToString() const
{
// error is a local variable
std::string error;
error += "In file: " + mFileName + "\r\n";
error += "In function: " + mFunction + "\r\n";
error += "On line: " + boost::lexical_cast<std::string>(mLine) + "\r\n";
error += "\r\n" + mErrorMessage;

std::ofstream file("output.txt");
file << error;
// return a pointer to the character data held by error
// the return causes the destruction of error, so the pointer returned is no longer valid
return error.c_str();
}

private:
std::string mErrorMessage;
std::string mFileName;
std::string mFunction;
int mLine;
};

#define DX_ERROR(errorMessage) DXError(errorMessage, __FILE__, __FUNCTION__, __LINE__)


// later, catching the exception
catch(const DXError& e)
{
MessageBox(0, e.ToString(), "Error", 0);
return 0;
}


Solution? Just return a std::string and call c_str() when you call MessageBox, i.e.:
MessageBox(0, e.ToString().c_str(), "Error", 0);


Enigma

Share this post


Link to post
Share on other sites

This topic is 4762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this