Admin Production ni-theme
Current Publication

CA_ServerSetObjData

LabWindows/CVI

CA_ServerSetObjData

HRESULT CA_ServerSetObjData (CAServerObjHandle serverObjectHandle, void *objectData);

Purpose

Sets the data associated with your ActiveX object.

Note   Use the CA_ServerSetObjData, CA_ServerGetObjData, and CA_ServerReleaseObjData functions to manage any data associated with the object. These functions protect the object data from multithread access based on the server's threading model. If you do not use these functions, you must protect any object data from multithread access.

Example Code

The following code demonstrates the use of object data functions:

// Structure to represent the object data
typedef struct __MyObjData {
   double dVal;
   int iVal;
} MyObjData;

// Object callback function
HRESULT CVIFUNC SrvrObjCb (CAServerObjHandle objHandle, const CLSID *pClsid, int event, void *callbackData)
{
   HRESULT hr = S_OK;
   MyObjData *pData = NULL;

   if (event == CA_SERVER_EVENT_OBJECT_CREATE) {
      // Allocating per-object data on the heap
      // Memory will be valid for the lifetime of the object
      pData = CA_AllocMemory (sizeof (MyObjData));
      if (pData) {
         pData->dVal = 3.1415;
         pData->iVal = 4;
         hr = CA_ServerSetObjData (objHandle, pData);
         pData = NULL;
      }
      else {
         hr = E_OUTOFMEMORY;
      }
   }
   else if (event == CA_SERVER_EVENT_OBJECT_DESTROY) {
      hr = CA_ServerGetObjData (objHandle, &pData);
      if (SUCCEEDED (hr)) {
         if (pData) {
            CA_FreeMemory (pData);
            pData = NULL;
         }
         hr = CA_ServerReleaseObjData (objHandle);
      }
   }
   return hr;
}

// Simple function that uses the object data
HRESULT UseObjectData (CAServerObjHandle objHandle, double dV, int iV)
{
   HRESULT hr;
   void *pvObjData = NULL;

   // Get ptr to object data
   hr = CA_ServerGetObjData (objHandle, &pvObjData);
   if (FAILED (hr))
      return hr;

   if (pvObjData) {
      // Use object data
      MyObjData pData = (MyObjData*) pvObjData;
      printf ("Double Data: %f, Int Data: %d");

      pData.dVal = dV;
      pData.iVal = iV;

      // Prevent further use of ptr to object data
      pvObjData = NULL;
   }

   // Release ptr to object data
   // MUST be called because CA_ServerGetObjData succeeded
   hr = CA_ServerReleaseObjData (objHandle);

   return hr;
}

Parameters

Input
Name Type Description
serverObjectHandle CAServerObjHandle The handle to an ActiveX object in your ActiveX server.
objectData void * A pointer to user-defined data to be associated with your ActiveX object.

Note    The ActiveX Library does not make a copy of this data. Do not pass the address of a local variable or any other variable that might not be valid when CA_ServerGetObjData is executed.

Return Value

Name Type Description
status HRESULT A value indicating whether an error occurred. Function failure is indicated by a negative error code.

Error codes are defined in CVIversion\include\cviauto.h and <Program Files>\National Instruments\Shared\MSDTRedistributables\SDKHeaderFiles\8.1\winerror.h. The LabWindows/CVI ActiveX Library explicitly returns error codes. Other error codes in winerror.h are generated by the COM runtime and passed on to you by the ActiveX Library.

You can use CA_GetAutomationErrorString to get the description of an error code or CA_DisplayErrorInfo to display the description of the error code.

Note   You should not return the ActiveX Library error codes from your ActiveX server to your ActiveX clients, unless you document them in your server documentation. An acceptable compromise in this case is to return E_UNEXPECTED to the clients.

The error codes defined in <Program Files>\National Instruments\Shared\MSDTRedistributables\SDKHeaderFiles\8.1\winerror.h are too numerous to display here. These error codes can be returned to your ActiveX clients.

Additional Information

Library: ActiveX Library

Include file: cviauto.h

LabWindows/CVI compatibility: LabWindows/CVI 6.0 and later