DirectInput8 and MFC problem

Started by
7 comments, last by domin 16 years ago
Hi all, I try to use DI keyboard in MFC app, but I have problems with SetCooperativeLevel() method. In my class (CView descendant), in OnInitialUpdate() I have following DI init code:

	HR = DirectInput8Create(_HInstance, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&_DirectInput, NULL);

	if (FAILED(HR))
	{
		strcpy(OutputText, "DirectInput8Create() - FAILED");
		
		goto __Failure;
	}

	HR = _DirectInput->CreateDevice(GUID_SysKeyboard, &_KeyboardDevice, 0); 

	if (FAILED(HR))
	{
		strcpy(OutputText, "Keyboard Device: CreateDevice() - FAILED");
		
		goto __Failure;
	}

	HR = _KeyboardDevice->SetDataFormat(&c_dfDIKeyboard); 

	if (FAILED(HR))
	{
		strcpy(OutputText, "Keyboard Device: SetDataFormat() - FAILED");
		
		goto __Failure;
	}

	HR = _KeyboardDevice->SetCooperativeLevel(_HWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE);  

	if (FAILED(HR))
	{
		strcpy(OutputText, "Keyboard Device: SetCooperativeLevel() - FAILED");
		
		goto __Failure;
	}

	HR = _KeyboardDevice->Acquire();

	if (FAILED(HR))
	{
		strcpy(OutputText, "Keyboard Device: Acquire() - FAILED");
		
		goto __Failure;
	}



The line: HR = _KeyboardDevice->SetCooperativeLevel(_HWnd, DISCL_FOREGROUND | DISCL_EXCLUSIVE); returns E_HANDLE no matter what coop. level is set... Any idea where is the problem? Thanks.
Advertisement
Don't do that. It's an incredibly bad idea to use DirectInput for keyboard or mouse input - just use window messages, which will be easier to use and have less overhead.

However, that error is telling you that _HWnd is invalid. Check that it is valid (with the debugger). Or, better - don't, and use window messages.
Thanks, yes, I know, DI is not a best solution here, but I just want to put my 3d engine (without any modifications (leaving window creation to MFC of course)) into MFC to create a level editor...

But I don't understand what is wrong with my _HWnd... I get it using CView::GetSafeHwnd(). It is ok for D3D cration, but not ok for DI? Strange...
Quote:Original post by domin
Thanks, yes, I know, DI is not a best solution here, but I just want to put my 3d engine (without any modifications (leaving window creation to MFC of course)) into MFC to create a level editor...

But I don't understand what is wrong with my _HWnd... I get it using CView::GetSafeHwnd(). It is ok for D3D cration, but not ok for DI? Strange...
What is the actual value of the _HWnd variable (in hex)? Is it a top level window handle (Use Spy++ to check).

I still don't understand why you're using DirectInput here - all you're doing is making life difficult for yourself. MFC already gives you keyboard input, you're just trying to get it in another, less efficient way. Are you also going to get rid of MFC's dialog rendering and try to do it yourself by drawing shapes into CDC's?
The value is: HWnd = 0x000f08ea {unused=-267535699 }

The reason for using DI or XI is simple - my engine must run on XB360 HW.
Quote:Original post by domin
The value is: HWnd = 0x000f08ea {unused=-267535699 }
What window does Spy++ tell you that is? Is it a top level window or a child window?

Quote:Original post by domin
The reason for using DI or XI is simple - my engine must run on XB360 HW.
That's what XInput is for. DirectInput doesn't exist on the 360 (As far as I know).
According to Spy++ the handle belongs to window that has WS_CHILDWINDOW in styles. Is it important?
Quote:Original post by domin
According to Spy++ the handle belongs to window that has WS_CHILDWINDOW in styles. Is it important?
I thought that the window handle passed to DirectInput had to be a top level window. Are you able to put in the top level window handle? For testing, you could put a breakpoint on the SetCooperativeLevel line, get the HWND from Spy++ and then change the _HWnd variable in the debugger to the value Spy++ gives.
Yep, for the top level window everything works fine. Thanks for your time!

This topic is closed to new replies.

Advertisement