C++ .NET object instantiation

G

Greg Freeman

I'm missing something obvious with C++ instantiation
of .NET objects. (accessing managed code from unmannaged
code) Anyone suggest good examples in C++ (not C#)?
I'm trying to create an XmlTextWriter object, but the
class is undeclared. How do you declare a class in .NET
C++?

-Greg
 
K

Konstantin Erman

Here is the sample in managed C++:

#include "stdafx.h"

#using <mscorlib.dll>

using namespace System;
using namespace System::Xml;

static void WriteQuote(XmlWriter* pWriter, String* symbol,
double price, double change, long volume)
{
pWriter->WriteStartElement("Stock");
pWriter->WriteAttributeString("Symbol", symbol);
pWriter->WriteElementString("Price", XmlConvert::ToString(price));
pWriter->WriteElementString("Change", XmlConvert::ToString(change));
pWriter->WriteElementString("Volume", XmlConvert::ToString(volume));
pWriter->WriteEndElement();
}

int _tmain()
{
XmlTextWriter* pWriter = new XmlTextWriter(Console::Out);
pWriter->Formatting = Formatting::Indented;
WriteQuote(pWriter, "MSFT", 74.125, 5.89, 69020000);
pWriter->Close();
return 0;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top