hWnd in COM Question.
-
Thanks. I will first try to pass the parent HWND in and try get things working that way. Then I will attempt the QI of the container. I suppose that underneath the hood that is what MessageBox and AfxMessageBox does, yes? Matt (Padawan Learner)
No, they take an HWND (as you're doing in your code) or, if NULL, use 0 (the desktop's HWND - the top-level window). Keep in mind that the container must implement the interface your QI'ing for, and that it has to give you a pointer to it somehow (
IOBjectWithSite
is but one of many ways - including the infinite possibilities that you can define). Again, the container has to support QI'ing for that interface. The best thing is too support various OLE interfaces when appropriate. Most containers like IE, Word, Excel, etc., support these interfaces. It's all basically speghetti work - interfaces implemented and supported strewn all over and the container and the component must understand each other somehow. Many times, ActiveX controls will support several interfaces that accomplish the same thing (or at least share a common subset) so that they can be contained in various containers taking a chance that the containers support at least one of the interfaces the ActiveX control implements. If you are using a Microsoft container, there should be documentation in the PSDK about what interfaces it supports. If it's a non-Microsoft container but still third-party to you, I would hope they would have similar documentation. If you wrote it, then make it support whatever (either pre-defined or define your own) you like. If it can contain practically any ActiveX control, again looking at and implement the various OLE interfaces,IPersist*
interfaces, and various others is always safe. If it's pretty much a custom solution, the latter suggestion would probably be overkill.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
The cool thing about COM is that you don't have to worry about implementation (i.e., VC++ vs. VB vs. et al.). The bad thing about COM is that you don't have to worry about implementation! The important thing is to get an HWND through a COM-friendly manner. Your
CNormalDlg
needs a parent HWND (or application frame HWND). You can either design your class so that you pass thatwndParent
to its constructor or something (to associate the parent HWND; this is a fairly common practice). Or, in the spirit of .NET, you can QI the container for various OLE-defined or user-defined (you and / or your team) interface, likeIOleWindow
(common for ActiveX UI components). It has a methodGetWindow
that returns (via a retval) the HWND. How you QI the parent is another store, but usually has to do with implementingIOBjectWithSite
or something similar (the container should QI for this and callSetSite
to pass itsIUnknown
interface that you would hold on to in order to QI it later, forIOleWindow
or something). Of course, the container has to implement / support these interfaces. COM is all about contracts, and the "terms" of those contracts is up to the containers and components - you don't get all the functionality for free! So, either of these methods (either passing the parent HWND or QI'ing for an interface to get the parent HWND) are pretty common. I used a similar QI when I wrote an IE BHO that QI'd the site for theIWebBrowser2
interface then callget_HWND
to get its HWND. I then used that as the parent HWND when displaying dialogs and what-not.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
Getting closer, but no cigar yet. I passed in the parent like so:
void __stdcall CMattDlgCmpnt::HtmlMsgBox( HWND wndParent, BSTR strmsg ) { CWnd* pWnd = new CWnd(); pWnd = CWnd::FromHandle(wndParent); CNormalDlg *wndNormalDlg = new CNormalDlg( pWnd ); wndNormalDlg->DoModal(); }
It seems that the class is failing to create its own m_hWnd: {CNormalDlg hWnd=0x00000000} This may be a question for the C++ board but if you want to tkae a shot at it I'd appreciate it. Thanks. Matt (Padawan Learner) -
Getting closer, but no cigar yet. I passed in the parent like so:
void __stdcall CMattDlgCmpnt::HtmlMsgBox( HWND wndParent, BSTR strmsg ) { CWnd* pWnd = new CWnd(); pWnd = CWnd::FromHandle(wndParent); CNormalDlg *wndNormalDlg = new CNormalDlg( pWnd ); wndNormalDlg->DoModal(); }
It seems that the class is failing to create its own m_hWnd: {CNormalDlg hWnd=0x00000000} This may be a question for the C++ board but if you want to tkae a shot at it I'd appreciate it. Thanks. Matt (Padawan Learner)Hi , I suggest u too create ur dialog using ATL CAxDialogImpl Temnplate class, for this u just have to go to insert menu , click New ATL Object , under Miscleneaous button u will find Dialog , insert into ur project, Actually in ATL contains several template classes for allowing u to use window. using CAxDialogImpl this template class u can easily create model or modeless dialog box. Just by making ur dialog class object and DoModal with it :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
-
Getting closer, but no cigar yet. I passed in the parent like so:
void __stdcall CMattDlgCmpnt::HtmlMsgBox( HWND wndParent, BSTR strmsg ) { CWnd* pWnd = new CWnd(); pWnd = CWnd::FromHandle(wndParent); CNormalDlg *wndNormalDlg = new CNormalDlg( pWnd ); wndNormalDlg->DoModal(); }
It seems that the class is failing to create its own m_hWnd: {CNormalDlg hWnd=0x00000000} This may be a question for the C++ board but if you want to tkae a shot at it I'd appreciate it. Thanks. Matt (Padawan Learner)Just pass the
HWND
to yourCNormalDlg
constructor. Inside the constructor when you either use a wrapper class or create the window manually (viaRegisterWindow
thenCreateWindow
, or their extended equivalents) you pass the parentHWND
that was passed to your constructor. If this is an MFC class - and it appears to be - and you're deriving fromCDialog
- and your class name indicates such - then call the base class constructor forCDialog
which takes a parentCWnd
. Don't forget to call the base class constructor, though.-----BEGIN GEEK CODE BLOCK----- Version: 3.21 GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++ -----END GEEK CODE BLOCK-----
-
Hi , I suggest u too create ur dialog using ATL CAxDialogImpl Temnplate class, for this u just have to go to insert menu , click New ATL Object , under Miscleneaous button u will find Dialog , insert into ur project, Actually in ATL contains several template classes for allowing u to use window. using CAxDialogImpl this template class u can easily create model or modeless dialog box. Just by making ur dialog class object and DoModal with it :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
I tried to add ATL to my dll but it says that ATL can only be added to regular MFC dll. Mine is an extension dll. Thanks for all the help guys, but I think I am in over my head here. I thought that once I got the basics down I could jump right and use MFC. All the books I have don't show any examples similar to what I am doing. I ordered some books on COM, MFC, and ATL today... Matt (Padawan Learner)
-
Hi , I suggest u too create ur dialog using ATL CAxDialogImpl Temnplate class, for this u just have to go to insert menu , click New ATL Object , under Miscleneaous button u will find Dialog , insert into ur project, Actually in ATL contains several template classes for allowing u to use window. using CAxDialogImpl this template class u can easily create model or modeless dialog box. Just by making ur dialog class object and DoModal with it :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
Hello Srivastava Thanks a lot for ur help . I am doing a outlook addin project which i found from the cp site http://www.codeproject.com/com/outlookaddin.asp?target=office2k and when we execute and register we get two buttons added to the outlook's bar . when we click it we get a message box only . so i need to add a dialog so i found ur reply to a person demanding the same thing ...i did derive a calss from CDialog and tried to DoModal() it but it simple did not work so i looked into ur advise and did the following... Insert new ATL --> Miscelaneous --->Dialog ...simply here i gave a new dialog class name and got a dialog class derived from CAxDialogImpl ..and i did the following ... added a memeber of this class Cnewdailog :public CAxDialogImpl and in the CAddin class i addeed Cnewdialog *dlg; and in the OnButtonClick() of cpp file { dlg=new Cnewdialog(); HWND hwnd = ::GetActiveWindow(); dlg->DoModal(hwnd,NULL); } ...now superb when i click the addin buttons on the outlook 's bar i get this dialog fired ...but the problem is i am not able to add members to the controls of the dialog ...coz i want to get some user name , password to be retrived from the dialog so that i could use them to check against a DB table ...but simple i am not able to use thye classwizard to get memebrs for dialog's controls as we do it normal MFC apps ...but my app is ATL COM with MFC support ...so to be precise i have three edit boxes in the CAxDialogImpl dilaog box ( cnewdialog) ...these three's valuses are to be retrived from the control when we DoModal()==IDOK ...ok so please help me out how i could do it ... thanks a lot withregards dharani
-
Hello Srivastava Thanks a lot for ur help . I am doing a outlook addin project which i found from the cp site http://www.codeproject.com/com/outlookaddin.asp?target=office2k and when we execute and register we get two buttons added to the outlook's bar . when we click it we get a message box only . so i need to add a dialog so i found ur reply to a person demanding the same thing ...i did derive a calss from CDialog and tried to DoModal() it but it simple did not work so i looked into ur advise and did the following... Insert new ATL --> Miscelaneous --->Dialog ...simply here i gave a new dialog class name and got a dialog class derived from CAxDialogImpl ..and i did the following ... added a memeber of this class Cnewdailog :public CAxDialogImpl and in the CAddin class i addeed Cnewdialog *dlg; and in the OnButtonClick() of cpp file { dlg=new Cnewdialog(); HWND hwnd = ::GetActiveWindow(); dlg->DoModal(hwnd,NULL); } ...now superb when i click the addin buttons on the outlook 's bar i get this dialog fired ...but the problem is i am not able to add members to the controls of the dialog ...coz i want to get some user name , password to be retrived from the dialog so that i could use them to check against a DB table ...but simple i am not able to use thye classwizard to get memebrs for dialog's controls as we do it normal MFC apps ...but my app is ATL COM with MFC support ...so to be precise i have three edit boxes in the CAxDialogImpl dilaog box ( cnewdialog) ...these three's valuses are to be retrived from the control when we DoModal()==IDOK ...ok so please help me out how i could do it ... thanks a lot withregards dharani
Hi :) Actually MFC wizard wont work in ATL case . so u have to manually code to get the , instance of the control placed on ur dialog box. u can use GetDlgItem(UINT nID) API for that in MFC this API returns the CWnd * , whic u can type cast into ur Control Class, but in ATL this API returns u the HWND of that control placed at ur Dialogbox. So do the follwoing steps inside your ATL (CAxDialogImpl Derived) DialogBox class HWND hwnd1=GetDlgItem(IDC_EDIT1), where IDC_EDIT1 , is the Resource ID of ur control . CEdit edit1; edit1.Attach(hwnd1); now this CEdit object,can directly advert to ur Edit box placed on ur dialog box now u can use CEdit class methods to get or set the text like CString str edit1.GetWindowText(str) , or edit1.SetWindowText("hello"); This will allow u to access ur control completely Regards :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
-
Hi :) Actually MFC wizard wont work in ATL case . so u have to manually code to get the , instance of the control placed on ur dialog box. u can use GetDlgItem(UINT nID) API for that in MFC this API returns the CWnd * , whic u can type cast into ur Control Class, but in ATL this API returns u the HWND of that control placed at ur Dialogbox. So do the follwoing steps inside your ATL (CAxDialogImpl Derived) DialogBox class HWND hwnd1=GetDlgItem(IDC_EDIT1), where IDC_EDIT1 , is the Resource ID of ur control . CEdit edit1; edit1.Attach(hwnd1); now this CEdit object,can directly advert to ur Edit box placed on ur dialog box now u can use CEdit class methods to get or set the text like CString str edit1.GetWindowText(str) , or edit1.SetWindowText("hello"); This will allow u to access ur control completely Regards :) Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
hi sri Thanks a lot . I did the following . as i am building a release version i wanted to check whether the control's text is retrived or not . so i wanted to display a text right before DoModal so i did the follwoing ... ndlg=new CNewDialog(); HWND wnd=::GetActiveWindow(); HWND h=ndlg->GetDlgItem(IDC_NAME); CEdit *e=new CEdit(); // i treid with simple CEdit e; then switched to CEdit* e e->Attach(h); CString s="hello"; e->SetWindowText(_T("hello")); ndlg->DoModal(wnd,NULL); but the text hello is not displayed in the control ..aslo i tried the following to test the retrivel of text from edit box ,.... if(ndlg->Domodl(wnd,NULL)==IDOK) { HWND wd=ndlg->GetDlgItem(IDC_NAME); CEdit ned; ned.Attach(wd); CString str; ned.GetWindowText(str); //AfxMessageBox(str,0,0); MessageBox(NULL, str, _T("OnClickButton2"), MB_OK); } but the message box is not displaying the retrived string --str -. so i am still confused what i could do ... thanks for u r advise bye dharani
-
hi sri Thanks a lot . I did the following . as i am building a release version i wanted to check whether the control's text is retrived or not . so i wanted to display a text right before DoModal so i did the follwoing ... ndlg=new CNewDialog(); HWND wnd=::GetActiveWindow(); HWND h=ndlg->GetDlgItem(IDC_NAME); CEdit *e=new CEdit(); // i treid with simple CEdit e; then switched to CEdit* e e->Attach(h); CString s="hello"; e->SetWindowText(_T("hello")); ndlg->DoModal(wnd,NULL); but the text hello is not displayed in the control ..aslo i tried the following to test the retrivel of text from edit box ,.... if(ndlg->Domodl(wnd,NULL)==IDOK) { HWND wd=ndlg->GetDlgItem(IDC_NAME); CEdit ned; ned.Attach(wd); CString str; ned.GetWindowText(str); //AfxMessageBox(str,0,0); MessageBox(NULL, str, _T("OnClickButton2"), MB_OK); } but the message box is not displaying the retrived string --str -. so i am still confused what i could do ... thanks for u r advise bye dharani
Hi there, Actually , u r commiting a mistake, U have tried to get the Window handle of your edit box before Doing DOModal, that means , ur controls are still not created ndlg=new CNewDialog(); HWND wnd=::GetActiveWindow(); HWND h=ndlg->GetDlgItem(IDC_NAME); CEdit *e=new CEdit(); // i treid with simple CEdit e; then switched to CEdit* e e->Attach(h); CString s="hello"; e->SetWindowText(_T("hello")); in the above code , u have just created an instance of your dialog class, but controls are still not created , so setting text wil not work and the same thing u r doing after DoModal when OK Button is pressed ur dialog box is again closed ,and for that reason ur Control Box have also left Memory , i mean now they are not there , so when u try to get text when Dialog is closed from the EDit box will not work again. So try to do these things in your Dilaog class , i mean try to set text, in InitDialog Method of your dialog class, try to create some member variable in your class , get The text of ur edit box in OnOK method and then u will be able to access I Hope I am making myself clear!! Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
-
Hi there, Actually , u r commiting a mistake, U have tried to get the Window handle of your edit box before Doing DOModal, that means , ur controls are still not created ndlg=new CNewDialog(); HWND wnd=::GetActiveWindow(); HWND h=ndlg->GetDlgItem(IDC_NAME); CEdit *e=new CEdit(); // i treid with simple CEdit e; then switched to CEdit* e e->Attach(h); CString s="hello"; e->SetWindowText(_T("hello")); in the above code , u have just created an instance of your dialog class, but controls are still not created , so setting text wil not work and the same thing u r doing after DoModal when OK Button is pressed ur dialog box is again closed ,and for that reason ur Control Box have also left Memory , i mean now they are not there , so when u try to get text when Dialog is closed from the EDit box will not work again. So try to do these things in your Dilaog class , i mean try to set text, in InitDialog Method of your dialog class, try to create some member variable in your class , get The text of ur edit box in OnOK method and then u will be able to access I Hope I am making myself clear!! Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
hi srivarstava exactly i did it before u mailed me !!!! thanks a lot !!! what i did was i opened the CNewDialog.h and added the folowoing in the onOk handler ( ur right after OnOk the dialog leaves memory !! so how can i get the control data persistent ? thats a mistake ) .. LRESULT OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled) { HWND wnd=GetDlgItem(IDC_NAME); CEdit ed; ed.Attach(wnd); CString str; ed.GetWindowText(str); st=str; // this st is a member i added to the class newdialog coz i am able to access it from outside the class by typing CString mystr= dlg-->st ..cool.. EndDialog(wID); return 0; } now i got i t... thanks for ur continued support ...and one more thing ...how can i add a Explorer bar to be fired when i click button ?? I want to open a site www.google.com when i click a button ...and one more thing ...i have office 9 so that in the atdafx.h i am hardcoding the dlls needed as #import "C:\Program Files\Microsoft Office\Office\mso9.dll" rename_namespace("Office"), named_guids using namespace Office; #import "C:\Program Files\Microsoft Office\Office\MSOUTL9.olb" rename_namespace("Outlook"), named_guids, raw_interfaces_only using namespace Outlook; but the same dll is not ok for office2002 ..it uses outlook 10 ..so i need to have a functionlaity withwhich i can find whihc version of dll is present in the registry and /..and i have to accoringly #import the necessary dll ( like mso10.dll and msoutl10.dll ) ..so help me in this regard also ...one more ..i am going toadd a nwe Creordset class from this ATL COM proj ...any advise on possible prob i will face when i link with DB ?? thanks a lotttt with regards dharani
-
I am stumbling my way through COM and have hit a snag. I can get a MessageBox to work from a call to my component but I cannot get a CDialog derived class to display. Every thing runs fine but the CDialog derived class has hWnd=0xcccccccc which means nothing happens when I call DoModal(). I am using a MFC extension dll to serve up the components. What gives? Thanks for your time.
void __stdcall CMattDlgCmpnt::SimpleMsgBox( HWND wndParent, BSTR strmsg ) { ... //This Works MessageBox ( wndParent, strTxt, _T("Simple Message Box"), MB_OK ); return; } void __stdcall CMattDlgCmpnt::HtmlMsgBox( HWND wndParent, BSTR strmsg ) { CNormalDlg wndNormalDlg; //This does not work wndNormalDlg.DoModal(); //hWnd=0xcccccccc Nothing Happens }
Matt (Padawan Learner)i guess i have answered your this question in some other thread... this Line should solve ur problem. Just put it at the start of each method where you are invoking dialogs, basically every where if you are using MFC in COM AFX_MANAGE_STATE(AfxGetStaticModuleState( )); The World is getting smaller and so are the people.
-
I am stumbling my way through COM and have hit a snag. I can get a MessageBox to work from a call to my component but I cannot get a CDialog derived class to display. Every thing runs fine but the CDialog derived class has hWnd=0xcccccccc which means nothing happens when I call DoModal(). I am using a MFC extension dll to serve up the components. What gives? Thanks for your time.
void __stdcall CMattDlgCmpnt::SimpleMsgBox( HWND wndParent, BSTR strmsg ) { ... //This Works MessageBox ( wndParent, strTxt, _T("Simple Message Box"), MB_OK ); return; } void __stdcall CMattDlgCmpnt::HtmlMsgBox( HWND wndParent, BSTR strmsg ) { CNormalDlg wndNormalDlg; //This does not work wndNormalDlg.DoModal(); //hWnd=0xcccccccc Nothing Happens }
Matt (Padawan Learner) -
Hi there, Actually , u r commiting a mistake, U have tried to get the Window handle of your edit box before Doing DOModal, that means , ur controls are still not created ndlg=new CNewDialog(); HWND wnd=::GetActiveWindow(); HWND h=ndlg->GetDlgItem(IDC_NAME); CEdit *e=new CEdit(); // i treid with simple CEdit e; then switched to CEdit* e e->Attach(h); CString s="hello"; e->SetWindowText(_T("hello")); in the above code , u have just created an instance of your dialog class, but controls are still not created , so setting text wil not work and the same thing u r doing after DoModal when OK Button is pressed ur dialog box is again closed ,and for that reason ur Control Box have also left Memory , i mean now they are not there , so when u try to get text when Dialog is closed from the EDit box will not work again. So try to do these things in your Dilaog class , i mean try to set text, in InitDialog Method of your dialog class, try to create some member variable in your class , get The text of ur edit box in OnOK method and then u will be able to access I Hope I am making myself clear!! Abhishek Srivastava Software Engg (VC++) India ,Noida Mobile no 9891492921 :)
Hi I have one tiny question? Wich header file do I must include for CEdit object? I included but I get an error ***\VC98\MFC\INCLUDE\afxv_w32.h(14) : fatal error C1189: #error : WINDOWS.H already included. MFC apps must not #include If I dont include I get error of undefined member 'CEdit' : missing storage-class or type specifiers Tomaz Rotovnik