The demo code is as below. Please note: A BSTR cannot be directly assigned a string literal. A BSTR is a length prefixed Unicode string, so an assignment like this has virtually zero chance of marshaling across processes because the length prefix is completely arbitrary. The best way is to initialize the BSTR by CComBSTR.
static HRESULT test1() { HRESULT hr = S_OK; // assume we have got Inventor application // the code opens a part document and // saves it to a jpg file CComPtr pDocs; hr =m_pApplication->get_Documents(&pDocs); if (FAILED(hr)) return hr; _bstr_t docName("C:\Part1.ipt"); CComPtr pDoc; hr = pDocs->Open(docName,VARIANT_TRUE,&pDoc); if (FAILED(hr)) return hr; CComBSTR sjpgname(L"C:\Part1.ipt.jpg");
<p style="margin: 0px"><span style="line-height: 140%">     hr = pDoc->SaveAs(sjpgname,VARIANT_TRUE);</span></p> <p style="margin: 0px"><span style="line-height: 140%">     </span><span style="line-height: 140%;color: blue">if</span><span style="line-height: 140%"> (FAILED(hr)) </span><span style="line-height: 140%;color: blue">return</span><span style="line-height: 140%"> hr;</span></p> <p style="margin: 0px"> </p> <p style="margin: 0px"><span style="line-height: 140%">     hr = pDoc->Close(VARIANT_TRUE);</span></p> <p style="margin: 0px"><span style="line-height: 140%">     </span><span style="line-height: 140%;color: blue">if</span><span style="line-height: 140%"> (FAILED(hr)) </span><span style="line-height: 140%;color: blue">return</span><span style="line-height: 140%"> hr;</span></p> <p style="margin: 0px"><span style="line-height: 140%">}</span></p> </div>

Leave a Reply