Re: MFC and COM components
On Mar 26, 12:51 am, Hector Santos <sant9...@nospam.gmail.com> wrote:
I never did this before under MFC C/C++. I added COM components to
the Toolbar
right click the ToolBox | Choose Items
and selected some Microsoft components, specifically the Microsoft
TabStrip component. It was added to the palette and now I can drop
the control into the dialog form. Then I right click the control and
"Add Variable" to create a m_tabs class member. When you do this, the
TLB information is used to a class wrapper for the dispatch commands.
In this case, it created, TabStrip2.h and TabStrips2.cpp.
What I want to get access to is the SelectedItem, which it has these
get/put functions to be used in property members:
LPDISPATCH get_SelectedItem()
{
LPDISPATCH result;
InvokeHelper(0xf, DISPATCH_PROPERTYGET, V=
T_DISPATCH, (void*)&result,
NULL);
return result;
}
void putref_SelectedItem(LPDISPATCH newValue)
{
static BYTE parms[] = VTS_DISPATCH ;
InvokeHelper(0xf, DISPATCH_PROPERTYPUTREF=
, VT_EMPTY, NULL, parms,
newValue);
}
void put_SelectedItem(VARIANT * newValue)
{
static BYTE parms[] = VTS_PVARIANT ;
InvokeHelper(0xf, DISPATCH_PROPERTYPUT, V=
T_EMPTY, NULL, parms,
newValue);
}
The question is how do use these in properties with LPDISPATCH?
For example, it is easier for a BOOL types such as get_ShowTips and
put_ShowTips:
BOOL get_ShowTips()
{
BOOL result;
InvokeHelper(0x3, DISPATCH_PROPERTYGET, V=
T_BOOL, (void*)&result, NULL);
return result;
}
void put_ShowTips(BOOL newValue)
{
static BYTE parms[] = VTS_BOOL ;
InvokeHelper(0x3, DISPATCH_PROPERTYPUT, V=
T_EMPTY, NULL, parms,
newValue);
}
By adding the following property to the TabStrip2.h class:
_declspec(property(get=get_ShowTips,put=put_ShowTips)) BOOL ShowT=
ips;
I now can access this member in m_tabs:
m_tabs.ShowTips = FALSE;
What class or object is get_SelectedItem returning? I looked overall
google on this and there is no MFC C/C++ example with this.
OleView says that SelectedItem is returning an ITab*. (There's two MS
tab strips on my system, I picked one in mscomctl.ocx).
Overall, for better results, try to open the type library in OleView
(you can find this in your Win32 SDK, in Bin\OleView.Exe, but I would
guess it lurks somewhere else in VS installation, too).
BTW, from the type library, you should be able to find out where is
the help file for it's component(s) (if there is a help file). For
mscomctl.ocx on my machine, it's "cmctl198.chm".
In all honesty, I always preferred stuff provided by compiler COM
support ("#import") to what MFC wizards spit out. I'd recommend using
this, if you are ready to take the plunge into COM world (it really IS
a different world). You can e.g. use VS help to, say, drop control
onto a dialog, then just use stuff #import gives you (take LPDISPATCH
from the control, turn it into corresponding smart pointer from *.tlh
generated through #import and work with that).
Beware, by default, #import-generated stuff uses throws _com_error to
report errors, which is bad in context of MFC. So you need to either:
* try-catch-demarcate all calls to #import-generated stuff
* use _set_com_error_handler to turn COM errors into MFC exceptions
(IMO best approach by a long stretch, but curiously, this function was
never documented by MS, although it exists since, I dunno, forever)
* use "raw" interfaces only (I personally would never bother with that
PITA)
HTH,
Goran.