Virtuozzo Virtualization SDK C API Reference
ContentsIndex
Example
PrlSrv_Login Function

The following is a complete sample program that illustrates how to log in to a remote Virtuozzo Service.

#include "Parallels.h"
#include "Wrappers/SdkWrap/SdkWrap.h"
#include <stdio.h>

#ifdef _WIN_
#include <windows.h>
#else
#include <unistd.h>
#endif

const char *szVmName = "Windows XP";
const char *szServer = "127.0.0.1";
const char *szUsername = "SomeUserName";
const char *szPassword = "SomePassword";

int main( int argc, char* argv[] )
{
    PRL_HANDLE hServer;
    PRL_RESULT ret;

    // Use the correct dynamic library depending on the platform.
    #ifdef _WIN_
    #define SDK_LIB_NAME "prl_sdk.dll"
    #elif defined(_LIN_)
    #define SDK_LIB_NAME "libprl_sdk.so"
    #elif defined(_MAC_)
    #define SDK_LIB_NAME "libprl_sdk.dylib"
    #endif

    // Load the SDK library.
    if ( PRL_FAILED( SdkWrap_Load( SDK_LIB_NAME ) ) &&
        PRL_FAILED( SdkWrap_Load( "./" SDK_LIB_NAME ) ) )
    {
        fprintf( stderr, "Failed to load " SDK_LIB_NAME "\n" );
        return -1;
    }

    // Initialize the API.
    ret = PrlApi_Init( PARALLELS_API_VER );
    if ( PRL_FAILED( ret ) )
    {
        fprintf( stderr, "PrlApi_Init returned with error: %s.\n",
            PRL_RESULT_TO_STRING( ret ) );
        PrlApi_Deinit();
        SdkWrap_Unload();
        return ret;
    }

    // Create a server handle.
    ret = PrlSrv_Create( &hServer );
    if ( PRL_FAILED( ret ) )
    {
        fprintf( stderr, "PrlSvr_Create failed, error: %s",
            PRL_RESULT_TO_STRING( ret ) );
        PrlHandle_Free( hServer );
        PrlApi_Deinit();
        SdkWrap_Unload();
        return -1;
    }

    // Log in (asynchronous call).
    PRL_HANDLE hJob = PrlSrv_Login(
            hServer,            // PRL_HANDLE of type PHT_SERVER.
            szServer,           // Name and IP address of the host machine.
            szUsername,         // User name.
            szPassword,         // Password.
            0,                  // Previuos session ID (0 if not used).
            0,                  // Port number (0 for default port).
            0,                  // Timeout value (0 for default timeout).
            PSL_LOW_SECURITY ); // Security level.

    // Wait for a maximum of 10 seconds for
    // the job to complete.
    ret = PrlJob_Wait( hJob, 1000 );
    if ( PRL_FAILED( ret ) )
    {
        fprintf( stderr, "PrlJob_Wait for PrlSrv_Login returned with error: %s\n",
            PRL_RESULT_TO_STRING( ret) );
        PrlHandle_Free( hJob );
        PrlHandle_Free( hServer );
        PrlApi_Deinit();
        SdkWrap_Unload();
        return -1;
    }

    // Analyze the result of PrlSrv_Login.
    PRL_RESULT nJobResult;
    ret = PrlJob_GetRetCode( hJob, &nJobResult );
    if ( PRL_FAILED( nJobResult ) )
    {
        PrlHandle_Free( hJob );
        PrlHandle_Free( hServer );
        printf( "Login job returned with error: %s\n",
            PRL_RESULT_TO_STRING( nJobResult ) );
        PrlHandle_Free( hJob );
        PrlHandle_Free( hServer );
        PrlApi_Deinit();
        SdkWrap_Unload();
        return -1;
    }
    else
        printf( "Login was successful.\n" );

    // Log off.
    hJob = PrlSrv_Logoff( hServer );
    ret = PrlJob_Wait( hJob, 1000 );
    if ( PRL_FAILED( ret ) )
    {
        fprintf( stderr, "PrlJob_Wait for PrlSrv_Logoff returned error: %s\n",
            PRL_RESULT_TO_STRING( ret ) );
        PrlHandle_Free( hVm );
        PrlHandle_Free( hJob );
        PrlHandle_Free( hServer );
        PrlApi_Deinit();
        SdkWrap_Unload();
        return -1;
    }

    ret = PrlJob_GetRetCode( hJob, &nJobResult );
    if ( PRL_FAILED( ret ) )
    {
        fprintf( stderr, "PrlJob_GetRetCode failed for PrlSrv_Logoff with error: %s\n",
            PRL_RESULT_TO_STRING( ret ) );
        PrlHandle_Free( hVm );
        PrlHandle_Free( hJob );
        PrlHandle_Free( hServer );
        PrlApi_Deinit();
        SdkWrap_Unload();
        return -1;
    }

    // Report success or failure of PrlSrv_Logoff.
    if ( PRL_FAILED( nJobResult ) )
    {
        fprintf( stderr, "PrlSrv_Logoff failed with error: %s\n",
            PRL_RESULT_TO_STRING( nJobResult ) );
        PrlHandle_Free( hVm );
        PrlHandle_Free( hJob );
        PrlHandle_Free( hServer );
        PrlApi_Deinit();
        SdkWrap_Unload();
        return -1;
    }
    else
        printf( "Logoff was successful.\n" );

    // Free handles that are no longer required.
    PrlHandle_Free( hVm );
    PrlHandle_Free( hJob );
    PrlHandle_Free( hServer );

    // De-initialize the Virtuozzo API, and unload the SDK.
    PrlApi_Deinit();
    SdkWrap_Unload();

    printf( "\n\nEnd of program.\n\n" );
    char c;
    scanf( &c, "%c" );

    return 0;
}
Copyright ©2016-2017 Parallels International GmbH. All rights reserved.