CoInitialize Problem

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

Moderator: Moderators

Post Reply
neobond
Posts: 13
Joined: Wed Jul 04, 2007 4:11 am

CoInitialize Problem

Post by neobond »

Hello...

Now I have to port my SpaceNavigator implementation to the same Framework, simply an older version (but still VS2005)

It is the same machine, same user etc...

BUT: once it works and in the other framework it complains about

Code: Select all

		hr	0x800401f0 CoInitialize wurde nicht aufgerufen. 	HRESULT
CoInitialize was not called

Could anyone please give me a hint...

Greetz
Andreas
ngomes
Moderator
Moderator
Posts: 3344
Joined: Mon Nov 27, 2006 7:22 am
Contact:

Post by ngomes »

Hi neobond,

To use the COM library, it is necessary to call ::CoInitialize(). For more information, please refer to MSDN.
Nuno Gomes
neobond
Posts: 13
Joined: Wed Jul 04, 2007 4:11 am

Post by neobond »

Hello ngomes,

I am not against using CoInitialize :), but it's not in my scope?!

Code: Select all

mHR = mPDevice.CreateInstance(__uuidof(Device));
is handling this, or not... and is telling me that it doesn't call CoInitialize...

I tried also the InitializeCOM()-example from ATL3DCube, same result... (not in the example, of course, but in my framework..)

Are there other things to mention?? For example in the project-file?? We made no changes in the other framework, it worked out of the box... (the #import-thing was enough)

Greetings
Andreas
ngomes
Moderator
Moderator
Posts: 3344
Joined: Mon Nov 27, 2006 7:22 am
Contact:

Post by ngomes »

Hi neobond,

Before calling any COM functions (including Device::CreateInstance), you will need to initialise the COM library. That's a generic COM requirement.

Simply call CoInitialize() when your application starts and before any call to a COM function. Remember to call CoUninitialize() before exiting your program.
neobond
Posts: 13
Joined: Wed Jul 04, 2007 4:11 am

Post by neobond »

More exactly:

it throws the error-message in here

Code: Select all

 hr = CoCreateInstance(rclsid, pOuter, dwClsContext, __uuidof(IUnknown), reinterpret_cast<void>(&pIUnknown));
inside HRESULT CreateInstance(const CLSID& rclsid, IUnknown* pOuter = NULL, DWORD dwClsContext = CLSCTX_ALL) throw() in comip.h

Greetings Andreas
ngomes
Moderator
Moderator
Posts: 3344
Joined: Mon Nov 27, 2006 7:22 am
Contact:

Post by ngomes »

hi neobond,

Where exactly are you calling CoInitialize()?
neobond
Posts: 13
Joined: Wed Jul 04, 2007 4:11 am

Post by neobond »

Hi ngomes

we try to get the spacenavigator initialized within the constructor

Code: Select all

SpaceNavigator::SpaceNavigator(){

	//for global referencing if SpaceNavigator has been found!
	deviceFound = false; 

#ifdef __USE_SPACENAV__

	//initializing input values from sensor
	x_trans = 0.0, y_trans = 0.0, z_trans = 0.0;
	x_vert  = 0.0, y_vert  = 0.0, z_vert  = 0.0;
	x_rot   = 0.0, y_rot   = 0.0, z_rot   = 0.0, angle_rot = 0.0;
	mMoveVolume = false; mMoveCamera = true; mMoveSlices = false;
 

	//mHR = mPDevice.CreateInstance(__uuidof(Device));
	mHR = mDevice.CoCreateInstance(__uuidof(Device));
	if ( SUCCEEDED( mHR ) ) { 
		
		mHR = mPDevice->Connect();  //will always be true, when driver is found;
									//even when there is no SpaveNavigator connected
		if ( SUCCEEDED( mHR ) ) {	// && mPDevice->Type == 6 SpaceNavigator has TypeId = 6

			//registrate (hook_in) the functions to DevicePointers and internal Operations
			mHR = __hook(&_ISimpleDeviceEvents::DeviceChange, mPDevice, &SpaceNavigator::OnDeviceChange);
			mHR = mPDevice->get_Sensor  (&mP3DSensor);
			mHR = __hook(&_ISensorEvents::SensorInput, mP3DSensor, &SpaceNavigator::OnSensorInput);

			mHR = mPDevice->get_Keyboard(&mP3DKeyboard);
			mHR = __hook(&_IKeyboardEvents::KeyDown, mP3DKeyboard, &SpaceNavigator::OnKeyDown);
			mHR = __hook(&_IKeyboardEvents::KeyUp,   mP3DKeyboard, &SpaceNavigator::OnKeyUp);

			//QTLogWindow::the()->addInfo( "SpaceNavigator::SpaceNavigator() - SpaceNavigator initialized correctly" );
			std::cout << "       JUHUHUHU" <<std>addError( "SpaceNavigator  ::SpaceNavigator() - sorry, unknown device type detected" );
			std::cout << "       NaNaNaNa" <<std>addError( "SpaceNavigator  ::SpaceNavigator() - driver not installed" );
		std::cout << "       NeinNÖNÖNÖ" << std::endl;
	}

	//initializeCOM();

#endif
}
we also tried to copy the initialize example from ATL3DCube as mentionend above....

the pointer are declared in the header

Code: Select all

		IUnknownPtr		  mDevice; /*CComPtr<IUnknown> */
		ISimpleDevicePtr  mPDevice; 
		ISensorPtr        mP3DSensor; 
		IKeyboardPtr      mP3DKeyboard;
		ITDxInfoPtr		  mP3DInfo;
As we are now working with vs2005 both declarations are possible... the framework is aiming to stay compatible also with vs2003 (which was not able to handle CComPtr<IUnknown>)

Greetings
Andreas

THANX for your quick replies[/code]
ngomes
Moderator
Moderator
Posts: 3344
Joined: Mon Nov 27, 2006 7:22 am
Contact:

Post by ngomes »

Hi neobond,

Code: Select all

	//initializeCOM();
This is suspicious. Why is it commented out? This function is probably calling ::CoInitialize(). If it is, try uncommenting it and move it up so it is the first thing that is called in the constructor.
neobond
Posts: 13
Joined: Wed Jul 04, 2007 4:11 am

SOLVED

Post by neobond »

Hello ngomes

we solved the problem. Maybe anyone is interested...

Visual Studio was telling us all the time what it needed!!!! Coinitialize :)

So we simply inserted
mHR::CoInitialize(......) before
mHR = mDevice.CoCreateInstance(__uuidof(Device));

and see it works!

But one curiosity is up to stay... in our other framework we never needed to call CoInitialize by ourself, it simply worked out of the box! (Code posted before)

THANX
Andreas

PS: //initializeCOM(); was the copied code from the ATL3DCube example, but didn't work either...
ngomes
Moderator
Moderator
Posts: 3344
Joined: Mon Nov 27, 2006 7:22 am
Contact:

Re: SOLVED

Post by ngomes »

Visual Studio was telling us all the time what it needed!!!! Coinitialize :)
Same here.
rodrigo.seabra
Posts: 44
Joined: Fri Apr 04, 2008 7:03 am

Post by rodrigo.seabra »

Hi neobond,

I could to see the complete code of your class SpaceNavigator, including the methods to catch the coordinates of the device?

Thanks.
Post Reply