Previous page

Next page

Locate page in Contents

Obtaining Virtual Machine Configuration Information

The virtual machine configuration information is obtained using functions of the PHT_VM_CONFIGURATION object. The functions are prefixed with PrlVmCfg_. To use the functions, a handle of type PHT_VM_CONFIGURATION must first be obtained from the virtual machine object (a handle of type PHT_VIRTUAL_MACHINE) using the PrlVm_GetConfig function. The following example shows how to obtain the virtual machine name, guest operating system type and version, RAM size, HDD size, and CPU count. To obtain the virtual machine handle (hVm input parameter), use the helper function described in the Searching for Virtual Machine by Name section.

PRL_RESULT GetVmConfig(PRL_HANDLE hVm)

{

    PRL_RESULT ret = PRL_ERR_UNINITIALIZED;

  

    // Obtain the PHT_VM_CONFIGURATION handle.

    PRL_HANDLE hVmCfg = PRL_INVALID_HANDLE;

    

    ret = PrlVm_GetConfig(hVm, &hVmCfg);

  

    if (PRL_FAILED(ret))

    {

        // Handle the error...

        return ret;

    }

      

    // Get the virtual machine name.

    PRL_STR szVmName;

    PRL_UINT32 nVmNameSize = 0;

        

    // Call once with NULL (PRL_INVALID_HANDLE) to get the size

    // of the buffer to allocate for the VM name.

    PrlVmCfg_GetName(hVmCfg, PRL_INVALID_HANDLE, &nVmNameSize);

        

    // Allocate memory for the VM name.

    szVmName = (PRL_STR)malloc(nVmNameSize);

        

    // Get the virtual machine name.

    PrlVmCfg_GetName(hVmCfg, szVmName, &nVmNameSize);

    printf("Virtual machine name: %s\n", szVmName);

        

    free(szVmName);

        

    // Get the OS type.

    PRL_UINT32 nOsType = 0;

    PrlVmCfg_GetOsType(hVmCfg, &nOsType);

  

    char* sOsTypeName;

    switch (nOsType)

    {

        case PVS_GUEST_TYPE_WINDOWS:

            sOsTypeName = "Windows";

            printf("OS Type: %s\n", PVS_GUEST_TYPE_NAME_WINDOWS);

            break;

        case PVS_GUEST_TYPE_LINUX:

            printf("OS Type: %s\n", PVS_GUEST_TYPE_NAME_LINUX);

            break;

        case PVS_GUEST_TYPE_MACOS:

            printf("OS Type: %s\n", PVS_GUEST_TYPE_NAME_MACOS);

            break;

        case PVS_GUEST_TYPE_FREEBSD:

            printf("OS Type: %s\n", PVS_GUEST_TYPE_NAME_FREEBSD);

            break;

        default:

            printf("OS Type: %s: %d\n", "Other OS Type: ", nOsType);

    }

              

    // Get the OS version.

    PRL_UINT32 nOsVersion = 0;

    PrlVmCfg_GetOsVersion(hVmCfg, &nOsVersion);

    printf("OS Version: %s\n", PVS_GUEST_TO_STRING(nOsVersion));

        

    // Get RAM size.

    PRL_UINT32 nRamSize = 0;

    PrlVmCfg_GetRamSize(hVmCfg, &nRamSize);

    printf("RAM size: %dMB\n", nRamSize);

        

    // Get default HDD size.

    PRL_UINT32 nDefaultHddSize = 0;

    PrlVmCfg_GetDefaultHddSize(nOsVersion, &nDefaultHddSize);  

    printf("Default HDD size: %dMB\n", nDefaultHddSize);

        

    // Get CPU count.

    PRL_UINT32 nCpuCount = 0;

    PrlVmCfg_GetCpuCount(hVmCfg, &nCpuCount);

    printf("Number of CPUs: %d\n", nCpuCount);

  

    return PRL_ERR_SUCCESS;

}