MFC:throw "Access violation" when execute "IHTMLTable::insertRow" - mfc

I want to create rows and cells in the CDHtmlDialog. Here is the code my wrote:
void CHtmlTableTestDlg::OnDocumentComplete(LPDISPATCH pDisp, LPCTSTR szUrl)
{
IHTMLTable* table;
this->GetElement(_T("Table"),(IHTMLElement**)&table);
IDispatch* dispatch;
table->insertRow(1,&dispatch);
}
Here is the html of the table:
<table id="Table">
<tr>
<th>Column</th>
</tr>
</table>
I debugged the project and got the error below when execute "table->insertRow(1,&dispatch)"
Unhandled exception at 0x3fa53780 in HtmlTableTest.exe: 0xC0000005: Access violation writing location 0x00000001.
I have uploaded my test project:
http://top720.com/Temp/HtmlTableTest.zip
How to create rows and cells base on the CDHtmlDialog?
Thank you for any helps in in advance.

You can't just cast between IHTMLElement and IHTMLTable. Try this:
IHTMLElement* element;
if(S_OK == this->GetElement(_T("Table"),(IHTMLElement**)&element)) {
IHTMLTable* table;
if(S_OK == element->QueryInterface(&table)) {
IDispatch* dispatch;
table->insertRow(1,&dispatch);
table->Release();
table = NULL;
}
}
It would also be worth reading an introduction to COM.

Related

how to solve a violation Error using a method

I am getting such a weird violation error by using the getAt() method.
I use the method in this order:
OdDbBlockTablePtr w_kOdBlockTablePtr ;
bool lbCreateDefaults = false;
OdDb::MeasurementValue lkMeasurement = OdDb::kEnglish;
OdDbDatabasePtr pDb;
// Datenbank initialisieren
pDb = g_ExSystemServices.createDatabase(lbCreateDefaults,
lkMeasurement);
// TABLE - Hold Ptr
w_kOdBlockTablePtr = pDb->getBlockTableId().openObject(OdDb::kForWrite);
const wchar_t AcadBlockModelSpace[] = L "*MODEL_SPACE";
wstring lsModelSpace(AcadBlockModelSpace);
w_kOdModelSpaceBlockRecPtr = GetTableRecordIdFromName(lsModelSpace, (OdDbSymbolTablePtr&)w_kOdBlockTablePtr).safeOpenObject(OdDb::kForWrite);
OdDbObjectId K_TeighaClass::GetTableRecordIdFromName(wstring& psName, OdDbSymbolTablePtr& pkTablePtr)
{
OdDbObjectId lkId;
try {
OdString lsOdName = psName.c_str();
lkId = pkTablePtr->getAt(lsOdName);
}
catch (OdError& err)
{
DoOdError(err, NULL, NULL);
}
return lkId;
}
I would really appreciate if someone could help me.
Thanks in advance
That's not weird at all. If you hover your mouse over pkTablePtr, you will almost certainly find that it is nullptr (or the debugger might report this as 0).
There's not enough information in your question to say why this might be, but since you are already running under the debugger you can walk through your code and find out.
try ... catch won't catch a hard error like this, by the way. For that, you need __try ... __except (supported on Windows only).

Change div innerHTML through IHTMLDocument2 and C++

I'm trying to change the content of a div using IHTMLDocument2 interface this way:
IHTMLElementCollection* collection = NULL;
IDispatch* mydiv;
doc2->get_all(&collection);
long count;
collection->get_length(&count); //just to check I get something
CComVariant varstr = L"mydivname";
CComVariant varint = 0;
collection->item(varstr, varint, &mydiv); //this works I get the div
IHTMLElement* htmldiv;
mydiv->QueryInterface(IID_IHTMLElement, (void**)&htmldiv);
CComBSTR html;
htmldiv->get_innerHTML(&html); //works too, I get the current content
HRESULT hr=htmldiv->put_innerText(L"hello"); //this does not work but returns S_OK
collection->Release();
So the content of my div is just cleared and not replaced with "hello", I don't understand why, can it be a security issue ?
Thanks
According to the MSDN documentation, the string passed to put_innerText is of type BSTR.
So, I would suggest trying some code like this:
CComBSTR text(OLESTR("hello"));
hr = htmldiv->put_innerText(text);

XSL Transformation in C++ catch message with terminate flag

