LoadDSArray Example for C++ 6.0 and later

void TestLoadArray(ILEADDicomDSPtr spDataSet)
{
   HRESULT hr;
   VARIANT varData;
   BYTE* pData;
   CFile DicomFile;
   DWORD dwSize;
   LPBYTE pByteArray;
   int nDicomRet;
   CString szClass;
   CString szFlags;
   CString szOut;

   IDicomIODItemPtr pIOD=NULL;

   spDataSet->EnableMethodErrors = FALSE;
   spDataSet->ResetDS();

   //Load the DICOM file from disk
   DicomFile.Open("D:\\Program Files\\LEAD Technologies, Inc\\LEADTOOLS14\\Images\\IMAGE2.dic", CFile::modeRead);
   //Get the size of the file
   dwSize = DicomFile.GetLength();
   //Allocate a buffer to hold the DICOM data
   pByteArray = (LPBYTE) malloc(dwSize);
   memset(pByteArray, 0,dwSize);
   //Read data into buffer
   DicomFile.Read(pByteArray, dwSize);
   DicomFile.Close();
   //Create a safe array to hold the data
   SAFEARRAY* psa = SafeArrayCreateVector(VT_UI1, 0, dwSize);
   VariantInit(&varData);
   varData.vt = VT_ARRAY | VT_UI1;
   varData.parray = psa;
   _variant_t v;
   v.Attach(varData);
   if(psa)
   {
      if( SUCCEEDED( hr = SafeArrayAccessData(psa, (VOID**)&pData)))
      {
         //Put the data into the safe array
         CopyMemory(pData, pByteArray, dwSize);
         hr = SafeArrayUnaccessData( psa );
         if ( SUCCEEDED( hr ) )
         {
            //Load the DICOM file from memory
            nDicomRet = spDataSet->LoadDSArray(v, 0);
            if(nDicomRet == 0)
            {
               //Get the info of the DICOM dataset
               spDataSet->GetInfoDS();
               nDicomRet = spDataSet->FindClassIOD(spDataSet->GetInfoClass());
               if(nDicomRet == 0)
               {
                  //Get the name of the IOD class for this DICOM file and display it
                  pIOD = spDataSet->GetCurrentIOD();
                  BSTR bstr = pIOD->GetName();
                  szClass = bstr;
                  SysFreeString(bstr);
                  szOut.Format("Class: %s", szClass);
                  AfxMessageBox(szOut);
               }
            }
         }
      }
   }

   //Clean up
   v.Detach();
   SafeArrayDestroy(psa);
   free(pByteArray);
   spDataSet->ResetDS();
   spDataSet->EnableMethodErrors = TRUE;
}