Thread: Direct Input shutting down improperly

  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    73

    Direct Input shutting down improperly

    Checklist
    Direct Input object created: YES.
    Direct Input device created: YES.
    Set data format: YES.
    Cooperative level: YES.
    Acquire: OH YEAH.
    Unacquire: NOPE.

    I am getting the DI_NOEFFECT return on the unaquire() call (I.e. device was never acquired in the first place) Obviously it was because I'm using it

    Only one reason why I can think this is happening.
    Code:
    g_InputDevice->SetCooperativeLevel(hWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);
    DISCL_FOREGROUND will automatically cause the program to unacquire the device if the application goes to the background.

    Well.. as far as I know my application isn't going to the background. And even if it is... I'm still getting a crash when shutting down my application.

    Everything deallocated fine before dinput was added.. so its either the direct input object or direct input device causing the crash.

    Once again the code goes up.. everything needed to compile

    Also.. should I not be gathering checking for input all the time.. (i.e. should I put a specific timer on it to only check every second or something? Seems like the constant calls result in a lot of miniscule additions/subtractions for moving objects..

    (147k)
    www.deo.HostVan.com/DinputFailure.zip
    Last edited by Deo; 06-12-2005 at 08:21 PM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    #ifndef CDINPUT
    #define CDINPUT
    
    #define DIRECTINPUT_VERSION 0x0800
    #include <dinput.h>
    
    
    
    
    class CDInput
    {
        protected:
            IDirectInput8 *_pDI8;
        public:
            CDInput() { };
    		CDInput(HINSTANCE hinst) {Init(hinst);};
            ~CDInput() { };
    
            HRESULT Init(HINSTANCE hinst);
    		void    ShutDown(void) {if (_pDI8) _pDI8->Release();};
    		IDirectInput8 *GetInterface(void) {return _pDI8;};
    };
    #endif
    Code:
    #include "CDinput.h"
    
    
    HRESULT CDInput::Init(HINSTANCE hinst)
    {
        //Create IDirectInput8 interface pointer 
        if (FAILED(DirectInput8Create(hinst, 
                                  DIRECTINPUT_VERSION, 
                                  IID_IDirectInput8, 
                                  (void**)&_pDI8, 
                                  NULL)))
        {
            ::MessageBox(0,"Failed to create DirectInput8 interface",0,0);
        }
        return true;
    
    }
    Works fine for me. But this is just a class for the main DirectInput object.

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    73
    u r able to compile my code and run it succesfully?

    I get:

    "Error"
    "Device not acquired"

    which is a custom message box I have set to pop up when my program tries to unaquire the direct input device.. in this case my KEYBOARD.

    Then I get that microsoft error that wants me to send them my error info... This occurs everytime..

    Direct input working fine while program is running and even after I click OK on these warnings windows seems to return to normal.. I have control of the keyboard.

    Compiler says no errors or warnings. but then again its
    Microsoft Toolkit 2003 free compiler... I'm stumped as to why this is occuring everytime..
    Not preventing me from continue my project but a very annoying occurence and certainly not kosher... no idea why this keeps occuring?

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    #include "CDInput.h"
    
    
    
    
    //Helpers
    #define DIKEYDOWN(data,n) (data[n] & 0x80)
    
    
    class CKeyboard
    {
        protected:
            IDirectInput8           *_pDI8;
    		    IDirectInputDevice8     *_pDID8;
        public:
            //So anyone can access our data - otherwise function overhead per use
            //could inline....but
            UCHAR           keystate[256];
            CKeyboard(IDirectInput8 *pDI8) {_pDI8=pDI8;};
            ~CKeyboard() {ShutDown();};
    			
    		void ShutDown(void) {//if (_pDID8) _pDID8->Unacquire();
    							 if (_pDID8) _pDID8->Release();};
        
            HRESULT Init(HWND hwnd);
            HRESULT Update(void);
    
            inline bool KeyDown(UCHAR key)
            {
              if (keystate[key]) 
              {
                return true; 
              } else return false;
            }
            inline bool KeyUp(UCHAR key) 
            {
              if (keystate[key])
              {
                return false;
              } else return true;
            }
    
    
               
    };
    Code:
    #include "CKeyboard.h"
    
    
    
    HRESULT CKeyboard::Init(HWND window)
    {
        
        //Create device
        if (FAILED(_pDI8->CreateDevice(GUID_SysKeyboard, 
                                       &_pDID8, 
                                       NULL)))
        {
            ::MessageBox(0,"Could not create keyboard device",0,0);
        }
        
        
        //Set cooperative level
        if (FAILED(_pDID8->SetCooperativeLevel(window,
                                               DISCL_BACKGROUND | DISCL_NONEXCLUSIVE)))
        {
            ::MessageBox(0,"Could not set keyboard cooperative level",0,0);
        }
    
        //Set data format
        if (FAILED(_pDID8->SetDataFormat(&c_dfDIKeyboard)))
        {
            ::MessageBox(0,"Could not set keyboard data format",0,0);
        }
    
        //Acquire the keyboard
        if (FAILED(_pDID8->Acquire()))
        {
            ::MessageBox(0,"Could not acquire keyboard device",0,0);
        }
    
        return true;
    } 
    
    HRESULT CKeyboard::Update(void)
    {
        HRESULT hr;
       
        //Make sure we still have the device
        //If so fill keystate with current state values representing current
        //keyboard state
        hr=(_pDID8->GetDeviceState(sizeof(keystate),(LPVOID)&keystate));
        if (hr==DIERR_INPUTLOST)
        {
            ::MessageBox(0,"Input lost",0,0);
        }
       
        return true;
    }
    This works fine for me. This is code from my input system which I've used in several projects - and a friend of mine has used it with OpenGL and we have no issues with it. Not sure what is going on with yours. Look at my code and perhaps you might be able to compare it with yours to see what is different or what is causing the error.

    If you'll notice I commented out the unacquire() code because I received the same error. It seems that you do not need to unacquire the device before releasing it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  5. Damnit Microsoft! Direct Input?!
    By Deo in forum Game Programming
    Replies: 4
    Last Post: 06-10-2005, 11:09 AM