Skip to main content
gamedev.net
🔒 Locked

DirectInput and C#

Started by noparity 1 reply 850+ views
Original Post
noparity
noparity
I''m attemping to get DirectInput up and running with C# but I''m having a bit of a problem. I''m new to DirectX, so I''ve been following the tutorials in the SDK. Specifically, my problem is setting up the buffer size. Here''s the function I use to initialize DirectInput:
  
public void InitializeDirectInput(int hwnd, DirectXEvent app)
		{
			m_objDirectInputDevice = m_objDirectInput.CreateDevice("GUID_SysMouse");
			m_objDirectInputDevice.SetCommonDataFormat(CONST_DICOMMONDATAFORMATS.DIFORMAT_MOUSE);
			m_objDirectInputDevice.SetCooperativeLevel(hwnd, CONST_DISCLFLAGS.DISCL_FOREGROUND | CONST_DISCLFLAGS.DISCL_EXCLUSIVE);
			
			DIPROPLONG diProp = new DIPROPLONG();
			diProp.lHow = (int)CONST_DIPHFLAGS.DIPH_DEVICE;
			diProp.lObj = 0;
			diProp.lData = 128;

			System.IntPtr pdiProp = new System.IntPtr();
			pdiProp = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(DIPROPLONG)));
			Marshal.StructureToPtr(diProp, pdiProp, true);

			m_objDirectInputDevice.SetProperty("DIPROP_BUFFERSIZE", pdiProp);
			Marshal.FreeHGlobal(pdiProp);

			int eventHandle;
			eventHandle = m_objDirectX.CreateEvent(app);
			m_objDirectInputDevice.SetEventNotification(eventHandle);
		}
  
The problem is the call SetProperty("DIPROP_BUFFERSIZE", pdiProp). This is the exception I''m getting: An unhandled exception of type ''System.ArgumentException'' occurred in SpaceBreakout.exe Additional information: The parameter is incorrect. This code is taken (almost)verbatim from the SDK (ported to C# of course), so maybe there''s a better way of doing this. Any help/ideas would be greatly appreciated!
IndirectX
IndirectX
In C++ DIPROPHEADER has two size members, are you initializing them if you have them?
---visit #directxdev on afternet <- not just for directx, despite the name
noparity
noparity
You''re right. It seems that the sample left out the fact that you need to initialize the lSize member:

  
diProp.lSize = Marshal.SizeOf(typeof(DIPROPLONG));

Thanks for the tip, everything is working fine now.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.