Previous page

Next page

Locate page in Contents

Compiling with Framework

If you are using the ParallelsVirtualizationSDK framework, the program must contain the following include directive:

#include "ParallelsVirtualizationSDK/Parallels.h"

Parallels.h is the main SDK header file. Please note the framework name in front of the SDK header file name. This is a common requirement when using a framework.

Note: The difference between the SdkWrap scenario (described in the previous subsection) and the framework scenario is that Parallels.h must be included when using the framework, while SdkWrap.h must be included when using SdkWrap. The two files must never be included together. Please also note that you don't have to load the dynamic library manually in your program when using the framework.

The only compiler option that must be specified when using the framework is:

-framework ParallelsVirtualizationSDK

Using Makefile

The following sample Makefile can be used to compile a program using the ParallelsVirtualizationSDK framework:

# Source file name.

# Substitute the file name with your own.

SOURCE = HelloWorld

    

# Target executable file name.

# Here we are using the same name as the source file name.

TARGET = $(SOURCE)

  

CXX = g++

LDFLAGS = -framework ParallelsVirtualizationSDK

    

all : $(TARGET)

  

$(TARGET) : $(OBJS)

$(CXX) -o $@ $(LDFLAGS) $(OBJS)

  

$(SOURCE).o : $(SOURCE).cpp

$(CXX) -c -o $@ $(SOURCE).cpp

  

clean:

@rm -f $(OBJS) $(TARGET)

  

.PHONY : all clean

Using Xcode IDE

When setting up an Xcode project, the only thing that you have to do is add the ParallelsVirtualizationSDK framework to the project. No other project modifications are necessary.

Sample

The following is a complete sample program that demonstrates the usage of the ParallelsVirtualizationSDK framework.  

#include "ParallelsVirtualizationSDK/Parallels.h"

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

  

#ifdef _WIN_

#include <windows.h>

#else

#include <unistd.h>

#endif

  

PRL_RESULT LoginLocal(PRL_HANDLE &hServer);

PRL_RESULT LogOff(PRL_HANDLE &hServer);

  

/////////////////////////////////////////////////////////////////////

  

int main(int argc, char* argv[])

{

    // Variables for handles.

    PRL_HANDLE hServer = PRL_INVALID_HANDLE;    // server handle

  

    // Variables for return codes.

    PRL_RESULT err = PRL_ERR_UNINITIALIZED;

  

    // Log in.

    err = LoginLocal(hServer);

  

    // Log off

    err = LogOff(hServer);

  

    printf( "\nEnd of program.\n\n" );

    printf("Press Enter to exit...");

    getchar();

  

    exit(0);

}

  

  

// Intializes the SDK library and

// logs in to the local Parallels Service.

//

PRL_RESULT LoginLocal(PRL_HANDLE &hServer)

{

    // Variables for handles.

    PRL_HANDLE hJob = PRL_INVALID_HANDLE; // job handle

  

    // Variables for return codes.

    PRL_RESULT err = PRL_ERR_UNINITIALIZED;

    PRL_RESULT nJobReturnCode = PRL_ERR_UNINITIALIZED;

  

    // Initialize the API. In this example, we are initializing the

    // API for Parallels Workstation.

    // To initialize in the Parallels Desktop mode, pass PAM_DESKTOP

    // as the second parameter.

    // To initialize for Parallels Server, pass PAM_SERVER.

    // See the PRL_APPLICATION_MODE enumeration for all possible options.

    err = PrlApi_InitEx(PARALLELS_API_VER, PAM_DESKTOP, 0, 0);

  

    if (PRL_FAILED(err))

    {

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

            prl_result_to_string(err));

        PrlApi_Deinit();

        return -1;

    }

  

    // Create a server handle (PHT_SERVER).

    err = PrlSrv_Create(&hServer);

    if (PRL_FAILED(err))

    {

        fprintf(stderr, "PrlSvr_Create failed, error: %s",

            prl_result_to_string(err));

        PrlApi_Deinit();

        return -1;

    }

  

    // Log in (asynchronous call).

    hJob = PrlSrv_LoginLocal(hServer, NULL, NULL, PSL_NORMAL_SECURITY);

  

    // Wait for a maximum of 10 seconds for

    // the job to complete.

    err = PrlJob_Wait(hJob, 1000);

    if (PRL_FAILED(err))

    {

        fprintf(stderr,

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

            prl_result_to_string(err));

        PrlHandle_Free(hJob);

        PrlHandle_Free(hServer);

        PrlApi_Deinit();

        return -1;

    }

  

    // Analyze the result of PrlSrv_Login.

    err = PrlJob_GetRetCode(hJob, &nJobReturnCode);

  

    // First, check PrlJob_GetRetCode success/failure.

    if (PRL_FAILED(err))

    {

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

            prl_result_to_string(err));

        PrlHandle_Free(hJob);

        PrlHandle_Free(hServer);

        PrlApi_Deinit();

        return -1;

    }

  

    // Now check the Login operation success/failure.

    if (PRL_FAILED(nJobReturnCode))

    {

        PrlHandle_Free(hJob);

        PrlHandle_Free(hServer);

        printf("Login job returned with error: %s\n",

            prl_result_to_string(nJobReturnCode));

        PrlHandle_Free(hJob);

        PrlHandle_Free(hServer);

        PrlApi_Deinit();

        return -1;

    }

    else

    {

        printf( "Login was successful.\n" );

    }

  

    return 0;

}

  

// Log off the Parallels Service and

// deinitializes the SDK library.

//

PRL_RESULT LogOff(PRL_HANDLE &hServer)

{

    PRL_HANDLE hJob = PRL_INVALID_HANDLE;

  

    PRL_RESULT err = PRL_ERR_UNINITIALIZED;

    PRL_RESULT nJobReturnCode = PRL_ERR_UNINITIALIZED;

  

    // Log off.

    hJob = PrlSrv_Logoff(hServer);

    err = PrlJob_Wait(hJob, 1000);

    if (PRL_FAILED(err))

    {

        fprintf(stderr, "PrlJob_Wait for PrlSrv_Logoff returned error: %s\n",

            prl_result_to_string(err));

        PrlHandle_Free(hJob);

        PrlHandle_Free(hServer);

        PrlApi_Deinit();

        return -1;

    }

  

    // Get the Logoff operation return code.

    err = PrlJob_GetRetCode(hJob, &nJobReturnCode);

  

    // Check the PrlJob_GetRetCode success/failure.

    if (PRL_FAILED(err))

    {

        fprintf(stderr, "PrlJob_GetRetCode failed for PrlSrv_Logoff with error: %s\n",

            prl_result_to_string(err));

        PrlHandle_Free(hJob);

        PrlHandle_Free(hServer);

        PrlApi_Deinit();

        return -1;

    }

  

    // Report success or failure of PrlSrv_Logoff.

    if (PRL_FAILED(nJobReturnCode))

    {

        fprintf(stderr, "PrlSrv_Logoff failed with error: %s\n",

            prl_result_to_string(nJobReturnCode));

        PrlHandle_Free(hJob);

        PrlHandle_Free(hServer);

        PrlApi_Deinit();

        return -1;

    }

    else

    {

        printf( "Logoff was successful.\n" );

    }

  

    // Free handles that are no longer required.

    PrlHandle_Free(hJob);

    PrlHandle_Free(hServer);

  

    // De-initialize the Parallels API, and unload the SDK.

    PrlApi_Deinit();

  

    return 0;

}