Admin Production ni-theme
Current Publication

CA_ServerGetObjData

LabWindows/CVI

CA_ServerGetObjData

HRESULT CA_ServerGetObjData (CAServerObjHandle serverObjectHandle, void *objectData);

Purpose

Obtains the data associated with your ActiveX object.

Note   You should 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, then you are responsible for protecting any object data from multithread access.
Note   This function acquires a lock to protect your data from multithread access. When you finish using the data returned by this function, you must call CA_ServerReleaseObjData to release the lock.

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.
Output
Name Type Description
objectData void * Data associated with your ActiveX object.

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