internal void CopySampleAndSampleInfo(
            IntPtr sample,
            IntPtr sampleInfo,
            IntPtr argPtr)
        {
            GCHandle          argHandle = GCHandle.FromIntPtr(argPtr);
            SampleCopyArg <T> arg       = (SampleCopyArg <T>)argHandle.Target;

            //object sampleElement = arg.samples[arg.index];
            //CopyOut(sample, ref sampleElement, 0);
            sampleMarshaler.CopyOut(sample, ref arg.samples[arg.index]);
            //arg.samples[arg.index] = sampleElement;
            if (arg.sampleInfos[arg.index] == null)
            {
                arg.sampleInfos[arg.index] = new SampleInfo();
            }
            SampleInfoMarshaler.CopyOut(sampleInfo, ref arg.sampleInfos[arg.index]);
            arg.index++;
            argHandle.Target = arg;
        }
        internal ReturnCode ReaderCopy(
            IntPtr sampleList,
            ref T[] data,
            ref SampleInfo[] sampleInfos)
        {
            T[]          newData;
            SampleInfo[] newSampleInfos;
            ReturnCode   result = DDS.ReturnCode.Ok;

            uint length = Common.SampleList.Length(sampleList);

            if (data != null && length == data.Length)
            {
                newData        = data;
                newSampleInfos = sampleInfos;
            }
            else
            {
                newData        = new T[length];
                newSampleInfos = new SampleInfo[length];
                if (data != null)
                {
                    for (uint i = 0; i < length && i < data.Length; i++)
                    {
                        newData[i]        = data[i];
                        newSampleInfos[i] = sampleInfos[i];
                    }
                }
            }

            if (length > 0)
            {
                SampleCopyArg <T> arg = new SampleCopyArg <T>();
                arg.samples     = newData;
                arg.sampleInfos = newSampleInfos;
                arg.index       = 0;
                GCHandle argHandle = GCHandle.Alloc(arg, GCHandleType.Normal);

                result = uResultToReturnCode(User.Reader.ProtectCopyOutEnter(rlReq_UserPeer));
                if (result == DDS.ReturnCode.Ok)
                {
                    int r = Common.SampleList.Flush(sampleList, CopySampleAndSampleInfo, GCHandle.ToIntPtr(argHandle));
                    User.Reader.ProtectCopyOutExit(rlReq_UserPeer);
                    if (r < 0)
                    {
                        result = DDS.ReturnCode.AlreadyDeleted;
                    }
                }
                data        = arg.samples;
                sampleInfos = arg.sampleInfos;
                argHandle.Free();
            }
            else
            {
                // newData and newSampleInfos have already been set to arrays with 0 elements.
                data        = newData;
                sampleInfos = newSampleInfos;
                result      = DDS.ReturnCode.NoData;
            }
            return(result);
        }