Event Listener not giving any events

Discussion about using the VirtualBox API, Tutorials, Samples.
Post Reply
MLKKK
Posts: 6
Joined: 10. Jan 2017, 18:47

Event Listener not giving any events

Post by MLKKK »

After implementing a workaround to the issue in my previous post-> viewtopic.php?f=34&t=81365 (I start the machine instead of trying to get a lock while it's running) -I've come across another issue.

Title explains it all. Execution is blocked at GetEvent, and only resumes if I give the function a finite timeout value as argument. Here's my event listener code using VB API version 5.1.14 (VB app is 5.1.12)

Code: Select all

......................
		session->get_Console(&console);

		auto msev_func = [](LPVOID p){
			//
			IConsole *console = (IConsole*)p;

			IEventSource *es; 
			HRESULT rc = console->get_EventSource(&es);			//get_VMerr(rc, "evso get");	// error description function - successful in all instances
			IEventListener *listener; es->CreateListener(&listener);   //get_VMerr(rc, "evli creat");  // error description function - successful in all instances


			VBoxEventType aTypes[] = { VBoxEventType::VBoxEventType_Any };

			SAFEARRAY * psa;
			SAFEARRAYBOUND rgsabound[1];
			rgsabound[0].lLbound = 0;
			rgsabound[0].cElements = 1;
			psa = SafeArrayCreate(VT_INT, 1, rgsabound);

			LONG  rgIndex = 0;
			HRESULT hr = SafeArrayPutElement(psa, &rgIndex, aTypes);

			int otyp;
			// list of event types to listen for
			es->RegisterListener(listener, psa, false /* active */);  get_VMerr(rc, "evli reg"); // error description function - successful in all instances
			// register passive listener111

			SafeArrayGetElement(psa, &rgIndex, &otyp); // test value
			printf("want: %d\n is: %d\n", VBoxEventType::VBoxEventType_OnMousePointerShapeChanged, otyp); // test is successful
			
			while (1)
			{
				IEvent *ev;
				hr = es->GetEvent(listener, -1, &ev); 		get_VMerr(rc, "evli get"); // error description function - successful in all instances of this code 
				if (ev != nullptr){
					// downcast to specific event interface (in this case we have only registered
					// for one type, otherwise IEvent::type would tell us)
					printf("evented");

					IMousePointerShapeChangedEvent *mcse;
					mcse->QueryInterface(&ev);
					// inspect and do something
					//
					es->EventProcessed(listener, ev);
				}
			}
			es->UnregisterListener(listener);
			return (DWORD)2;
		};

		HANDLE hThread;
		DWORD dwThread;

		hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)msev_func, console, 0, &dwThread);
		WaitForSingleObject(hThread, INFINITE);
Last edited by MLKKK on 30. Jan 2017, 18:44, edited 2 times in total.
socratis
Site Moderator
Posts: 27329
Joined: 22. Oct 2010, 11:03
Primary OS: Mac OS X other
VBox Version: PUEL
Guest OSses: Win(*>98), Linux*, OSX>10.5
Location: Greece

Re: Event Listener not giving any events

Post by socratis »

MLKKK wrote:After implementing a workaround to the issue in my previous viewtopic.php?f=34&t=81365
Could you also post the workaround in that thread and mark it as such? You left it hanging with "it doesn't work". Any future searchers will appreciate it...
Do NOT send me Personal Messages (PMs) for troubleshooting, they are simply deleted.
Do NOT reply with the "QUOTE" button, please use the "POST REPLY", at the bottom of the form.
If you obfuscate any information requested, I will obfuscate my response. These are virtual UUIDs, not real ones.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Event Listener not giving any events

Post by noteirak »

MLKKK wrote:Execution is blocked at GetEvent, and only resumes if I give the function a finite timeout value as argument.
That is by design and documented on IEventSource::getEvent()

I don't think IConsole fires any events, which would explain why you never get any. You should use the event source on IVirtualBox or the appropriate IMachine instead.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
MLKKK
Posts: 6
Joined: 10. Jan 2017, 18:47

Re: Event Listener not giving any events

Post by MLKKK »

socratis wrote:
MLKKK wrote:After implementing a workaround to the issue in my previous viewtopic.php?f=34&t=81365
Could you also post the workaround in that thread and mark it as such? You left it hanging with "it doesn't work". Any future searchers will appreciate it...
Because it doesn't work. The workaround, as I stated, is starting the machine programmatically using LaunchVMProcess(..) - takes a long to time to start the machine which makes testing code quite irritating. It'd be nice if LockMachine worked, but I'll post a mention of this workaround if you will..
MLKKK
Posts: 6
Joined: 10. Jan 2017, 18:47

Re: Event Listener not giving any events

Post by MLKKK »

noteirak wrote:
MLKKK wrote:Execution is blocked at GetEvent, and only resumes if I give the function a finite timeout value as argument.
That is by design and documented on IEventSource::getEvent()

I don't think IConsole fires any events, which would explain why you never get any. You should use the event source on IVirtualBox or the appropriate IMachine instead.
The code is actually a sample from the documentation, which I adapted to work with the API - apparently the documentation does not reflect the actual API it comes with; I added the part with SAFEARRAY.

I have an IVirtualBox object that I use to find the machine that I then start. The object does not point to a specific machine, and I don't know how to make it point to one, thus it would be useless as an event source. I tried though and no events fired. Also tried IMouse, same results.

As for IMachine, there is no event source attribute or function.
noteirak
Site Moderator
Posts: 5229
Joined: 13. Jan 2012, 11:14
Primary OS: Debian other
VBox Version: OSE Debian
Guest OSses: Debian, Win 2k8, Win 7
Contact:

Re: Event Listener not giving any events

Post by noteirak »

Let's try the other way around. Which events are you trying to capture?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply