Archived

This topic is now archived and is closed to further replies.

DCrazy

DirectInput: Acquire() failed with E_INVALIDARG

Recommended Posts

I''m implementing an action map class for my combination DirectX wrapper/engine. I created the action map and DirectInput wrapper completely on my own. Then I realized it was almost identical (!) to the sample framework code supplied with the DirectX 8.1 SDK. So why does Acquire() fail on my devices? I''m doing almost the exact same thing as the framework does. Here''s my code:
  
// IsoEngineInput.cpp: implementation of the CIsoEngineInput class.

//

//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "IsoEngineInput.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


CIsoEngineInput::CIsoEngineInput()
{
	m_pDirectInput8 = NULL;
	m_pDevices = NULL;
	m_nNumDevices = 0;
}

CIsoEngineInput::~CIsoEngineInput()
{
	if(m_pDirectInput8 != NULL)
	{
		m_pDirectInput8->Release();
		m_pDirectInput8 = NULL;
	}

	ReleaseDevices();
}

HRESULT CIsoEngineInput::Init(CIsoEngineWorld *pWorld, HINSTANCE hInstance, HWND hWnd)
{
	HRESULT hResult;

	m_pWorld = pWorld;
	m_hInstance = hInstance;
	m_hWnd = hWnd;

	hResult = DirectInput8Create(m_hInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void **)&m_pDirectInput8, NULL);
	if(FAILED(hResult))
	{
		IsoTrace(__FILE__, __LINE__, hResult, "DirectInput8Create FAILED!", DISP_MB);
		return hResult;
	}

	return S_OK;
}

HRESULT CIsoEngineInput::UseActionMap(CIsoEngineActionMap *pMap)
{
	HRESULT hResult;

	m_pMap = pMap;

	hResult = m_pDirectInput8->EnumDevicesBySemantics(NULL, &(m_pMap->m_diafFormat), EnumDevicesCallback, this, 0L);
	if(FAILED(hResult))
	{
		IsoTrace(__FILE__, __LINE__, hResult, "EnumDevicesBySemantics FAILED!", DISP_MB);
		return hResult;
	}

	return S_OK;
}

BOOL __stdcall CIsoEngineInput::EnumDevicesCallback(LPCDIDEVICEINSTANCE pDDI, LPDIRECTINPUTDEVICE8 pDIDevice, DWORD dwFlags, DWORD dwRemaining, LPVOID pData)
{
	CIsoEngineInput *pInput = (CIsoEngineInput *)pData;

	pInput->AddDevice(pDIDevice);
	pInput->BuildActionMap(pDIDevice);

	return DIENUM_CONTINUE;
}

//DEL HRESULT CIsoEngineInput::ImplementActionMap()

//DEL {

//DEL 	HRESULT hResult;

//DEL 

//DEL 	hResult = m_pInput->m_pDirectInput8->EnumDevicesBySemantics(NULL, &m_diafFormat, EnumDevicesCallback, this, 0L);

//DEL 	if(FAILED(hResult))

//DEL 	{

//DEL 		IsoTrace(__FILE__, __LINE__, hResult, "EnumDevicesBySemantics FAILED!", DISP_MB);

//DEL 		return hResult;

//DEL 	}

//DEL 

//DEL 	return S_OK;

//DEL }


HRESULT CIsoEngineInput::BuildActionMap(LPDIRECTINPUTDEVICE8 pDevice)
{
	HRESULT hResult;

	pDevice->Unacquire();

	hResult = pDevice->BuildActionMap(&(m_pMap->m_diafFormat), NULL, 0L);
	if(FAILED(hResult))
	{
		IsoTrace(__FILE__, __LINE__, hResult, "BuildActionMap FAILED!", DISP_MB);
		return hResult;
	}

	hResult = pDevice->SetActionMap(&(m_pMap->m_diafFormat), NULL, 0L);
	if(FAILED(hResult))
	{
		IsoTrace(__FILE__, __LINE__, hResult, "SetActionMap FAILED!", DISP_MB);
		return hResult;
	}

	pDevice->Acquire();

	return S_OK;
}

HRESULT CIsoEngineInput::AddDevice(LPDIRECTINPUTDEVICE8 pDevice)
{
	HRESULT hResult;

	m_nNumDevices++;

	if(m_pDevices != NULL)
	{
		m_pDevices = (LPDIRECTINPUTDEVICE8 *)realloc(m_pDevices, sizeof(LPDIRECTINPUTDEVICE8)*m_nNumDevices);
	}
	else
	{
		m_pDevices = (LPDIRECTINPUTDEVICE8 *)malloc(sizeof(LPDIRECTINPUTDEVICE8)*m_nNumDevices);
	}

	if(m_pDevices == NULL)
	{
		hResult = E_OUTOFMEMORY;
		IsoTrace(__FILE__, __LINE__, hResult, "Not enough memory to create device list!", DISP_MB);
		return hResult;
	}

	pDevice->Unacquire();
	pDevice->SetCooperativeLevel(m_hWnd, DISCL_NONEXCLUSIVE | DISCL_FOREGROUND);

	DIPROPDWORD dipdw;
    dipdw.diph.dwSize       = sizeof(DIPROPDWORD);
    dipdw.diph.dwHeaderSize = sizeof(DIPROPHEADER);
    dipdw.diph.dwObj        = 0;
    dipdw.diph.dwHow        = DIPH_DEVICE;
    dipdw.dwData            = 500;
    pDevice->SetProperty( DIPROP_DEADZONE, &dipdw.diph );
	// Simply sets deadzone


	pDevice->AddRef();
	m_pDevices[m_nNumDevices - 1] = pDevice;

	return S_OK;
}

