Windows Mobile Developer Network Forum

Windows Mobile Developer Network Forum
It is currently Fri Jul 30, 2010 2:59 pm

All times are UTC [ DST ]




Post new topic Reply to topic  [ 1 post ] 

Rate this article
1 0%  0%  [ 0 ]
2 0%  0%  [ 0 ]
3 0%  0%  [ 0 ]
4 0%  0%  [ 0 ]
5 0%  0%  [ 0 ]
Total votes : 0
Author Message
 Post subject: QA: I cannot use COM objects using #import because _com_issu
PostPosted: Thu Oct 28, 2004 4:41 pm 
Offline
Site Admin

Joined: Sun Mar 30, 2003 4:04 pm
Posts: 1004
<h2>Question</h2> When I use #import directive I get the following compile error: <i>error C2065: '_com_issue_errorex' : undeclared identifier.</i> The same code works OK on PC. Can I use smart pointers and #import directive on Pocket PC? How can I use them?
<h2>Answer</h2> <i>_com_issue_error</i> and <i>_com_issue_errorex</i> functions that are used in smart pointers are not defined in eVC++. You should define them yourself or use <a href="/libraries/stutil.html">STUtil</a> library that defines them. These functions are not defined because they use C++ exceptions that are not supported in eVC++.
Smart pointers uses exceptions to inform about error HRESULT return code. Method of smart pointer are wrappers to corresponding methods of wrapped COM interface like:
Code:
inline _bstr_t IConverter::IntToString ( int nValue ) {
    BSTR _result;
    HRESULT _hr = raw_IntToString(nValue, &_result);
    if (FAILED(_hr)) _com_issue_errorex(_hr, this, __uuidof(this));
    return _bstr_t(_result, false);
}

This method was defined in IDL as
Code:
HRESULT IntToString([in] INT nValue, [out, retval] BSTR *pResult);

As we see the method of the smart pointer return value of the [retval] parameter. But return value was used for error information. Smart pointers uses _com_issue_errorex function that throws C++ exceptions.
<h3>Using STUtil library</h3> <ol> <li>Download <a href="/libraries/stutil.html">STUtil</a> library and add <i>STUtil.h</i> and <i>STUtil.cpp</i> files into your project.</li> <li>Add include of <i>STUtil.h</i> file into <i>Stdafx.h</i> file.</li> <li>Use smart pointers. Smart pointers will throw exceptions that could be caught by <i>__except (CATCH_COM_ERROR)</i> code.</li> <li>Check COM error code by using GetComError() function that returns CSTComError object.</li> <li>If you want ignore COM error define ST_IGNORE_COM_EXCEPTIONS macro (using <i>#define ST_IGNORE_COM_EXCEPTIONS</i>) in your <i>Stdafx.h</i> file. In that case no exception will be thrown, all COM error codes will be ignored.</li> </ol>
<h2>Source code</h2> Here is a sample code that uses exceptions.
Code:
In StdAfx.h file:

#include "STUtil.h"


In custom .cpp code:

IConverterPtr cnv;
_bstr_t str;
__try {
   cnv.CreateInstance(_T("Test.Converter"));
   str = cnv->IntToString(46);
}
__except (CATCH_COM_ERROR) {
   switch (GetComError().Error()) {
   case E_FAIL:
      ::AfxMessageBox(_T("E_FAIL"));
      break;
   case E_INVALIDARG:
      ::AfxMessageBox(_T("E_InvalidArg"));
      break;
   }
}   

Here is a sample code that doesn't use exceptions (ignores COM errors).
Code:
In StdAfx.h file:

#define ST_IGNORE_COM_EXCEPTIONS
#include "STUtil.h"


In custom .cpp code:

IConverterPtr cnv(_T("Test.Converter"));
_bstr_t str = cnv->IntToString(46);

<h2>Related resources:</h2>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/sections/com.html">Section: COM/ActiveX</a>
<img src="http://www.pocketpcdn.com/images/bullet.gif" width="22" height="15"><a href="/libraries/stutil.html">Library: STUtil</a>


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group