favorite
I'm trying to move the mouse inside a VirtualBox Guest programmatically, but I can't get the steps right to actually use the mouse move function. Using C++. Building with nmake.
Here's the code. The virtual machine is running while I try this. If i run the program while it's virtual machine is shut down, I get a lock successfully but then the call console->getMouse causes a crash. Everything works fine, no errors until getConsole, where I find out that the session I give it is unlocked.
Code: Select all
int main()
{
HRESULT rc;
IVirtualBox *virtualBox;
do
{
/* Initialize the COM subsystem. */
CoInitialize(NULL);
/* Instantiate the VirtualBox root object. */
rc = CoCreateInstance(CLSID_VirtualBox, NULL, CLSCTX_LOCAL_SERVER, IID_IVirtualBox, (void**)&virtualBox);
if (!SUCCEEDED(rc)){
//
printf("Error creating VirtualBox instance! rc = 0x%x\n", rc);
break;
}
//listVMs(virtualBox);
//testErrorInfo(virtualBox);
ISession *session = NULL;
/* Create the session object. */
rc = CoCreateInstance(CLSID_Session, /* the VirtualBox base object */
NULL, /* no aggregation */
CLSCTX_INPROC_SERVER, /* the object lives in a server process on this machine */
IID_ISession, /* IID of the interface */
(void**)&session);
if (!SUCCEEDED(rc))
{
printf("Error creating Session instance! rc = 0x%x\n", rc);
break;
}
printf("Session instance created.\n");
IMachine *machine = NULL;
rc = virtualBox->FindMachine(SysAllocString(L"OVM1"), &machine);
if (!SUCCEEDED(rc))
{
printf("Error vm not found\n");
break;
}
printf("VM found.\n");
machine->LockMachine (session, LockType_Shared);
SessionType type;
session->get_Type(&type);
printf("typ: %d\n", type);
BSTR name;
machine->get_Name(&name);
printf("nam: %S\n", name);
/* Get console object. */
IConsole *console = NULL;
IMouse *mouse = NULL;
rc = session->get_Console(&console);
if (!SUCCEEDED(rc))
{
IErrorInfo *errorInfo;
rc = GetErrorInfo(0, &errorInfo);
if (FAILED(rc))
printf("Error getting error info! rc = 0x%x\n", rc);
else
{
BSTR errorDescription = NULL;
rc = errorInfo->GetDescription(&errorDescription);
if (FAILED(rc) || !errorDescription)
printf("Error getting error description! rc = 0x%x\n", rc);
else
{
printf("Successfully retrieved error description: %S\n", errorDescription);
SysFreeString(errorDescription);
}
SAFE_RELEASE(errorInfo);
}
printf("Error opening console\n");
break;
}
printf("Console opened.\n");
console->get_Mouse(&mouse);
lasterror();
BOOL supported=false;
mouse->get_AbsoluteSupported(&supported);
if(supported)
{
printf("Absolute Supported\n");
}
rc = mouse->PutMouseEventAbsolute(100,100,0,0,0);
if (rc==VBOX_E_IPRT_ERROR)
{
printf("Error sending mouse event\n");
break;
}
rc = mouse->PutMouseEvent(10,10,0,0,0);
/* Enable the following line to get a VM started. */
//testStartVM(virtualBox);
/* Release the VirtualBox object. */
session->UnlockMachine();
virtualBox->Release();
} while (0);
Sleep(200000);
CoUninitialize();
return 0;
}
Code: Select all
IErrorInfo *errorInfo;
rc = GetErrorInfo(0, &errorInfo);
if (FAILED(rc))
printf("Error getting error info! rc = 0x%x\n", rc);
else
{
BSTR errorDescription = NULL;
rc = errorInfo->GetDescription(&errorDescription);
if (FAILED(rc) || !errorDescription)
printf("Error getting error description! rc = 0x%x\n", rc);
else
{
printf("Successfully retrieved error description: %S\n", errorDescription);
SysFreeString(errorDescription);
}
SAFE_RELEASE(errorInfo);
}
Any help is welcome, thank you.