Re: MFC ActiveX Control in VC++ by Tom
Tom
Sun Feb 27 18:38:55 CST 2005
IN MFC Project, when you select AddClass in VSIDE then you get two options add the class
from ActiveX control or add the Class from TypeLib (.tlb) file. IN first version VSIDE
will create a wrapper of CWnd class and wrap the CreateControl function in Create
function, where as in the later, it create the class from COleDispatchDriver. When you do
the later process even for the ATL - SimpleObject it does the same thing, but this works
fine in ATL - SimpleObject, but not working when I create the same ActiveX control with
MFC ActiveX (.ocx) file wizard.
Here is the complete class created by the VSIDE In this both type of functions AddVal and
AddNum gives me Catastrophic Failuer exception error.
------------------------------------------------------------------------------------------
-------------------------
class IMath : public COleDispatchDriver
{
public:
IMath(){} // Calls COleDispatchDriver default constructor
IMath(LPDISPATCH pDispatch) : COleDispatchDriver(pDispatch) {}
IMath(const IMath& dispatchSrc) : COleDispatchDriver(dispatchSrc) {}
// Attributes
public:
// Operations
public:
// IMath methods
public:
long AddVal(long v1, long v2)
{
long result;
static BYTE parms[] = VTS_I4 VTS_I4 ;
InvokeHelper(0x1, DISPATCH_METHOD, VT_I4, (void*)&result, parms, v1, v2);
return result;
}
STDMETHOD(AddNum)(long v1, long v2, long * retVal)
{
HRESULT result;
static BYTE parms[] = VTS_I4 VTS_I4 VTS_PI4 ;
InvokeHelper(0x2, DISPATCH_METHOD, VT_HRESULT, (void*)&result, parms, v1, v2, retVal);
return result;
}
// IMath properties
public:
};
------------------------------------------------------------------------------------------
--------------------------
"Larry Brasfield" <donotspam_larry_brasfield@hotmail.com> wrote in message
news:Olch6eSHFHA.580@TK2MSFTNGP15.phx.gbl...
> [Top-posting undone and reformatted for clarity.]
> "Tom Alter" <Lord2702@MSN.com> wrote in message
> news:%23n$ChpRHFHA.400@TK2MSFTNGP14.phx.gbl...
> > "Larry Brasfield" <donotspam_larry_brasfield@hotmail.com> wrote in message
> > news:%23wjWqcRHFHA.3376@TK2MSFTNGP14.phx.gbl...
> >> "Tom Alter" <Lord2702@MSN.com> wrote in message
> >> news:%238NPLFRHFHA.3352@TK2MSFTNGP10.phx.gbl...
> >> > I am using MS VSIDE 2003. I want to create a simple MFC ActiveX control. I create
the
> >> > control as follows, but it gives me Catastrophic Error. Please let me know where I
am
> >> > doing wrong.
> >> >
> >> > I create the project, name is SMath, the name of the control. I select "Invisible
at
> >> > runtime" checked, and all other options unchecked. After creating the project, in
> >> > the Class View write click on the Interface node, and add the method, say
> >> > LONG AddVal(LONG v1, LONG v2); class wizard create the method, and the
> >> > method in the control defination, I just add the code...
> >> > return v1 + v2;
> >> > and compile the project. I also create the test project and add the class created
> >> > from the SMath typeLib. After this created the instance, and the CreateDispatch,
> >> > properly. CreateDispatch works correct, and not throwing any error, and returns
> >> > 1 as success. When I try to use the method on the control it gives me the
exception,
> >> > and error says, "Catastrophic Error". Am I really doing catastrophically? I create
> >> > the same project in ATL by adding the simple object and it works perfect. I also
> >> > add the AddVal method, as follows...
> >> > HRESULT AddVal(LONG v1, LONG v2, LONG *retVal); with appropriate
> >> > code. But still it gives me the same error.
> >> >
> >> > I appreciate your help. If the steps that I am using while creating the project are
> >> > not correct, then please provide the steps. I want absolutely a simple control, w/o
> >> > window. One more thing, I test my control in the ActiveX Test Container, and it
> >> > works fine, invokes method correctly, but in the test project it is not working.
> >>
> >> From your description, it is difficult to see what is
> >> going wrong, but from all your words about AddVal(...)
> >> I guess that you get some kind of fault when calling
> >> that, I guess through the IDispatch interface.
> >>
> >> One clue may be in the difference in behavior in
> >> different contexts. Ultimately, the same method
> >> should be called in either case. You might want
> >> to verify that in the debugger.
> >>
> >> It will help immensely if you show the IDL for that
> >> method (the one that faults), a reduced version of
> >> your implementation that still crashes, and the calling
> >> code sequence.
>
> >
> > Hello Larry:
> > Thanks for your reply. Here is the code in IDL.
>
> That would be 1 of the 3 items I claimed would help.
>
> > If I create a control instead of typelib
> > class, then it works fine,
>
> I'm sorry, but I do not know what a "typelib class" is.
> My expertise, (such as it is), lies in the mechanics of
> COM and Automation, not the diverse lingo that has
> arisen around it. Do you mean simply an IDispatch
> interface implemented by handing an ITypInfo object
> to CreateStdDispatch()?
>
> > but I don't want to wrap my ActiveX control in CWnd class,
> > which wraps the CreateControl in Create method, this has another headache of using the
> > resource ID for the control. If you see my explanation about the ATL ActiveX with
> > SimpleObject, and then creating the typeLib class from it I want to use the same
method
> > with MFC ActiveX control, and I do not want to drop the control on to th form or
Dialog.
> > ------------------ IDL snippet for the function -------------------------
> > // Primary dispatch interface for CSMathCtrl
> >
> > [ uuid(E60ADA69-1F96-4984-ABA2-7454AD97DF26),
> > helpstring("Dispatch interface for SMath Control")]
> > dispinterface _DSMath
> > {
> > properties:
> > methods:
> >
> > [id(DISPID_ABOUTBOX)] void AboutBox();
> > [id(1), helpstring("method AddNum")] LONG AddNum(LONG v1, LONG v2);
> > };
> > ------------------------------------------------------------------------
>
> It would be a good idea, (in addition to the missing 2 items
> already mentioned), to show what COM threading model you
> are using and, if not free-threaded, how the calling thread
> relates to the CoInitialize(Ex)() thread.
>
> --
> --Larry Brasfield
> email: donotspam_larry_brasfield@hotmail.com
> Above views may belong only to me.
>
>