Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. COM
  4. hWnd in COM Question.

hWnd in COM Question.

Scheduled Pinned Locked Moved COM
questionc++com
16 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • H Heath Stewart

    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 that wndParent 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, like IOleWindow (common for ActiveX UI components). It has a method GetWindow that returns (via a retval) the HWND. How you QI the parent is another store, but usually has to do with implementing IOBjectWithSite or something similar (the container should QI for this and call SetSite to pass its IUnknown interface that you would hold on to in order to QI it later, for IOleWindow 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 the IWebBrowser2 interface then call get_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-----

    M Offline
    M Offline
    Matt Gates
    wrote on last edited by
    #5

    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)

    A H 2 Replies Last reply
    0
    • M Matt Gates

      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)

      A Offline
      A Offline
      Abhishek Srivastava
      wrote on last edited by
      #6

      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 :)

      M D 2 Replies Last reply
      0
      • M Matt Gates

        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)

        H Offline
        H Offline
        Heath Stewart
        wrote on last edited by
        #7

        Just pass the HWND to your CNormalDlg constructor. Inside the constructor when you either use a wrapper class or create the window manually (via RegisterWindow then CreateWindow, or their extended equivalents) you pass the parent HWND that was passed to your constructor. If this is an MFC class - and it appears to be - and you're deriving from CDialog - and your class name indicates such - then call the base class constructor for CDialog which takes a parent CWnd. 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-----

        1 Reply Last reply
        0
        • A Abhishek Srivastava

          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 :)

          M Offline
          M Offline
          Matt Gates
          wrote on last edited by
          #8

          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)

          1 Reply Last reply
          0
          • A Abhishek Srivastava

            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 :)

            D Offline
            D Offline
            dharani
            wrote on last edited by
            #9

            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

            A 1 Reply Last reply
            0
            • D 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

              A Offline
              A Offline
              Abhishek Srivastava
              wrote on last edited by
              #10

              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 :)

              D 1 Reply Last reply
              0
              • A Abhishek Srivastava

                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 :)

                D Offline
                D Offline
                dharani
                wrote on last edited by
                #11

                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

                A 1 Reply Last reply
                0
                • D 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

                  A Offline
                  A Offline
                  Abhishek Srivastava
                  wrote on last edited by
                  #12

                  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 :)

                  D T 2 Replies Last reply
                  0
                  • A Abhishek Srivastava

                    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 :)

                    D Offline
                    D Offline
                    dharani
                    wrote on last edited by
                    #13

                    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

                    1 Reply Last reply
                    0
                    • M Matt Gates

                      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)

                      P Offline
                      P Offline
                      Prakash Nadar
                      wrote on last edited by
                      #14

                      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.

                      1 Reply Last reply
                      0
                      • M Matt Gates

                        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)

                        A Offline
                        A Offline
                        Anonymous
                        wrote on last edited by
                        #15

                        missing AFX_MANAGE_STATE(AfxGetStaticModuleState()) ???:confused:

                        1 Reply Last reply
                        0
                        • A Abhishek Srivastava

                          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 :)

                          T Offline
                          T Offline
                          Tomaz Rotovnik
                          wrote on last edited by
                          #16

                          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

                          1 Reply Last reply
                          0
                          Reply
                          • Reply as topic
                          Log in to reply
                          • Oldest to Newest
                          • Newest to Oldest
                          • Most Votes


                          • Login

                          • Don't have an account? Register

                          • Login or register to search.
                          • First post
                            Last post
                          0
                          • Categories
                          • Recent
                          • Tags
                          • Popular
                          • World
                          • Users
                          • Groups