Passing a smart pointer to and IDL COM component

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
We are replacing some C++ COM modules with VB.NET COM module. We are using the #import directive for the TLB files generated by our VB.NET project. I ran into a case where I needed to pass smart pointer to a C++ COM which still uses an IDL file to expose
its methods. I found a solution which works but I would like to know if there is a better way:
1. In the main appl I create an instance of the smart pointer:
<pre class="prettyprint #import "NavMenuContext.tlb"
rename_namespace("NavMenuContext") raw_interfaces_on .. pitfNAVMenuContxt = NavMenuContext::_clsMenuContextuelPtr(__uuidof(NavMenuContext::clsMenuContextuel)); [/code]

2. In order for the old C++ COM to receive the pointer, I have manualy modified its IDL file. I have added a definition for the interface exposed by the VB.NET module:
<pre class="prettyprint [
odl,
uuid(63A0D3D5-0923-4803-B058-A026E542E776),
version(1.0),
dual,
oleautomation,
custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "Navigateur_Menu_Contextuel.clsMenuContextuel+_clsMenuContextuel")
]
interface _clsMenuContextuel : IDispatch {
[id(0x00000001), propput]
HRESULT IdDossier([in] BSTR rhs);
[id(0x00000002), propput]
HRESULT WorksInfoPieceName([in] BSTR rhs);
[id(0x00000003)]
HRESULT getMenuOption(
[in] BSTR _strIDPiece,
[in, out] SAFEARRAY(VARIANT)* _arrContextOption,
[in, out] IDQSEror** _IntDQSEror,
[out, retval] long* pRetVal);
};[/code]
3. And I have modified the signature of the method which I need to call passing the smart pointer as first argument:
<pre class="prettyprint [id(20), helpstring("method rightClickMenuOption")] HRESULT rightClickMenuOption([in] _clsMenuContextuel* _pitfMenuContext, [out, retval] IDQSEror** _ppitfErreur);[/code]
4. Trying to call the method with my smart pointer will result in a compile error:
"Impossible to convert parameter 1 from NavMenuContext::_clsMenuContextuelPtr to _clsMenuContextuel*"

5. So I figured out a way to solve this by creating a second pointer:
<pre class="prettyprint hr = pitfNAVMenuContxt.QueryInterface(__uuidof(NavMenuContext::_clsMenuContextuel), &pitfMenuContextuel);[/code]
And this pointer is accepted by the compiler, and everything works fine. But...I have done this right ?
Thanks for any tips.

View the full article
 
Back
Top