Previous page

Next page

Locate page in Contents

Obtaining a List of Templates

A list of virtual machines and virtual machine templates are obtained from the server using the same function: PrlSrv_GetVmList . A template is identified by calling the PrlVmCfg_IsTemplate function which returns a boolean value indicating whether the specified virtual machine handle contains information about a regular virtual or a handle. The value of PRL_TRUE indicates that the machine is a template. The value of PRL_FALSE indicates that the machine is a regular virtual machine. The following sample is identical to the sample provided in the Obtaining a List of Virtual Machines section with the exception that it was modified to display only the lists of templates on the screen:

PRL_RESULT GetTemplateList(const PRL_HANDLE &hServer)

{

PRL_HANDLE hJob = PRL_INVALID_HANDLE;

PRL_HANDLE hJobResult = PRL_INVALID_HANDLE;

PRL_RESULT ret = PRL_ERR_UNINITIALIZED;

PRL_RESULT nJobReturnCode = PRL_ERR_UNINITIALIZED;

// Get a list of the available virtual machines.

hJob = PrlSrv_GetVmList(hServer);

// Wait for a maximum of 10 seconds for PrlSrv_GetVmList.

ret = PrlJob_Wait(hJob, 10000);

if (PRL_FAILED(ret))

{

fprintf(stderr,

"PrlJob_Wait for PrlSrv_GetVmList returned with error: %s\n",

prl_result_to_string(ret));

PrlHandle_Free(hJob);

return ret;

}

// Check the results of PrlSrv_GetVmList.

ret = PrlJob_GetRetCode(hJob, &nJobReturnCode);

if (PRL_FAILED(ret))

{

fprintf(stderr, "PrlJob_GetRetCode returned with error: %s\n",

prl_result_to_string(ret));

PrlHandle_Free(hJob);

return ret;

}

if (PRL_FAILED(nJobReturnCode))

{

fprintf(stderr, "PrlSrv_GetVmList returned with error: %s\n",

prl_result_to_string(ret));

PrlHandle_Free(hJob);

return ret;

}

// Get the results of PrlSrv_GetVmList.

ret = PrlJob_GetResult(hJob, &hJobResult);

if (PRL_FAILED(ret))

{

fprintf(stderr, "PrlJob_GetResult returned with error: %s\n",

prl_result_to_string(ret));

PrlHandle_Free(hJob);

return ret;

}

// Handle to result object is available,

// job handle is no longer needed, so free it.

PrlHandle_Free(hJob);

// Iterate through the results (list of virtual machines returned).

PRL_UINT32 nParamsCount = 0;

ret = PrlResult_GetParamsCount(hJobResult, &nParamsCount);

for (PRL_UINT32 i = 0; i < nParamsCount; ++i)

{

// Virtual machine handle

PRL_HANDLE hVm = PRL_INVALID_HANDLE;

// Get a handle to result at index i.

PrlResult_GetParamByIndex(hJobResult, i, &hVm);

// Obtain the PHT_VM_CONFIGURATION object.

PRL_HANDLE hVmCfg = PRL_INVALID_HANDLE;

ret = PrlVm_GetConfig(hVm, &hVmCfg);

// See if the handle contains information about a template.

PRL_BOOL isTemplate = PRL_FALSE;

PrlVmCfg_IsTemplate(hVmCfg, &isTemplate);

// If this is not a template, proceed to the next

// virtual machine in the list.

if (isTemplate == PRL_FALSE)

{

PrlHandle_Free(hVmCfg);

PrlHandle_Free(hVm);

continue;

}

// Get the name of the template for result i.

char szVmNameReturned[1024];

PRL_UINT32 nBufSize = sizeof(szVmNameReturned);

ret = PrlVmCfg_GetName(hVmCfg, szVmNameReturned, &nBufSize);

if (PRL_FAILED(ret))

{

printf("PrlVmCfg_GetName returned with error (%s)\n",

prl_result_to_string(ret));

}

else

{

printf("Template name: '%s'.\n",

szVmNameReturned);

}

PrlHandle_Free(hVm);

PrlHandle_Free(hVmCfg);

}

return PRL_ERR_SUCCESS;

}