[vbox-dev] takecSreenShot

Klaus Espenlaub klaus.espenlaub at oracle.com
Tue May 21 12:45:09 GMT 2013


On 20.05.2013 11:50, 一生有你 wrote:
> I tried to take a screen shot by takecSreenShot(),,but everytime when i
> trys it, display->TakeScreenShot fails,,and VisualBox.exe crashes....
> I am sure that I can get the correct width and height of the screen
> "printf("Width: %ul Height: %ul Bits Per Pixel: %ul. ",
> width,height,bpp);"......
> my vbox and SDK version are the same 4.1.6....
> please help me ...thank U!!

The cause of this is simple (and the crash is the expected symptom of 
this misuse of the API): you're using an API which only works within the 
VM process from outside the VM process.

The API documentation clearly says "Note: This API can be used only 
locally by a VM process through the COM/XPCOM C++ API as it requires 
pointer support. It is not available for scripting langages, Java or any 
webservice clients. Unless you are writing a new VM frontend use 
takeScreenShotToArray.", and ignoring this is lethal.

The SDK comes with documentation, and there's also the online version: 

The description of the used API method is at 
and warns twice against your attempt to misuse it.

Additionally you ignored the fact that it always returns the screenshot 
with 32bpp, no matter what the current VM framebuffer bpp is.


> ///////here is the code..........bellow
> int testTakeScreenShot(ISession *session)
> {
>      IConsole *console = NULL;
>      IDisplay *display = NULL;
>      IFramebuffer* fb=NULL;
>      unsigned long width, height, bpp;
>      SIZE_T buffersize;
>      BYTE* databuff;
>      HRESULT rc;
>      if (session == NULL)
>        return 1;
>    /* Get console object. */
>      rc=session->get_Console(&console);
>      rc=console->get_Display(&display);
>      display->GetScreenResolution(0,&width,&height,&bpp);
>      printf("Width: %ul Height: %ul Bits Per Pixel: %ul. ",
> width,height,bpp);
>      buffersize = width*height*bpp/8;
>      databuff= (BYTE*)CoTaskMemAlloc(buffersize);
>      printf("%p\n", databuff);
>    // Test databuffer
>    if (databuff != NULL)
>    {
>     rc = display->TakeScreenShot(0,databuff,width,height);
>     if (SUCCEEDED(rc))
>     {
>      printf("Took a screenshot! %d\n", rc);
>     }
>     else
>     {
>      printf("Error taking screen shot! rc = 0x%x\n", rc);
>     }
>     CoTaskMemFree(databuff);
>    }
>   return 0;
> }

More information about the vbox-dev mailing list