Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Using XmlWriter/Reader? (Read 2707 times)
alan_wood
YaBB Newbies
*
Offline



Posts: 16
Joined: Jul 17th, 2007
Using XmlWriter/Reader?
Jul 17th, 2007 at 2:52pm
Print Post  
I'm trying to get XmlWriter and XmlReader to work in my project, but I can't seem to get the correct sequence of method calls to do this.

XmlReader and XmlWriter both take in a LPDISPATCH during construction (or as part of the SetDocument call).

The documentation says to pass in a "instance of a FlowChart".  However, FlowChart derives off CWnd and not IDispatch.  Therefore, I cannot just pass in a FlowChart object (or a pointer to one).

Trying to call GetIDispatch() on the FlowChart object (from CCmdTarget) results in an error with a comment that says I haven't called EnableAutomation. 

If I call EnableAutomation() and then try to use the resulting GetIDispatch() pointer, I get an error saying that Invoke was called on a null lpDispatch.

Although I'm very strong with C++, I'm relatively green when it comes to COM/ActiveX/MFC.

My setup:

Visual C++ 2005
FlowChartX Pro Trial Edition

Any help would be much appreciated!
  
Back to top
 
IP Logged
 
Stoyo
God Member
*****
Offline


MindFusion support

Posts: 13230
Joined: Jul 20th, 2005
Re: Using XmlWriter/Reader?
Reply #1 - Jul 18th, 2007 at 6:35am
Print Post  
This is from the FCDemo project code, at the end of FCDemoDlg.cpp:

Code
Select All
void CFCDemoDlg::OnBnClickedSaveXmlBtn()
{
	CFileDialog dlg(FALSE);

	if (dlg.DoModal() == IDOK)
	{
		XMLWriter writer;

		writer.CreateDispatch(_T("FlowChartPro.XMLWriter"));

		writer.SetDocument((LPDISPATCH)m_FlowChart.GetControlUnknown());
		writer.Write(dlg.GetPathName());

		writer.ReleaseDispatch();
	}
}

void CFCDemoDlg::OnBnClickedLoadXmlBtn()
{
	CFileDialog dlg(TRUE);

	if (dlg.DoModal() == IDOK)
	{
		XMLReader reader;

		reader.CreateDispatch(_T("FlowChartPro.XMLReader"));

		reader.SetDocument((LPDISPATCH)m_FlowChart.GetControlUnknown());
		reader.Read(dlg.GetPathName());

		reader.ReleaseDispatch();
	}
}
 



Though the right thing to do is to QueryInterface(IID_IDispatch) on the GetControlUnknown() result instead of type-casting. It's a matter of internal knowledge that the IUnkown and IDispath pointers refer to the same object, so you should not rely on that because the implementation might change in the future.

Also, you need the MSXML4 runtime to use the XmlWriter and XmlReader classes.

I hope that helps,
Stoyan
  
Back to top
 
IP Logged
 
alan_wood
YaBB Newbies
*
Offline



Posts: 16
Joined: Jul 17th, 2007
Re: Using XmlWriter/Reader?
Reply #2 - Jul 18th, 2007 at 1:05pm
Print Post  
This helped tremendously.  Thanks.

If anyone else is having this difficulty, below you'll find the code I ended up using:

[code]

// Get the flowchart's IDispatch interface...
LPUNKNOWN lpUnknown = m_pFlowchart->GetControlUnknown();
LPDISPATCH lpDispatch;

if (SUCCEEDED(lpUnknown->QueryInterface(IID_IDispatch, (void**)&lpDispatch)))
{
    XMLWriter xmlWriter;
    xmlWriter.CreateDispatch(_T("FlowChartPro.XMLWriter"));
    xmlWriter.SetDocument(lpDispatch);
    xmlWriter.Write(strFile);
    xmlWriter.ReleaseDispatch();

    lpDispatch->Release();
    lpDispatch = NULL;
}
lpUnknown = NULL;
[/code]
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint