C++ polling without COM

Post questions, comments and feedback to our 3Dconnexion Windows Development Team.

Moderator: Moderators

Post Reply
idcsteve
Posts: 8
Joined: Wed Nov 05, 2008 11:52 am

C++ polling without COM

Post by idcsteve »

Hi,

Now that I managed to make things work in C#, I have to figure out C++ so that my colleague (who's old school) can use it too. Wonderful.

What I would like it to be able to poll the spaceball whenever I want. I will be using this within the OGRE framework, so I won't really have any control over threads etc - the info has to be there when I ask for it.

We can't use ATL or MFC, so I've been using the code from
viewtopic.php?t=676
which was most helpful.

I still can't get any actual results out though - everything is zero. It could just be (and I really hope it is) that I'm not setting something to allow polling.

My sample code is:

Code: Select all

#include <Windows>
#include <stdlib>
#include <iostream>

#import "progid:TDxInput.Device.1" no_namespace 
using namespace std;

int main() {
	HRESULT hr=::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED );
	if (!SUCCEEDED(hr))
	{
		return -1;
	} 
	// create device
	ISimpleDevicePtr _3DxDevice;

	ISensor* m_p3DSensor;
	IKeyboard* m_p3DKeyboard;

	hr = _3DxDevice.CreateInstance(__uuidof(Device));
	if (SUCCEEDED(hr))
	{

		ISimpleDevicePtr pSimpleDevice = _3DxDevice;
		if (pSimpleDevice != NULL) 
		{
			// Get the interfaces to the sensor and the keyboard;
			m_p3DSensor      = pSimpleDevice->Sensor;
			m_p3DKeyboard   = pSimpleDevice->Keyboard;
			pSimpleDevice->LoadPreferences("Cube3DPolling");
			// Connect to the driver
			pSimpleDevice->Connect();
			cout << "Connected" <<endl>Translation;
			rot = m_p3DSensor->Rotation;
			cout <<trans>X << "    " <<rot>Angle << endl;
		}
	}
	return 0;
}
Some important things to consider:
  • I know about as much COM as you could write on the small crumpled remains of Don Box if I ever find him.
    I only know slightly more C++ than I know COM
This is all so I can hand over to a colleague and get back to using the product - which I like very much - in C# where I belong.
idcsteve
Posts: 8
Joined: Wed Nov 05, 2008 11:52 am

Post by idcsteve »

Sorry, the code seems to have copied over strangely. I couldn't get it to work in this post until I removed the "cout <<Connected>"'s.

Code: Select all

#include <Windows>
#include <stdlib>
#include <iostream>

#import "progid:TDxInput.Device.1" no_namespace 
using namespace std;

__int64 gKeyStates=0;

int main() {
	HRESULT hr=::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED );
	if (!SUCCEEDED(hr))
	{
		return -1;
	} 
	// create device
	ISimpleDevicePtr _3DxDevice;

	ISensor* m_p3DSensor;
	IKeyboard* m_p3DKeyboard;

	hr = _3DxDevice.CreateInstance(__uuidof(Device));
	if (SUCCEEDED(hr))
	{

		ISimpleDevicePtr pSimpleDevice = _3DxDevice;
		if (pSimpleDevice != NULL) 
		{
			// Get the interfaces to the sensor and the keyboard;
			m_p3DSensor      = pSimpleDevice->Sensor;
			m_p3DKeyboard   = pSimpleDevice->Keyboard;
			pSimpleDevice->LoadPreferences("Cube3DPolling");
			// Connect to the driver
			pSimpleDevice->Connect();
		}
	} 

	Sleep(1000);
	while(1) {
		Sleep(10);
		if (m_p3DSensor)  {
			IVector3DPtr trans;
			IAngleAxisPtr rot;
			trans = m_p3DSensor->Translation;
			rot = m_p3DSensor->Rotation;
			cout <<trans>X << "    " <<rot>Angle << endl;
		}
	}
	return 0;
}
mbouchard71
Posts: 1
Joined: Sat Nov 15, 2008 12:33 pm

Post by mbouchard71 »

I am having the same problem with my application where everything looks fine but the rotation and translation always read 0s.

I'll be very interested in the solution for this problem.

In my case, we use WxWidget instead of MFC. Would it be possible that the COM subsystem does not work in that case?
ngomes
Moderator
Moderator
Posts: 3344
Joined: Mon Nov 27, 2006 7:22 am
Contact:

Post by ngomes »

Hi mbouchard71,
In my case, we use WxWidget instead of MFC. Would it be possible that the COM subsystem does not work in that case?
It should work provided that the thread where you make the 3DxInput calls has a windows message loop.
Nuno Gomes
Post Reply