Exemple #1
0
        public void Add(PropertyKey key, IEnumerable <int> values)
        {
            IPortableDevicePropVariantCollection col = (IPortableDevicePropVariantCollection) new PortableDevicePropVariantCollection();

            foreach (var value in values)
            {
                var var = PropVariantFacade.IntToPropVariant(value);
                col.Add(ref var.Value);
            }
            this.values.SetIPortableDevicePropVariantCollectionValue(ref key, col);
        }
Exemple #2
0
        /// <summary>
        /// MTPコマンドのパラメータを構築する
        /// </summary>
        /// <param name="param">パラメータ</param>
        /// <returns></returns>
        private static IPortableDevicePropVariantCollection createMtpCommandParameter(uint[] param)
        {
            IPortableDevicePropVariantCollection mtpCommandParameter
                = (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection();

            foreach (uint p in param)
            {
                tag_inner_PROPVARIANT propValiant = createPropVariant(p);
                mtpCommandParameter.Add(ref propValiant);
            }

            return(mtpCommandParameter);
        }
        public MTPDataResponse ExecuteReadData(uint code, params uint[] parameters)
        {
            IPortableDevicePropVariantCollection propVariant =
                (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection();

            foreach (uint parameter in parameters)
            {
                tag_inner_PROPVARIANT vparam1 = new tag_inner_PROPVARIANT();
                UintToPropVariant(parameter, out vparam1);
                propVariant.Add(ref vparam1);
            }
            return(ExecuteReadData(code, propVariant));
        }
        public uint ExecuteWithNoData(uint code, params uint[] parameters)
        {
            IPortableDeviceValues commandValues =
                (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();
            IPortableDevicePropVariantCollection propVariant =
                (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection();
            IPortableDeviceValues results;

            //commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid);
            commandValues.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY,
                                       PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITHOUT_DATA_PHASE.fmtid);
            commandValues.SetUnsignedIntegerValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID,
                                                  PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITHOUT_DATA_PHASE.
                                                  pid);

            foreach (uint parameter in parameters)
            {
                tag_inner_PROPVARIANT vparam = new tag_inner_PROPVARIANT();
                UintToPropVariant(parameter, out vparam);
                propVariant.Add(ref vparam);
            }

            commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_CODE, code);

            commandValues.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant);

            // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx)
            this.portableDeviceClass.SendCommand(0, commandValues, out results);
            //int pvalue = 0;
            try
            {
                int iValue = 0;
                results.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out iValue);
                if (iValue != 0)
                {
                    return((uint)iValue);
                }
                uint pValue = 0;
                results.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out pValue);
                if (pValue != 0)
                {
                    return(pValue);
                }
            }
            catch (Exception)
            {
            }
            //results.GetSignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out pvalue);
            return(0);
        }
        public MTPDataResponse ExecuteReadBigData(uint code, TransferCallback callback, params uint[] parameters)
        {
            MTPDataResponse res = new MTPDataResponse();

            // source: http://msdn.microsoft.com/en-us/library/windows/desktop/ff384843(v=vs.85).aspx
            // and view-source:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26860397.html
            // error codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd319335(v=vs.85).aspx
            byte[] imgdate = new byte[8];

            IPortableDeviceValues commandValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();
            IPortableDeviceValues pParameters   = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues();

            IPortableDevicePropVariantCollection propVariant =
                (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection();
            IPortableDeviceValues pResults;

            //commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid);
            commandValues.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY,
                                       PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.fmtid);
            commandValues.SetUnsignedIntegerValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID,
                                                  PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ.pid);
            commandValues.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref imgdate[0], (uint)imgdate.Length);

            foreach (uint parameter in parameters)
            {
                tag_inner_PROPVARIANT vparam = new tag_inner_PROPVARIANT();
                UintToPropVariant(parameter, out vparam);
                propVariant.Add(ref vparam);
            }

            commandValues.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant);
            commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_CODE, (uint)code);

            // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx)
            this.portableDeviceClass.SendCommand(0, commandValues, out pResults);

            try
            {
                int pValue = 0;
                pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue);
                if (pValue != 0)
                {
                    // check if the device is busy, and after 100 ms seconds try again
                    if (((uint)pValue) == PortableDeviceErrorCodes.ERROR_BUSY)
                    {
                        Thread.Sleep(50);
                        return(ExecuteReadBigData(code, callback, parameters));
                    }
                }
            }
            catch (Exception)
            {
            }
            //string pwszContext = string.Empty;
            //pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out pwszContext);
            //uint cbReportedDataSize = 0;
            //pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out cbReportedDataSize);


            uint   tmpBufferSize      = 0;
            uint   tmpTransferSize    = 0;
            string tmpTransferContext = string.Empty;

            {
                pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out tmpTransferContext);
                pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out tmpBufferSize);
                pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPTIMAL_TRANSFER_BUFFER_SIZE, out tmpTransferSize);

                try
                {
                    int pValue;
                    pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue);
                    if (pValue != 0)
                    {
                        return(null);
                    }
                }
                catch
                {
                }
            }


            pParameters.Clear();
            pResults.Clear();
            uint offset = 0;

            res.Data = new byte[(int)tmpBufferSize];
            bool cont = true;

            do
            {
                if (offset + tmpTransferSize >= tmpBufferSize)
                {
                    cont            = false;
                    tmpTransferSize = (uint)(tmpBufferSize - offset);
                }

                byte[] tmpData = new byte[(int)tmpTransferSize];
                pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY,
                                         PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.fmtid);
                pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID,
                                                    PortableDevicePKeys.WPD_COMMAND_MTP_EXT_READ_DATA.pid);
                pParameters.SetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext);
                pParameters.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref tmpData[0],
                                           (uint)tmpTransferSize);
                pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_READ,
                                                    (uint)tmpTransferSize);
                pParameters.SetIPortableDevicePropVariantCollectionValue(
                    ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant);


                portableDeviceClass.SendCommand(0, pParameters, out pResults);


                uint cbBytesRead = 0;

                try
                {
                    int pValue = 0;
                    pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue);
                    if (pValue != 0)
                    {
                        res.ErrorCode = (uint)pValue;
                        return(res);
                    }
                }
                catch (Exception)
                {
                }

                callback((int)tmpBufferSize, (int)offset);

                GCHandle pinnedArray = GCHandle.Alloc(imgdate, GCHandleType.Pinned);
                IntPtr   ptr         = pinnedArray.AddrOfPinnedObject();

                uint dataread = 0;
                pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_READ,
                                                 out dataread);
                pResults.GetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ptr, out cbBytesRead);

                IntPtr tmpPtr = new IntPtr(Marshal.ReadInt64(ptr));

                //Marshal.Copy(tmpPtr, res.Data, (int)offset, (int)cbBytesRead);

                for (int i = 0; i < cbBytesRead; i++)
                {
                    res.Data[offset + i] = Marshal.ReadByte(tmpPtr, i);
                }

                Marshal.FreeHGlobal(tmpPtr);
                pinnedArray.Free();

                offset += cbBytesRead;
            } while (cont);

            pParameters.Clear();
            pResults.Clear();
            {
                pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.fmtid);
                pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.pid);
                pParameters.SetStringValue(PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext);
            }

            portableDeviceClass.SendCommand(0, pParameters, out pResults);

            try
            {
                int tmpResult = 0;

                pResults.GetErrorValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out tmpResult);
                if (tmpResult != 0)
                {
                }
            }
            catch
            {
            }
            return(res);
        }
        public uint ExecuteWriteData(uint code, byte[] data, params uint[] parameters)
        {
            // source: http://msdn.microsoft.com/en-us/library/windows/desktop/ff384843(v=vs.85).aspx
            // and view-source:http://www.experts-exchange.com/Programming/Languages/C_Sharp/Q_26860397.html
            // error codes http://msdn.microsoft.com/en-us/library/windows/desktop/dd319335(v=vs.85).aspx
            //byte[] imgdate = new byte[8];

            IPortableDeviceValues commandValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass();
            IPortableDeviceValues pParameters   = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues();

            IPortableDevicePropVariantCollection propVariant =
                (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection();
            IPortableDeviceValues pResults;

            //commandValues.SetGuidValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref command.fmtid);
            commandValues.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY,
                                       PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_WRITE.fmtid);
            commandValues.SetUnsignedIntegerValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID,
                                                  PortableDevicePKeys.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_WRITE.pid);
            commandValues.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref data[0], (uint)data.Length);

            foreach (uint parameter in parameters)
            {
                tag_inner_PROPVARIANT vparam = new tag_inner_PROPVARIANT();
                UintToPropVariant(parameter, out vparam);
                propVariant.Add(ref vparam);
            }

            commandValues.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant);
            commandValues.SetUnsignedLargeIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE,
                                                       (ulong)data.Length);
            commandValues.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_CODE, (uint)code);

            // According to documentation, first parameter should be 0 (see http://msdn.microsoft.com/en-us/library/dd375691%28v=VS.85%29.aspx)
            this.portableDeviceClass.SendCommand(0, commandValues, out pResults);

            try
            {
                int pValue = 0;
                pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue);
                if (pValue != 0)
                {
                    return((uint)pValue);
                }
                //uint mtperror;
                //pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out mtperror);
                //if (mtperror != 0)
                //  return mtperror;
            }
            catch (Exception)
            {
            }
            string tmpTransferContext = string.Empty;

            pResults.GetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out tmpTransferContext);


            pParameters.Clear();
            pResults.Clear();

            pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_WRITE_DATA.fmtid);
            pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_WRITE_DATA.pid);
            pParameters.SetStringValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext);
            pParameters.SetBufferValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref data[0], (uint)data.Length);
            pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_WRITE, (uint)data.Length);
            pParameters.SetIPortableDevicePropVariantCollectionValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, propVariant);


            portableDeviceClass.SendCommand(0, pParameters, out pResults);


            try
            {
                int pValue = 0;
                pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue);
                if (pValue != 0)
                {
                    return((uint)pValue);
                }
                //uint mtperror;
                //pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out mtperror);
                //if (mtperror != 0)
                //  return mtperror;
            }
            catch (Exception)
            {
            }


            pParameters.Clear();
            pResults.Clear();
            {
                pParameters.SetGuidValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.fmtid);
                pParameters.SetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_COMMAND_ID, PortableDevicePKeys.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.pid);
                pParameters.SetStringValue(PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, tmpTransferContext);
            }

            portableDeviceClass.SendCommand(0, pParameters, out pResults);


            try
            {
                int tmpResult = 0;

                pResults.GetErrorValue(ref PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out tmpResult);
                if (tmpResult != 0)
                {
                    return((uint)tmpResult);
                }
                uint mtperror;
                pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out mtperror);
                if (mtperror != 0)
                {
                    return(mtperror);
                }
            }
            catch
            {
            }
            return(0);
        }