Previous page

Next page

Locate page in Contents

Host-only and Shared Networking

The following sample function illustrates how to add virtual network adapters using the host-only and shared networking (both types are created similarly). The steps are:

  1. Call the PrlVm_BeginEdit function to mark the beginning of the virtual machine editing operation. This step is required when modifying any of the virtual machine configuration parameters.
  2. Obtain a handle of type PHT_VM_CONFIGURATION containing the virtual machine configuration information.
  3. Create a new virtual device handle of type PHT_VIRTUAL_DEV_NET_ADAPTER (virtual network adapter) using the PrlVmCfg_CreateVmDev function.
  4. Set the desired device emulation type (host or shared) using the PrlVmDev_SetEmulatedType function. Virtual network adapter emulation types are defined in the PRL_VM_DEV_EMULATION_TYPE enumeration.
  5. The MAC address for the adapter will be generated automatically. If needed, you can set the address manually using the PrlVmDevNet_SetMacAddress function.
  6. Call the PrlVm_Commit function to finalize the changes.

    PRL_RESULT AddNetAdapterHostOrShared(PRL_HANDLE hVm)

    {

    PRL_HANDLE hJobBeginEdit = PRL_INVALID_HANDLE;

    PRL_HANDLE hJobCommit = PRL_INVALID_HANDLE;

    PRL_HANDLE hVmCfg = PRL_INVALID_HANDLE;

    PRL_RESULT nJobRetCode = PRL_INVALID_HANDLE;

    PRL_UINT32 ret = PRL_ERR_UNINITIALIZED;

    // Timestamp the beginning of the configuration changes operation.

    // The hVm parameter specifies the target virtual machine.

    hJobBeginEdit = PrlVm_BeginEdit(hVm);

    ret = PrlJob_Wait(hJobBeginEdit, 10000);

    PrlJob_GetRetCode(hJobBeginEdit, &nJobRetCode);

    if (PRL_FAILED(nJobRetCode))

    {

    fprintf(stderr, "Error: %s\n", prl_result_to_string(nJobRetCode));

    PrlHandle_Free(hJobBeginEdit);

    return nJobRetCode;

    }

    // Obtain a handle of type PHT_VM_CONFIGURATION containing

    // the virtual machine configuration information.

    ret = PrlVm_GetConfig(hVm, &hVmCfg);

    if (PRL_FAILED(ret))

    {

    // Handle the error.

    }

    // Create a virtual network adapter device handle.

    PRL_HANDLE hNet = PRL_INVALID_HANDLE;

    ret = PrlVmCfg_CreateVmDev(

    hVmCfg, // The virtual machine configuration handle.

    PDE_GENERIC_NETWORK_ADAPTER, // Device type.

    &hNet); // Device handle.

    if (PRL_FAILED(ret))

    {

    // Handle the error.

    }

    // For host-only networking, set the device emulation type

    // to PDT_USE_HOST_ONLY_NETWORK, which is an enumerator from the

    // PRL_VM_DEV_EMULATION_TYPE enumeration.

    // For shared networking, set the device emulation type

    // to PDT_USE_SHARED_NETWORK, which is also an enumerator from

    // the same enumeration.

    // Un-comment one of the following lines (and comment out the other)

    // to set the the desired emulation type.

    PRL_VM_DEV_EMULATION_TYPE pdtType = PDT_USE_HOST_ONLY_NETWORK;

    //PRL_VM_DEV_EMULATION_TYPE pdtType = PDT_USE_SHARED_NETWORK;

    ret = PrlVmDev_SetEmulatedType(hNet, pdtType);

    if (PRL_FAILED(ret))

    {

    // Handle the error.

    }

    // By default, a new device is created disabled.

    // You can set the "connected" and "enabled" properties

    // as desired.

    PrlVmDev_SetConnected(hNet, PRL_TRUE);

    PrlVmDev_SetEnabled(hNet, PRL_TRUE);

    // Commit the changes.

    hJobCommit = PrlVm_Commit(hVm);

    ret = PrlJob_Wait(hJobBeginEdit, 10000);

    PrlJob_GetRetCode(hJobBeginEdit, &nJobRetCode);

    if (PRL_FAILED(nJobRetCode))

    {

    // Handle the error.

    }

    // Release all handles.

    PrlHandle_Free(hNet);

    PrlHandle_Free(hVmCfg);

    PrlHandle_Free(hJobBeginEdit);

    PrlHandle_Free(hJobCommit);

    return PRL_ERR_SUCCESS;

    }