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. C / C++ / MFC
  4. How to get the HWND of mfc DialogBox for .Net

How to get the HWND of mfc DialogBox for .Net

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++helptutorialquestion
37 Posts 3 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.
  • L LaHaHa

    In fact that the function code is only like this: HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), TEXT("DirectInput Sample"), MB_ICONERROR | MB_OK ); return 0; return S_OK; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; So how to solve this problem! Please help!

    P Offline
    P Offline
    prasad_som
    wrote on last edited by
    #14

    Come on ! Where is dialog class instance ? If dialog is already invoked then use AfxGetMainWnd, to get its handle.

    LaHaHa wrote:

    if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr;

    g_pJoystick->SetCooperativeLevel(AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )

    Prasad Notifier using ATL | Operator new[],delete[][^]

    L 1 Reply Last reply
    0
    • P prasad_som

      Come on ! Where is dialog class instance ? If dialog is already invoked then use AfxGetMainWnd, to get its handle.

      LaHaHa wrote:

      if( FAILED( hr = g_pJoystick->SetCooperativeLevel(this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr;

      g_pJoystick->SetCooperativeLevel(AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )

      Prasad Notifier using ATL | Operator new[],delete[][^]

      L Offline
      L Offline
      LaHaHa
      wrote on last edited by
      #15

      I got this one now! error C2664: 'IDirectInputDevice8A::SetCooperativeLevel' : cannot convert parameter 1 from 'CWnd *' to 'HWND' Please help!

      P M 2 Replies Last reply
      0
      • L LaHaHa

        I got this one now! error C2664: 'IDirectInputDevice8A::SetCooperativeLevel' : cannot convert parameter 1 from 'CWnd *' to 'HWND' Please help!

        P Offline
        P Offline
        prasad_som
        wrote on last edited by
        #16

        You can get its handle by type-casting it. like

        SetCooperativeLevel((HWND)(AfxGetMainWnd()),..);

        Prasad Notifier using ATL | Operator new[],delete[][^]

        L M 2 Replies Last reply
        0
        • P prasad_som

          You can get its handle by type-casting it. like

          SetCooperativeLevel((HWND)(AfxGetMainWnd()),..);

          Prasad Notifier using ATL | Operator new[],delete[][^]

          L Offline
          L Offline
          LaHaHa
          wrote on last edited by
          #17

          I will try this, I will tell you the result later! Many thanks!

          1 Reply Last reply
          0
          • L LaHaHa

            I got this one now! error C2664: 'IDirectInputDevice8A::SetCooperativeLevel' : cannot convert parameter 1 from 'CWnd *' to 'HWND' Please help!

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #18

            You need to force it to use the overloaded (HWND) casting operator... For the main window's HWND: g_pJoystick->SetCooperativeLevel(*AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) For the window's HWND (if this is called within a CWnd-derived class): g_pJoystick->SetCooperativeLevel(*this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) Note the asterisks dereferencing the CWnd pointers. You can also use this: g_pJoystick->SetCooperativeLevel(AfxGetMainWnd()->GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) or g_pJoystick->SetCooperativeLevel(GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )

            L 1 Reply Last reply
            0
            • P prasad_som

              You can get its handle by type-casting it. like

              SetCooperativeLevel((HWND)(AfxGetMainWnd()),..);

              Prasad Notifier using ATL | Operator new[],delete[][^]

              M Offline
              M Offline
              Mark Salsbery
              wrote on last edited by
              #19

              Try

              SetCooperativeLevel((HWND)*AfxGetMainWnd(),..);

              ;)

              1 Reply Last reply
              0
              • M Mark Salsbery

                You need to force it to use the overloaded (HWND) casting operator... For the main window's HWND: g_pJoystick->SetCooperativeLevel(*AfxGetMainWnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) For the window's HWND (if this is called within a CWnd-derived class): g_pJoystick->SetCooperativeLevel(*this, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) Note the asterisks dereferencing the CWnd pointers. You can also use this: g_pJoystick->SetCooperativeLevel(AfxGetMainWnd()->GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) or g_pJoystick->SetCooperativeLevel(GetSafeHwnd(), DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) )

                L Offline
                L Offline
                LaHaHa
                wrote on last edited by
                #20

                I have tried all, the result are *this and GetSafeHwnd() will get error *AfxGetMainWnd() AfxGetMainWnd()->GetSafeHwnd() both without error, but it will get a E_HANDLE after running the SetCooperativeLevel(...). Please help!

                M 1 Reply Last reply
                0
                • L LaHaHa

                  I have tried all, the result are *this and GetSafeHwnd() will get error *AfxGetMainWnd() AfxGetMainWnd()->GetSafeHwnd() both without error, but it will get a E_HANDLE after running the SetCooperativeLevel(...). Please help!

                  M Offline
                  M Offline
                  Mark Salsbery
                  wrote on last edited by
                  #21

                  I looked back through all your posts and I didn't see where you are making the call from. Can you post the entire function that includes the call to SetCooperativeLevel()?

                  LaHaHa wrote:

                  I have tried all, the result are *this and GetSafeHwnd() will get error

                  Are you getting compiler error or runtime error?

                  L 1 Reply Last reply
                  0
                  • M Mark Salsbery

                    I looked back through all your posts and I didn't see where you are making the call from. Can you post the entire function that includes the call to SetCooperativeLevel()?

                    LaHaHa wrote:

                    I have tried all, the result are *this and GetSafeHwnd() will get error

                    Are you getting compiler error or runtime error?

                    L Offline
                    L Offline
                    LaHaHa
                    wrote on last edited by
                    #22

                    HRESULT InitDirectInput(); Void CT1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default switch (nIDEvent) { case 5: KillTimer(5); if(!FAILED(InitDirectInput())) { . . . HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { // MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), // TEXT("DirectInput Sample"), // MB_ICONERROR | MB_OK ); return 0; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; HWND hDlg=AfxGetMainWnd()->GetSafeHwnd(); if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hDlg, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; if( FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback, (VOID*)hDlg, DIDFT_ALL ) ) ) return hr; return S_OK; } I just call it by OnTimer() function. Please help!

                    M 2 Replies Last reply
                    0
                    • L LaHaHa

                      HRESULT InitDirectInput(); Void CT1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default switch (nIDEvent) { case 5: KillTimer(5); if(!FAILED(InitDirectInput())) { . . . HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { // MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), // TEXT("DirectInput Sample"), // MB_ICONERROR | MB_OK ); return 0; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; HWND hDlg=AfxGetMainWnd()->GetSafeHwnd(); if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hDlg, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; if( FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback, (VOID*)hDlg, DIDFT_ALL ) ) ) return hr; return S_OK; } I just call it by OnTimer() function. Please help!

                      M Offline
                      M Offline
                      Mark Salsbery
                      wrote on last edited by
                      #23

                      Thanks that helps! I would pass the HWND into your InitDirectInput() function.

                      Void CT1Dlg::OnTimer(UINT nIDEvent)
                      {
                      ...
                      if(!FAILED(InitDirectInput(*this)))
                      {
                      ...
                      }
                      ...
                      }

                      HRESULT InitDirectInput(HWND hwndMain)
                      {
                      ...
                      if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hwndMain, DISCL_NONEXCLUSIVE |
                      DISCL_BACKGROUND ) ) )
                      return hr;
                      ...
                      }

                      If your dialog window is wrapped in (a client of) a frame window then you'd need to pass that framewnd's handle. I'm not sure what you're doing overall, but are you sure you want to initialize in response to a timer event? Generally DirectX initializations are done once, you use the functionality, and cleanup when you're done. Mark

                      L 1 Reply Last reply
                      0
                      • M Mark Salsbery

                        Thanks that helps! I would pass the HWND into your InitDirectInput() function.

                        Void CT1Dlg::OnTimer(UINT nIDEvent)
                        {
                        ...
                        if(!FAILED(InitDirectInput(*this)))
                        {
                        ...
                        }
                        ...
                        }

                        HRESULT InitDirectInput(HWND hwndMain)
                        {
                        ...
                        if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hwndMain, DISCL_NONEXCLUSIVE |
                        DISCL_BACKGROUND ) ) )
                        return hr;
                        ...
                        }

                        If your dialog window is wrapped in (a client of) a frame window then you'd need to pass that framewnd's handle. I'm not sure what you're doing overall, but are you sure you want to initialize in response to a timer event? Generally DirectX initializations are done once, you use the functionality, and cleanup when you're done. Mark

                        L Offline
                        L Offline
                        LaHaHa
                        wrote on last edited by
                        #24

                        hr got E_HANDLE again! Please help!

                        M 3 Replies Last reply
                        0
                        • L LaHaHa

                          hr got E_HANDLE again! Please help!

                          M Offline
                          M Offline
                          Mark Salsbery
                          wrote on last edited by
                          #25

                          If you put a breakpoint at the SetCooperativeLevel() call, what's the value of the window handle?

                          1 Reply Last reply
                          0
                          • L LaHaHa

                            hr got E_HANDLE again! Please help!

                            M Offline
                            M Offline
                            Mark Salsbery
                            wrote on last edited by
                            #26

                            Also, what are the properties of your dialog resource? Try setting the dialog's "Overlapped Window" property to true.

                            L 1 Reply Last reply
                            0
                            • M Mark Salsbery

                              Also, what are the properties of your dialog resource? Try setting the dialog's "Overlapped Window" property to true.

                              L Offline
                              L Offline
                              LaHaHa
                              wrote on last edited by
                              #27

                              The setting the dialog's "Overlapped Window" property is changed to true, but the result is the same. g_pJoystick = 0x001672d4 hwndMain = 0x0007059c {unused=???} hr = E_HANDLE Please help!

                              1 Reply Last reply
                              0
                              • L LaHaHa

                                hr got E_HANDLE again! Please help!

                                M Offline
                                M Offline
                                Mark Salsbery
                                wrote on last edited by
                                #28

                                Hmm something else is going wrong. The dialog doesn't have the WS_CHILD style does it? Maybe a dialog is unacceptable as a "top-level window"? Just for the heck of it, maybe try calling your InitDirectInput() from your dialog's WM_INITDIALOG handler.

                                L 1 Reply Last reply
                                0
                                • M Mark Salsbery

                                  Hmm something else is going wrong. The dialog doesn't have the WS_CHILD style does it? Maybe a dialog is unacceptable as a "top-level window"? Just for the heck of it, maybe try calling your InitDirectInput() from your dialog's WM_INITDIALOG handler.

                                  L Offline
                                  L Offline
                                  LaHaHa
                                  wrote on last edited by
                                  #29

                                  I have created the project again. Now it will not get the E_HANDLE. The InitDirectInput(*this) coding is called from OnTimer as before. It can detect the joystick, but it cannot get any data from the joystick. The coding are if(!FAILED(InitDirectInput(*this))) { if( NULL != g_pJoystick ) { HRESULT hr; TCHAR strText[512] = {0}; // Device state text DIJOYSTATE2 js; // DInput joystick state // Poll the device to read the current state hr = g_pJoystick->Poll(); if(FAILED(hr) ) { hr = g_pJoystick->Acquire(); while( hr == DIERR_INPUTLOST ) hr = g_pJoystick->Acquire(); } if( !FAILED( hr = g_pJoystick->GetDeviceState( sizeof(DIJOYSTATE2), &js ) ) ) { for( int i = 0; i < 128; i++ ) { if ( js.rgbButtons[i] & 0x80 ) { TCHAR sz[128]; StringCchPrintf( sz, 128, TEXT("%02d "), i ); StringCchCat( strText, 512, sz ); } } } Please help!

                                  1 Reply Last reply
                                  0
                                  • L LaHaHa

                                    HRESULT InitDirectInput(); Void CT1Dlg::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default switch (nIDEvent) { case 5: KillTimer(5); if(!FAILED(InitDirectInput())) { . . . HRESULT InitDirectInput() { HRESULT hr; if( FAILED( hr = DirectInput8Create( GetModuleHandle(NULL), DIRECTINPUT_VERSION, IID_IDirectInput8, (VOID**)&g_pDI, NULL ) ) ) return hr; if( FAILED( hr = g_pDI->EnumDevices( DI8DEVCLASS_GAMECTRL, EnumJoysticksCallback, NULL, DIEDFL_ATTACHEDONLY ) ) ) return hr; if( NULL == g_pJoystick ) { // MessageBox( NULL, TEXT("Joystick not found. The sample will now exit."), // TEXT("DirectInput Sample"), // MB_ICONERROR | MB_OK ); return 0; } if( FAILED( hr = g_pJoystick->SetDataFormat( &c_dfDIJoystick2 ) ) ) return hr; HWND hDlg=AfxGetMainWnd()->GetSafeHwnd(); if( FAILED( hr = g_pJoystick->SetCooperativeLevel(hDlg, DISCL_NONEXCLUSIVE | DISCL_BACKGROUND ) ) ) return hr; if( FAILED( hr = g_pJoystick->EnumObjects( EnumObjectsCallback, (VOID*)hDlg, DIDFT_ALL ) ) ) return hr; return S_OK; } I just call it by OnTimer() function. Please help!

                                    M Offline
                                    M Offline
                                    Mark Salsbery
                                    wrote on last edited by
                                    #30

                                    I don't see anything except GetDeviceState() can return DIERR_INPUTLOST but Acquire() doesn't.

                                    L 1 Reply Last reply
                                    0
                                    • M Mark Salsbery

                                      I don't see anything except GetDeviceState() can return DIERR_INPUTLOST but Acquire() doesn't.

                                      L Offline
                                      L Offline
                                      LaHaHa
                                      wrote on last edited by
                                      #31

                                      I found that the first Poll() get "Invalid". After the Acquire(), the hr is S_OK. Then if I Poll() it again, it will get a S_False. Why? Please help!

                                      M 3 Replies Last reply
                                      0
                                      • L LaHaHa

                                        I found that the first Poll() get "Invalid". After the Acquire(), the hr is S_OK. Then if I Poll() it again, it will get a S_False. Why? Please help!

                                        M Offline
                                        M Offline
                                        Mark Salsbery
                                        wrote on last edited by
                                        #32

                                        LaHaHa wrote:

                                        I found that the first Poll() get "Invalid". After the Acquire(), the hr is S_OK. Then if I Poll() it again, it will get a S_False. Why?

                                        Poll returns what? DI_OK, DI_NOEFFECT or error DIERR_INPUTLOST, DIERR_NOTACQUIRED, or DIERR_NOTINITIALIZED? You only need to acquire the device once right?

                                        L 1 Reply Last reply
                                        0
                                        • M Mark Salsbery

                                          LaHaHa wrote:

                                          I found that the first Poll() get "Invalid". After the Acquire(), the hr is S_OK. Then if I Poll() it again, it will get a S_False. Why?

                                          Poll returns what? DI_OK, DI_NOEFFECT or error DIERR_INPUTLOST, DIERR_NOTACQUIRED, or DIERR_NOTINITIALIZED? You only need to acquire the device once right?

                                          L Offline
                                          L Offline
                                          LaHaHa
                                          wrote on last edited by
                                          #33

                                          After I put the InitDirectInput(*this) in the OnInitDialog(), it is alright. But my program need to check the joystick to be available periodically. So how can I do it? Please help!

                                          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