// GetMachines returns the machines known to VirtualBox. // It returns a slice of Machine instances and any error encountered. func GetMachines() ([]Machine, error) { if err := Init(); err != nil { return nil, err } var cmachinesPtr **C.IMachine var machineCount C.ULONG result := C.GoVboxGetMachines(cbox, &cmachinesPtr, &machineCount) if C.GoVboxFAILED(result) != 0 || (cmachinesPtr == nil && machineCount != 0) { return nil, errors.New( fmt.Sprintf("Failed to get IMachine array: %x", result)) } sliceHeader := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(cmachinesPtr)), Len: int(machineCount), Cap: int(machineCount), } cmachinesSlice := *(*[]*C.IMachine)(unsafe.Pointer(&sliceHeader)) var machines = make([]Machine, machineCount) for i := range cmachinesSlice { machines[i] = Machine{cmachinesSlice[i]} } C.GoVboxArrayOutFree(unsafe.Pointer(cmachinesPtr)) return machines, nil }
// GetMediumFormats returns the guest OS formats supported by VirtualBox. // It returns a slice of MediumFormat instances and any error encountered. func (props *SystemProperties) GetMediumFormats() ([]MediumFormat, error) { var cformatsPtr **C.IMediumFormat var formatCount C.ULONG result := C.GoVboxGetMediumFormats(props.cprops, &cformatsPtr, &formatCount) if C.GoVboxFAILED(result) != 0 || (cformatsPtr == nil && formatCount > 0) { return nil, errors.New( fmt.Sprintf("Failed to get IMediumFormat array: %x", result)) } sliceHeader := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(cformatsPtr)), Len: int(formatCount), Cap: int(formatCount), } cformatsSlice := *(*[]*C.IMediumFormat)(unsafe.Pointer(&sliceHeader)) var formats = make([]MediumFormat, formatCount) for i := range cformatsSlice { formats[i] = MediumFormat{cformatsSlice[i]} } C.GoVboxArrayOutFree(unsafe.Pointer(cformatsPtr)) return formats, nil }
// Unregister removes this from VirtualBox's list of registered machines. // The returned slice of Medium instances is intended to be passed to // DeleteConfig to get all the VM's files cleaned. // It returns an array of detached Medium instances and any error encountered. func (machine *Machine) Unregister(cleanupMode CleanupMode) ([]Medium, error) { var cmediaPtr **C.IMedium var mediaCount C.ULONG result := C.GoVboxMachineUnregister(machine.cmachine, C.PRUint32(cleanupMode), &cmediaPtr, &mediaCount) if C.GoVboxFAILED(result) != 0 || (cmediaPtr == nil && mediaCount != 0) { return nil, errors.New( fmt.Sprintf("Failed to unregister IMachine: %x", result)) } sliceHeader := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(cmediaPtr)), Len: int(mediaCount), Cap: int(mediaCount), } cmediaSlice := *(*[]*C.IMedium)(unsafe.Pointer(&sliceHeader)) var media = make([]Medium, mediaCount) for i := range cmediaSlice { media[i] = Medium{cmediaSlice[i]} } C.GoVboxArrayOutFree(unsafe.Pointer(cmediaPtr)) return media, nil }
// GetGuestOsTypes returns the guest OS types supported by VirtualBox. // It returns a slice of GuestOsType instances and any error encountered. func GetGuestOsTypes() ([]GuestOsType, error) { if err := Init(); err != nil { return nil, err } var ctypesPtr **C.IGuestOSType var typeCount C.ULONG result := C.GoVboxGetGuestOSTypes(cbox, &ctypesPtr, &typeCount) if C.GoVboxFAILED(result) != 0 || (ctypesPtr == nil && typeCount != 0) { return nil, errors.New( fmt.Sprintf("Failed to get IGuestOSType array: %x", result)) } sliceHeader := reflect.SliceHeader{ Data: uintptr(unsafe.Pointer(ctypesPtr)), Len: int(typeCount), Cap: int(typeCount), } ctypesSlice := *(*[]*C.IGuestOSType)(unsafe.Pointer(&sliceHeader)) var types = make([]GuestOsType, typeCount) for i := range ctypesSlice { types[i] = GuestOsType{ctypesSlice[i]} } C.GoVboxArrayOutFree(unsafe.Pointer(ctypesPtr)) return types, nil }
// TakeScreenShotToArray takes a screenshot of the VM's display. // It returns a byte slice encoding the image as RGBA, and any error // encountered. func (display *Display) TakeScreenShotToArray(screenId uint, width uint, height uint) ([]byte, error) { var cdataPtr *C.PRUint8 var dataSize C.PRUint32 result := C.GoVboxDisplayTakeScreenShotToArray(display.cdisplay, C.PRUint32(screenId), C.PRUint32(width), C.PRUint32(height), &dataSize, &cdataPtr) if C.GoVboxFAILED(result) != 0 { return nil, errors.New( fmt.Sprintf("Failed to take IDisplay screenshot: %x", result)) } data := C.GoBytes(unsafe.Pointer(cdataPtr), C.int(dataSize)) C.GoVboxArrayOutFree(unsafe.Pointer(cdataPtr)) return data, nil }