I need fast help!

Discussion about using the VirtualBox API, Tutorials, Samples.
Wulfiks
Posts: 13
Joined: 13. Jun 2015, 09:10

I need fast help!

Post by Wulfiks »

I'm just a student and for the second month I can`t write a full-featured software to interact with the virtual machine.
I started developing in C ++ and thanks examples I managed to run a VM, to change some of its parameters, but I can`t to get
image from it. I reviewed the entire forum, tried to search for information in letters to developers to implement whatever turns find examples. The result is zero.
Please, help me to grasp with the algorithm of interaction with the API VirtualBox.
I'm not asking for a turnkey solution, and I hope for your understanding and competent help. Hope for soon answer. Best regards!
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: I need fast help!

Post by noteirak »

What do you mean exactly by "can't get image from it"?
Also a good starting point would be the most stand alone code possible you would already have written. It would be a good place to start helping you.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Wulfiks
Posts: 13
Joined: 13. Jun 2015, 09:10

Re: I need fast help!

Post by Wulfiks »

Please do not judge strictly ...

Code: Select all

rc = virtualBox->FindMachine(machineName, &machine);
				
				if (FAILED(rc))
				{
					IErrorInfo *errorInfo;
					rc = GetErrorInfo(0, &errorInfo);
					if (FAILED(rc))
					{
						SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error getting error info! rc =")));
						SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
					}
					else
					{
						BSTR errorDescription = NULL;
						rc = errorInfo->GetDescription(&errorDescription);
						if (FAILED(rc) || !errorDescription)
						{
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error getting error description! rc =")));
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
						}
						else
						{
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Successfully retrieved error description:")));
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(errorDescription));
							SysFreeString(errorDescription);
						}
						SAFE_RELEASE(errorInfo);
					}
				}
				else
				{
						rc = machine->get_Id(&guid); 
						if (!SUCCEEDED(rc))
						{
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error retrieving machine ID! rc =")));
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
							break;
						}
						rc = CoCreateInstance(CLSID_Session,NULL, CLSCTX_INPROC_SERVER,IID_ISession,(void**)&session);
						if (!SUCCEEDED(rc))
						{
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error creating Session instance! rc = ")));
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
							break;
						}

						machine->LockMachine(session, LockType::LockType_VM);
						//machine->ShowConsoleWindow(0);
												
						rc = session->get_Console(&console);

						if (!SUCCEEDED(rc))
						{
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error creating Session instance! rc = ")));
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
							break;
						}
						else
						{
							
							console->get_Display(&display);
						display->SetFramebuffer(0,framebuffer);
						rc = console->PowerUp(&progress);
						}

						//rc = machine->LaunchVMProcess(session, sessiontype, NULL, &progress);
						if (!SUCCEEDED(rc))
						{
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Could not open remote session! rc = ")));
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
							break;
						}
						
						SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Starting VM, please wait ...\n")));
						rc = progress->WaitForCompletion (-1);
						SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("VM load!\n")));
						
						//machine->ShowConsoleWindow(0);
				}
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: I need fast help!

Post by noteirak »

I see, you try to create your own frontend. Then I really need the info listed in the API posting guidelines topic before going further.
This is no easy topic and, while I won't be able to give you the definitive answer, I'll do my best to point you in the right direction.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Wulfiks
Posts: 13
Joined: 13. Jun 2015, 09:10

Re: I need fast help!

Post by Wulfiks »

Virtualbox version 4.3.12-93733
Virtualbox sdk 4.3.12-93733
Connection type COM
Language used C++/WinAPI
OS host - Windows 7 x86/x64

________________________________________

As part of my thesis, I develop DLL that would use the functions of the virtual machine in applications for .NET
Sorry for my English.
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: I need fast help!

Post by noteirak »

Alright - maybe you don't need your own frontend then. what do you need to do exactly with the VM? Just run commands in it? Do you just want to start the VM in the background?
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Wulfiks
Posts: 13
Joined: 13. Jun 2015, 09:10

Re: I need fast help!

Post by Wulfiks »

It works for me. I start / stop / I get a list of installed machines. I need to get an image (screenshot). It's really a kind of front-end.
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: I need fast help!

Post by noteirak »

If you just want a screenshot of the console, then you can start the VM using the headless front-end [1] and then:
- Get a IDisplay object from IConsole::display after getting a shared lock with ISession
- Use IDisplay::takeScreenShot() or takeScreenShotToArray() or even takeScreenShotPNGToArray() to get the screenshot, whatever fits your need.

[1] Java example, lines 37 to 47 are of interest to you. I don't have a standalone code sample in C++ but you should work it out if you managed to get this far.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Wulfiks
Posts: 13
Joined: 13. Jun 2015, 09:10

Re: I need fast help!

Post by Wulfiks »

Thank you very much! I've already tried these functions, but I did not have a clear algorithm.
And this is a must-do through webmanager?
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: I need fast help!

Post by noteirak »

No you don't need to use the webmanager, don't worry about that part of the sample code.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Wulfiks
Posts: 13
Joined: 13. Jun 2015, 09:10

Re: I need fast help!

Post by Wulfiks »

I run a virtual machine in the mode "headless", but to get a screenshot and failed. The following piece of code to run VM and get screenshot.

Code: Select all

***
				rc = virtualBox->FindMachine(machineName, &machine);
				
				
						rc = machine->get_Id(&guid); 
						
						rc = CoCreateInstance(CLSID_Session,NULL, CLSCTX_INPROC_SERVER,IID_ISession,(void**)&session);
						if (!SUCCEEDED(rc))
						{
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error creating Session instance! rc = ") + rc));
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
							break;
						}
						else
						{
							rc = machine->LaunchVMProcess(session, sessiontype, NULL, &progress);
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Starting VM, please wait ...\n") + rc));
							rc = progress->WaitForCompletion (-1);
							SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("VM load!\n")));
							machine->ShowConsoleWindow(0);

							rc = session->get_Console(&console);
							if (!SUCCEEDED(rc))
							{
								SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error creating console! rc =") + rc));
								SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
								break;
							}

							rc = session->UnlockMachine();
							if (!SUCCEEDED(rc))
							{
								SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Failed sesion unlock! rc =") + rc));
								SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
								break;
							}

						}
/*
/
/ some code
/
*/
else if (LOWORD(wParam) == IDC_BUTTON5)  /*Get screenshot*/
			{
				
				machine->LockMachine(session, LockType::LockType_Shared);
				rc = console->get_Display(&display);
				if (!SUCCEEDED(rc))
				{
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error creating display! rc =") + rc));
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
					break;
				}
				
				rc = display->GetScreenResolution(0, &width, &height, &bperp, &xWidth, &xHeight);
				if (!SUCCEEDED(rc))
				{
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error GetScreenResolution! rc =") + rc));
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
					break;
				}

				BYTE * buffer = new BYTE[width*height*bperp*4];

				rc = display->TakeScreenShot(0, buffer, width, height);   /* x32 -  error 0x800706be - сбой при удаленном вызове процедуры*/ /* x64 - RPC_E_SERVERFAULT The server threw an exception.*/
				if (!SUCCEEDED(rc))
				{
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Error TakeScreenShot! rc =")+rc));
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
					break;
				}
				hBitmap = CreateBitmap(width, height, 1, 32, buffer);
				pBitmapInfo = CreateBitmapInfoStruct(hBitmap);
				CreateBMPFile(TEXT("D:\\picture2.bmp"), pBitmapInfo, hBitmap, hdc);
				
				SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Picture create!\n")));

				rc = session->UnlockMachine();
				if (!SUCCEEDED(rc))
				{
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(TEXT("Failed sesion unlock! rc =") + rc));
					SendMessage(hList, LB_ADDSTRING, 0, LPARAM(rc));
					break;
				}
			}
Image
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: I need fast help!

Post by noteirak »

Try the two other methods. In Java I need to use takeScreenShotPNGToArray() per example.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Wulfiks
Posts: 13
Joined: 13. Jun 2015, 09:10

Re: I need fast help!

Post by Wulfiks »

Good afternoon. Thanks to your tips, I was able to get a picture, but it turns out like this:

Image

What can I do to get a normal shot?
Thank you in advance.
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: I need fast help!

Post by noteirak »

If you could post an updated code, that would be great.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
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: I need fast help!

Post by noteirak »

Also, just before or after talking a screenshot with your code, run the following command to check that the issue is not at VBox level (replace VMNAME)

Code: Select all

VBoxManage controlvm VMNAME screenshotpng screen.png
then check the content of the screen.png with what you get with the API.
Hyperbox - Virtual Infrastructure Manager - https://apps.kamax.lu/hyperbox/
Manage your VirtualBox infrastructure the free way!
Post Reply