HRESULT CIsoEngineInput::Update()
{
	HRESULT hResult;

	for(UINT nDevice = 0; nDevice < m_nNumDevices; nDevice++)
	{
		DIDEVICEOBJECTDATA didod;
		DWORD dwCount = 1;

		// Acquire() below returns E_INVALIDARG... why?!

		hResult = m_pDevices[nDevice]->Acquire();
		if(FAILED(hResult))
		{
			IsoTrace(__FILE__, __LINE__, hResult, "Acquire FAILED!", DISP_MB);
			return hResult;
		}

		hResult = m_pDevices[nDevice]->Poll();
		if(FAILED(hResult))
		{
			IsoTrace(__FILE__, __LINE__, hResult, "Poll FAILED!", DISP_MB);
			return hResult;
		}

		hResult = m_pDevices[nDevice]->GetDeviceData(sizeof(didod), &didod, &dwCount, NULL);
		if(FAILED(hResult))
		{
			IsoTrace(__FILE__, __LINE__, hResult, "GetDeviceData FAILED!", DISP_MB);
			return hResult;
		}

		m_pWorld->HandleInput(didod.uAppData, didod.dwData);
	}

	return S_OK;
}

void CIsoEngineInput::ReleaseDevices()
{
	for(UINT i = 0; i < m_nNumDevices; i++)
	{
		m_pDevices[i]->Unacquire();
		m_pDevices[i]->Release();
		m_pDevices[i] = NULL;
	}

	if(m_pDevices != NULL)
	{
		free(m_pDevices);
		m_pDevices = NULL;
	}
}
  

  
// IsoEngineActionMap.cpp: implementation of the CIsoEngineActionMap class.

//

//////////////////////////////////////////////////////////////////////


#include "stdafx.h"
#include "IsoEngineActionMap.h"

//////////////////////////////////////////////////////////////////////

// Construction/Destruction

//////////////////////////////////////////////////////////////////////


CIsoEngineActionMap::CIsoEngineActionMap()
{
	m_pMappings = NULL;
	m_pActions = NULL;
	m_nNumActions = 0;
	m_nNumMappings = 0;
}

CIsoEngineActionMap::~CIsoEngineActionMap()
{
	if(m_pActions != NULL)
	{
		free(m_pActions);
	}

	if(m_pMappings != NULL)
	{
		free(m_pMappings);
	}
}

HRESULT CIsoEngineActionMap::Init(LPCTSTR lpszActionMapName, DWORD dwGenre, GUID guidApp)
{
	m_diafFormat.dwSize = sizeof(DIACTIONFORMAT);
	m_diafFormat.dwActionSize = sizeof(DIACTION);
	m_diafFormat.guidActionMap = guidApp;
	m_diafFormat.dwGenre = dwGenre;
	m_diafFormat.dwBufferSize = 16;
	m_diafFormat.lAxisMin = -100;
	m_diafFormat.lAxisMax = 100;
	strcpy(m_diafFormat.tszActionMap, lpszActionMapName);

	return S_OK;
}

HRESULT CIsoEngineActionMap::AddAction(UINT nActionID)
{
	HRESULT hResult;

	m_nNumActions++;

	if(m_pActions != NULL)
	{
		m_pActions = (UINT *)realloc(m_pActions, sizeof(UINT)*m_nNumActions);
	}
	else
	{
		m_pActions = (UINT *)malloc(sizeof(UINT)*m_nNumActions);
	}

	if(m_pActions == NULL)
	{
		hResult = E_OUTOFMEMORY;
		IsoTrace(__FILE__, __LINE__, hResult, "Not enough memory to create action list!", DISP_MB);
		return hResult;
	}

	m_pActions[m_nNumActions - 1] = nActionID;

	return S_OK;
}

HRESULT CIsoEngineActionMap::AddMapping(UINT nActionID, DWORD dwControl, LPCTSTR lpszActionName, BOOL bForceMap)
{
	HRESULT hResult;

	m_nNumMappings++;

	if(m_pMappings != NULL)
	{
		m_pMappings = (DIACTION *)realloc(m_pMappings, sizeof(DIACTION)*m_nNumMappings);
	}
	else
	{
		m_pMappings = (DIACTION *)malloc(sizeof(DIACTION)*m_nNumMappings);
	}

	if(m_pMappings == NULL)
	{
		hResult = E_OUTOFMEMORY;
		IsoTrace(__FILE__, __LINE__, hResult, "Not enough memory to create mapping list!", DISP_MB);
		return hResult;
	}

	m_pMappings[m_nNumMappings - 1].uAppData = nActionID;
	m_pMappings[m_nNumMappings - 1].dwSemantic = dwControl;
	m_pMappings[m_nNumMappings - 1].dwFlags = (bForceMap == TRUE ? DIA_APPFIXED : NULL);
	m_pMappings[m_nNumMappings - 1].lptszActionName = lpszActionName;
	m_pMappings[m_nNumMappings - 1].dwHow = NULL;

	m_diafFormat.dwDataSize = m_nNumMappings * sizeof(DWORD);
	m_diafFormat.dwNumActions = m_nNumMappings;
	m_diafFormat.rgoAction = m_pMappings;

	return S_OK;
}
  

Share this post


Link to post
Share on other sites
If you're running your program directly from Visual Studio, Visual Studio has control of the keyboard so Acquire() doesn't work the first time. The way I fixed this (temporarily) is by initializing it twice, but I'm not sure if that's the way to go.

[edited by - psybr on July 27, 2002 1:24:15 PM]

Share this post


Link to post
Share on other sites
It turns out that''s not the problem. Acquire() itself is failing. Why oh why? IDirectInputDevice8::Acquire is returning E_INVALIDARG, no matter how many times I call it. Can anyone help me?

Share this post


Link to post
Share on other sites