Using ActiveX wrapper class in MFC console
-
Previously, I have an ActiveX control which I use it in my MFC GUI program. The wrapper class (inheritanced from CWnd) for the activex control is generated automatically through class wizard. Everything just work fine. Now, I need my application to work in non-gui mode. Hence, no dialog or windows will be created to host the active x control. I just copy the ActiveX wrapper class file, which is generated previously, into my console project. i realize that a runtime error will occur whenever i try to use the active x control. this is because there is no dialog/ or windows to host that activex control. I read an article at http://www.codeproject.com/com/consoleactivex.asp The author shows the way by using COM. However, i am not familiar with COM. I prefer to use back the wizard generated wrapper source code, which is much more easier. may i noe how can i solve this problem? do i need to created a hidden window for it? or is there better way? thanks! cheok
-
Previously, I have an ActiveX control which I use it in my MFC GUI program. The wrapper class (inheritanced from CWnd) for the activex control is generated automatically through class wizard. Everything just work fine. Now, I need my application to work in non-gui mode. Hence, no dialog or windows will be created to host the active x control. I just copy the ActiveX wrapper class file, which is generated previously, into my console project. i realize that a runtime error will occur whenever i try to use the active x control. this is because there is no dialog/ or windows to host that activex control. I read an article at http://www.codeproject.com/com/consoleactivex.asp The author shows the way by using COM. However, i am not familiar with COM. I prefer to use back the wizard generated wrapper source code, which is much more easier. may i noe how can i solve this problem? do i need to created a hidden window for it? or is there better way? thanks! cheok
Use the class wizard to generate a wrapper class, not derived from CWnd, but from COleDispatchDriver. In ClassWizard click Add Class then select From a type libary and select the .tlb file of your control. After you have the wrapper class call CoCreateInstance to create the object, something like: IDispatch* pDisp; CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, dispid, &pDisp); where clsid is the class id for the object, dispid is the ID of object's IDispatch interface After a successful call of CoCreateInstance use the AttachDispatch method from the wrapper class and then you can call ActiveX methods, like this: CWrapperClass myDisp; myDisp.AttachDispatch(pDisp); myDisp.SomeActiveXMethod(); Remember to call CoInitialize before CoCreateInstance and when you're done using the object call pDisp->Release() in order to decrement the object's reference count and destroy it.
-
Use the class wizard to generate a wrapper class, not derived from CWnd, but from COleDispatchDriver. In ClassWizard click Add Class then select From a type libary and select the .tlb file of your control. After you have the wrapper class call CoCreateInstance to create the object, something like: IDispatch* pDisp; CoCreateInstance(clsid, NULL, CLSCTX_INPROC_SERVER, dispid, &pDisp); where clsid is the class id for the object, dispid is the ID of object's IDispatch interface After a successful call of CoCreateInstance use the AttachDispatch method from the wrapper class and then you can call ActiveX methods, like this: CWrapperClass myDisp; myDisp.AttachDispatch(pDisp); myDisp.SomeActiveXMethod(); Remember to call CoInitialize before CoCreateInstance and when you're done using the object call pDisp->Release() in order to decrement the object's reference count and destroy it.
Thanks. Using COleDispatchDriver as parent class really work! I didn't use AttachDispatch since I cann't make it works. Most probably I miss out something. However, by using CreateDispatch just rock :) I assume there is no clean up needed after CreateDispatch from the MSDN example. Thanks for help! cheok