Click to See Complete Forum and Search --> : no streams for native C++ method inside mixed DLL called from C# GUI


OneDanShow
December 7th, 2005, 04:20 AM
Hello everyone,

I'm working on a school project and am having the hardest time with it. Here's what I have:

First, C++ Class Library project in VS.NET 2003 with unmanaged C++ code that contains the functionality and managed C++ wrappers. This is all compiled to a mixed DLL.

Second, I have a C# Windows App project in VS.NET 2003 that has the above DLL as a reference and creates instances of the managed C++ wrapper classes to access the native C++ functionality.

This is all fine, and the C# and native C++ talk, except when a native C++ methods contains any streams (i.e. ofstream, stringstream, even cout!). It returns a System.NullPointerExeception. What is the deal?

Here is the method in native C++ that I'm trying to call (just tying to get it to print out a dummy XML file right now):


bool tigerHawk::generateVisualizationXML() {
bool result = false;
// open the XML file for output

ofstream outputfile ("visualizationData.xml");
if(outputfile.is_open()) {
string url = "http://www.onedanshow.com";
string link = "http://www.creativedaniel.com";
// write the <xml> header and <data> nodes
outputfile << "<xml blah blah>" << endl << "<data>" << endl;

int size = 5; //snapShotList.size();
int links = 2;
// for each webSnapShot object
for(int i = 0; i < size; i++) {
outputfile << "\t<page>" << endl;
outputfile << "\t\t<url>" << url << "</url>" << endl;
outputfile << "\t\t<url>" << snapShotList[i]->getURL(); << "</url>" << endl;

//stringstream os(snapShotList[i]->getLinks()); //a standard stringstream which parses 'line
//string link; //a temporary string
//while (os >> link)
for(int i = 0; i < links; i++) {
outputfile << "\t\t<link>" << link << "</link>" << endl;
}

outputfile << "\t</page>" << endl;
}
// close the </data> node and file writer
outputfile << "</data>";
outputfile.close();
result = true;

return result;
}



The code in the managed C++ to call the file:


bool TigerHawkWrap::generateVisualizationXML() {
try {
return (m_pTigerHawk->generateVisualizationXML());
}
catch(NullReferenceException* e) {
return false;
}
}


Then I simply all the method like so in C# (with the DLL reference, using the namespace, etc.)


tigerHawk.generateVisualizationXML()


Thanks for any help! This thing is due soon.

Alex F
December 7th, 2005, 11:12 AM
Remove catch(NullReferenceException* e) block and run the program under debugger. Debugger will break on the line which causes this exception, this will give you more information. I think this is managed exception and problem is not in unmanaged code.

Alex F
December 7th, 2005, 11:18 AM
Generally, it is not a good idea to catch NullReferenceException. You should catch exceptions which can normally happen like I/O exceptions, format exceptions reading user input etc. NullReferenceException is programmer's bug, and it is better to get exception message during program development rather than hide it with exception handler.

NoHero
December 7th, 2005, 11:35 AM
Btw why not using System::Xml namespace classes?