Message 1 of 2
Out-of-process C++ application causes intermittent crash of Inventor
Not applicable
03-30-2012
01:37 PM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
Hi there,
I've written a piece of code in C++ to open a part in Inventor and get parameter data from the part from out-of-process. This code works, but occasionally (i.e. randomly) execution of the code will result in an error message being returned from Inventor which indicates a crash of the program. I cannot figure out why, and because the problem occurs randomly I've had difficultly isolating the problem. Here is my code - any thoughts on what I might be doing wrong? The error suggests that I might be releasing memory that I don't own or something like that, but since it happens intermittently it is hard to know.
int error;
CComBSTR bsPartName;
CLSID clsid;
FILE *out;
HRESULT hres = NOERROR;
TCHAR szFileName[255];
/* Global Autodesk Inventor pointers */
CComPtr<Application> aiApp;
CComPtr<Document> aiDoc;
/* Get Inventor program ID */
CoInitialize(NULL);
hres = CLSIDFromProgID(OLESTR("Inventor.Application"), &clsid);
/* Open Inventor */
error = 0;
if (hres == S_OK)
{
hres = aiApp.CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER);
if (hres == S_OK) aiApp->Visible = VARIANT_FALSE;
else error = 1;
}
else error = 1;
if (!error)
{
/* Get the part */
_tcscpy(szFileName, _T("C:\\Temp\\Box.ipt"));
_bstr_t sPartName(szFileName);
bsPartName.AppendBSTR(sPartName);
hres = aiApp->Documents->Open(bsPartName, VARIANT_TRUE, &aiDoc);
if (hres != S_OK) error = 1;
if (!error)
{
/* Open file for printing */
out = fopen("C:\\Temp\\InventorTestLog.txt", "at");
/* Get the part document pointer from the document pointer */
CComQIPtr<PartDocument> aiPartDoc(aiDoc);
if (aiPartDoc)
{
/* Define necessary variables */
int ii;
double dim_value;
long ndim;
_bstr_t bsParamName;
TCHAR disp[255];
TCHAR szParamName[255];
_variant_t tempValue;
/* Get the number of model parameters in the part */
ndim = aiPartDoc->ComponentDefinition->Parameters->ModelParameters->Count;
/* Get data for each parameter */
for (ii = 1; ii <= ndim; ii++)
{
bsParamName = aiPartDoc->ComponentDefinition->Parameters->ModelParameters->Item[ii]->Name;
_stprintf(szParamName, _T("%s"), (LPCTSTR)bsParamName);
tempValue = aiPartDoc->ComponentDefinition->Parameters->ModelParameters->Item[ii]->Value;
if (tempValue.vt == VT_R4 || tempValue.vt == VT_R8)
{
if (tempValue.vt == VT_R4) dim_value = tempValue.fltVal;
else dim_value = tempValue.dblVal;
}
else dim_value = 0.0;
if (out)
{
_stprintf(disp, _T("Parameter %s value = %.6f\n"), szParamName, dim_value);
_fputts(disp, out);
}
}
aiPartDoc.Release();
}
/* Close file for printing */
if (out) fclose(out);
}
/* Close the part */
if (aiDoc)
{
hres = aiDoc->Close(VARIANT_TRUE);
aiDoc.Release();
}
}
/* Close Inventor */
if (aiApp)
{
aiApp->Quit();
aiApp.Release();
}
CoUninitialize();