Re: MFC ActiveX Control in VC++

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: Larry Brasfield (donotspam_larry_brasfield_at_hotmail.com)
Date: 02/27/05


Date: Sun, 27 Feb 2005 15:55:04 -0800


[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. 


Relevant Pages

  • Re: MFC ActiveX Control in VC++
    ... I want to create a simple MFC ActiveX control. ... but in the test project it is not working. ... I guess that you get some kind of fault when calling ...
    (microsoft.public.vc.language)
  • Re: When is Invoke() Really Required?
    ... then it is safe to call that method. ... in the context of the calling thread, now if the calling thread does not ... > Invoke, BeginInvoke, EndInvoke and CreateGraphics. ... Does this apply for absolutely every method or property in a control ...
    (microsoft.public.dotnet.framework.windowsforms.controls)
  • Re: Pass Control as Argument
    ... The Call keyword, while optional, can help you avoid problems associated with those inconsistencies. ... because I really wasn't calling like I posted. ... > All of a suddent this code now passes the ctl argument as the value of> the ... > control) and the result is the same. ...
    (microsoft.public.access.modulesdaovba)
  • Re: Pass Control as Argument
    ... No broken refs ... because I really wasn't calling like I posted. ... Function SelectAnItem(By Ref ctl as control, ... All of a suddent this code now passes the ctl argument as the value of the ...
    (microsoft.public.access.modulesdaovba)
  • RE: Control Step Question
    ... the code it is calling requires it to have focus on the OnCurrent, ... Dim ctrl As Control ... Dim StatusNumber As Long ... Change Set ctrl to Set ctrl = FieldName after declaring it as a string and ...
    (microsoft.public.access.formscoding)