I've made a project basing on this document to retrieve information from XML file basing on XSL file.
I am trying to throw an error in XSL file:
<xsl:if test="not(PIN/Length/text() = '4')">
<xsl:message terminate="yes">PIN length in input suppose to be 4</xsl:message>
</xsl:if>
But it seems not to work (no errors during work) - just like it is successfully done.
Can I somehow catch this message in C++?
void ManageXML::XML2Generic(string sOrgFilePath, string sOrgXSLFilePath, string sCpfPath)
{
wstring sTempFilePath = s2ws(sOrgFilePath);
LPCWSTR sFilePath = sTempFilePath.c_str();
wstring sTempXSLFilePath = s2ws(sOrgXSLFilePath);
LPCWSTR sXSLFilePath = sTempXSLFilePath.c_str();
HRESULT hr = S_OK;
IXMLDOMDocument *pXMLDom = nullptr;
IXMLDOMDocument *pXSLDoc = nullptr;
CHK_HR(CreateAndInitParserDOM(&pXMLDom));
CHK_HR(LoadXMLFile(pXMLDom, sFilePath, sOrgFilePath)); //cast to LPCWSTR
CHK_HR(CreateAndInitParserDOM(&pXSLDoc));
CHK_HR(LoadXMLFile(pXSLDoc, sXSLFilePath, sOrgXSLFilePath)); //cast to LPCWSTR
// Transform dom to a string:
CHK_HR(TransformDOM2Data(pXMLDom, pXSLDoc, sGenericResult));
CleanUp:
SAFE_RELEASE(pXSLDoc);
SAFE_RELEASE(pXMLDom);
this->CreateGenericFile(sCpfPath);
CoUninitialize();
}
One bad solution that comes to my mind is to make XSL like this:
<xsl:if test="not(PIN/Length/text() = '4')">
<xsl:text>MSXML_ERROR: PIN length in input suppose to be 4</xsl:message>
</xsl:if>
And
CleanUp:
SAFE_RELEASE(pXSLDoc);
SAFE_RELEASE(pXMLDom);
if (sGenericResult.find("MSXML_ERROR") != string::npos)
throw runtime_error("blah blah blah");
this->CreateGenericFile(sCpfPath);
CoUninitialize();

retrieve data from oracle database using the select statement and load to a vector

help!!! i am going to retrieve data from oracle database using the select statement and load to a vector.
SELECT *From table
where table contains ID,NAME and Age with over 100 elements
.... so can i have a simple technique to retrieve the values using c++(console) using the resultset.
.........
//here my struct first.
struct adopp
{
int age,
string name;
string id;
};
//the vector to load
vector<string> load()
{
oc::Environment* evt = oc::Environment::createEnvironment(oc::Environment::DEFAULT);//oracle::occi::
oc::Connection* con=evt->createConnection("usr","psw", "db");
string sqlQuery = "SELECT * FROM table";
oc::Statement *sttmnt = con->createStatement(sqlQuery);
oc::ResultSet* rset = sttmnt->executeQuery();
vector<string>vet;
adopp adp;
while(rset->next())
{
string iddd= rset->getString(1);
adp.id=iddd;
vet.push_back(iddd);
string nameee= rset->getString(2);
adp.name=nameee;
vet.push_back(nameee);
int age=rset->getint(3);
adp.age=ageee;
vet.push_back(ageee);
}
con->terminateStatement(sttmnt);
evt->terminateConnection(con);
oc::Environment::terminateEnvironment(evt);
return vet;
}
The Error/Exception popup message says:
Unhandled exception at 0x53cbad4a (msvcp100d.dll) in Test_project_L.exe: 0xC0000005: Access violation reading location 0xccccccd0.
thanks for your help.
Based on the understanding of your question and this question's answer
Kindly change
while(rset->next())
to
while(rset->next()!= NULL)

Getting runtime error information from IXMLDOMDocument::transformNode function

I'm using IXMLDOMDocument::transformNode to apply an XSLT transform using C++ code that looks a little bit like this:
CComPtr<IXMLDOMDocument2> spXMLDoc;
// load spXMLDoc
CComPtr<IXMLDOMDocument2> spXSLDoc;
// load spXSLDoc
BSTR *pResult;
HRESULT hr = spXMLDoc->transformNode( spXSLDoc, pResult );
if (FAILED(hr))
{
// Handle me
}
This code works, however if the transform fails then I don't know how to get any information about where or why it failed - at the moment its failing (on a complex XSLT with multiple includes) with a HRESULT of E_FAIL - is there any way I can get some more detail on why its failing?
I've already tried the parseError property to get more error detail:
IXMLDOMParseError *parseError = NULL;
hr = spXMLDoc->get_parseError(&parseError);
if ( !FAILED( hr ) )
{
long errorCode = 0;
hr = parseError->get_errorCode(&errorCode);
// etc...
The call to get_parseError succeeds, however the call to get_errorCode fails with HRESULT S_FALSE, indicating that there was not a parse error. This page tells me that there are two types of error - parse errors and runtime errors. It shows how to handle both, however appears to be JavaScript oriented - in my case no C++ runtime errors are thrown.
Sorry, I'm not sure from C++. You might try a quick command line transformation to help find the error in XSLT. There's a number of errors where the XSL will load, but can't transform. For an example pop an somewhere in the XSL file to trigger this type of error.
Here's a sample command line transformation tool. Write to transform.js and run cscript.exe transform.js from a command line
var strDOMObject = "MSXML2.FreeThreadedDOMDocument";
var strHTTPObject = "MSXML2.XMLHTTP";
var strTemplateObject = "MSXML2.XSLTemplate";
function transform( xml, xsl ) {
var xslt = new ActiveXObject( strTemplateObject );
var xmlReturn = new ActiveXObject( strDOMObject );
var xslProc;
try {
xslt.stylesheet = xsl;
} catch( e ) {
throw e;
}
xslProc = xslt.createProcessor();
xslProc.input = xml;
xslProc.transform();
return xslProc.output;
}
try {
var xml = new ActiveXObject( strDOMObject );
xml.load( "id.xml" );
var xsl = new ActiveXObject( strDOMObject );
xsl.load( "id.xsl" );
WScript.Echo( transform( xml, xsl ) );
} catch( err ) {
WScript.Echo (err.description );
}
Hope this helps, and that you can find out the C++ answer.