SaveAs to jpg in C++

By Xiaodong Liang

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%">&#160;&#160;&#160;&#160; hr = pDoc-&gt;SaveAs(sjpgname,VARIANT_TRUE);</span></p>    <p style="margin: 0px"><span style="line-height: 140%">&#160;&#160;&#160;&#160; </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">&#160;</p>    <p style="margin: 0px"><span style="line-height: 140%">&#160;&#160;&#160;&#160; hr = pDoc-&gt;Close(VARIANT_TRUE);</span></p>    <p style="margin: 0px"><span style="line-height: 140%">&#160;&#160;&#160;&#160; </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>

Comments

Leave a Reply

Discover more from Autodesk Developer Blog

Subscribe now to keep reading and get access to the full archive.

Continue reading