VMware Programming API Sample Code: callbackProc.c

VMware Programming API Sample Code

callbackProc.c

Copyright © 2008 VMware, Inc. All rights reserved.


// This demonstrates how to use callback procedures when calling asynchronous
// Vix functions.
//
// The main caller thread in this sample is simple, and just spins while
// waiting for the fucntion to return. A more sophisticated example could
// do other work.
//
// This sample was written for windows, but the same ideas apply to linux.
// Just replace the Sleep windows call with something appropriate.

#include "stdafx.h"
#include "windows.h"
#include "vix.h"

VixHandle hostHandle = VIX_INVALID_HANDLE;

void MyCallbackProc(VixHandle jobHandle,
                    VixEventType eventType,
                    VixHandle moreEventInfo,
                    void *clientData);


////////////////////////////////////////////////////////////////////////////////
int _tmain(int argc, _TCHAR* argv[])
{
    VixError err;
    VixHandle jobHandle = VIX_INVALID_HANDLE;
    VixHandle vmHandle = VIX_INVALID_HANDLE;
    int jobIsReady = 0;

    jobIsReady = 0;
    jobHandle = VixHost_Connect(VIX_API_VERSION,
                                VIX_SERVICEPROVIDER_VMWARE_SERVER,
                                NULL, // *hostName,
                                0, // hostPort,
                                NULL, // *userName,
                                NULL, // *password,
                                0, // options,
                                VIX_INVALID_HANDLE, // propertyListHandle,
                                &MyCallbackProc, // *callbackProc,
                                &jobIsReady); // *clientData);
    while (!jobIsReady) {
       Sleep(100);
    }

    err = Vix_GetProperties(jobHandle,
                            VIX_PROPERTY_JOB_RESULT_HANDLE,
                            &hostHandle,
                            VIX_PROPERTY_NONE);
    if (VIX_OK != err) {
        goto abort;
    }

    Vix_ReleaseHandle(jobHandle);
    jobIsReady = 0;
    jobHandle = VixVM_Open(hostHandle,
                           "C:\\Virtual Machines\\Windows XP Pro\\Windows XP Professional.vmx",
                           MyCallbackProc, // VixEventProc *callbackProc,
                           &jobIsReady); // void *clientData);
    while (!jobIsReady) {
       Sleep(100);
    }

    err = Vix_GetProperties(jobHandle,
                            VIX_PROPERTY_JOB_RESULT_HANDLE,
                            &vmHandle,
                            VIX_PROPERTY_NONE);
    if (VIX_OK != err) {
        goto abort;
    }

    Vix_ReleaseHandle(jobHandle);
    jobIsReady = 0;
    jobHandle = VixVM_PowerOn(vmHandle,
                              VIX_VMPOWEROP_NORMAL,
                              VIX_INVALID_HANDLE,
                              MyCallbackProc, // *callbackProc,
                              &jobIsReady); // *clientData);
    while (!jobIsReady) {
       Sleep(100);
    }

    Vix_ReleaseHandle(jobHandle);
    jobIsReady = 0;
    jobHandle = VixVM_PowerOff(vmHandle,
                              VIX_VMPOWEROP_NORMAL,
                              MyCallbackProc, // *callbackProc,
                              &jobIsReady); // *clientData);
    while (!jobIsReady) {
       Sleep(100);
    }

    VixHost_Disconnect(hostHandle);
    goto done;

abort:
	return 0;

done:
    Vix_ReleaseHandle(jobHandle);
    Vix_ReleaseHandle(vmHandle);
}




////////////////////////////////////////////////////////////////////////////////
void
MyCallbackProc(VixHandle jobHandle,
               VixEventType eventType,
               VixHandle moreEventInfo,
               void *clientData)
{
   int *flagPointer = NULL;

   /*
    * Ignore any events other than a job completing.
    */
   if (VIX_EVENTTYPE_JOB_COMPLETED != eventType) {
      return;
   }

   flagPointer = (int *) clientData;
   if (NULL == flagPointer) {
      return;
   }

   *flagPointer = 1;
} // MyCallbackProc

If you have obtained this sample code as part of the VMware Server 1.0 SDK, or are using this sample code in conjunction with the VMware Server 1.0 SDK, the license terms of that SDK will govern your use of this sample code.

Otherwise, the sample code is provided "AS-IS" for use, modification, and redistribution in source and binary forms, provided that the copyright notice and this following list of conditions are retained and/or reproduced in your distribution. To the maximum extent permitted by law, VMware, Inc., its subsidiaries and affiliates hereby disclaim all express, implied and/or statutory warranties, including duties or conditions of merchantability, fitness for a particular purpose, and non-infringement of intellectual property rights. IN NO EVENT WILL VMWARE, ITS SUBSIDIARIES OR AFFILIATES BE LIABLE TO ANY OTHER PARTY FOR THE COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, INDIRECT, OR SPECIAL DAMAGES, ARISING OUT OF THIS OR ANY OTHER AGREEMENT RELATING TO THE SAMPLE CODE.

You agree to defend, indemnify and hold harmless VMware, and any of its directors, officers, employees, agents, affiliates, or subsidiaries from and against all losses, damages, costs and liabilities arising from your use, modification and distribution of the sample code.

VMware does not certify or endorse your use of the sample code, nor is any support or other service provided in connection with the sample code.