public static IEnumerable <string> ToStrings(this IPortableDevicePropVariantCollection col) { uint count = 0; col.GetCount(ref count); for (uint i = 0; i < count; i++) { using (PropVariantFacade val = new PropVariantFacade()) { col.GetAt(i, ref val.Value); yield return(val.ToString()); } } }
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 static IEnumerable <TEnum> ToEnum <TEnum>(this IPortableDevicePropVariantCollection col) where TEnum : struct // enum { uint count = 0; col.GetCount(ref count); for (uint i = 0; i < count; i++) { using (PropVariantFacade val = new PropVariantFacade()) { col.GetAt(i, ref val.Value); yield return(GetEnumFromAttrGuid <TEnum>(val.ToGuid())); } } }
/// <summary> /// EndDataTransferを送信する /// </summary> /// <param name="device"></param> /// <param name="context"></param> /// <returns></returns> private static void sendEndDataTransfer(PortableDevice device, string context, out uint responseCode, out uint[] responseParam) { IPortableDeviceValues mtpEndDataTransfer = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); mtpEndDataTransfer.SetGuidValue(ref WpdProperty.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref WpdProperty.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.fmtid); mtpEndDataTransfer.SetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_COMMON_COMMAND_ID, WpdProperty.WPD_COMMAND_MTP_EXT_END_DATA_TRANSFER.pid); mtpEndDataTransfer.SetStringValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, context); IPortableDeviceValues spResults; device.SendCommand(0, mtpEndDataTransfer, out spResults); Marshal.ReleaseComObject(mtpEndDataTransfer); int ret = 1; spResults.GetErrorValue(ref WpdProperty.WPD_PROPERTY_COMMON_HRESULT, out ret); if (ret != 0) { Marshal.ReleaseComObject(spResults); responseCode = 0; responseParam = null; return; } // レスポンスコード spResults.GetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out responseCode); // パラメータ responseParam = null; if (responseCode == 0x2001) { IPortableDevicePropVariantCollection resultValues = (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection(); spResults.GetIPortableDevicePropVariantCollectionValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_RESPONSE_PARAMS, out resultValues); uint count = 1; resultValues.GetCount(ref count); responseParam = new uint[count]; for (uint i = 0; i < count; i++) { tag_inner_PROPVARIANT value = new tag_inner_PROPVARIANT(); resultValues.GetAt(i, ref value); responseParam[i] = getUintValue(value); } Marshal.ReleaseComObject(resultValues); } Marshal.ReleaseComObject(spResults); }
public static Guid[] ToGuid(this IPortableDevicePropVariantCollection col) { uint count = 0; col.GetCount(ref count); var result = new Guid[count]; for (uint i = 0; i < count; i++) { PROPVARIANT val = new PROPVARIANT(); col.GetAt(i, ref val); result[i] = PropVariant.FromValue(val); } return(result); }
public static TEnum[] ToArray <TEnum>(this IPortableDevicePropVariantCollection col) where TEnum : struct // enum { uint count = 0; col.GetCount(ref count); var result = new TEnum[count]; for (uint i = 0; i < count; i++) { PROPVARIANT val = new PROPVARIANT(); col.GetAt(i, ref val); result[i] = GetEnumFromAttrGuid <TEnum>(PropVariant.FromValue(val)); } return(result); }
public static void WriteObject(IPortableDevicePropVariantCollection collection) { Trace.WriteLine("###############################"); uint num = 0; collection.GetCount(ref num); for (uint index = 0; index < num; index++) { using (PropVariantFacade val = new PropVariantFacade()) { collection.GetAt(index, ref val.Value); Trace.WriteLine($"##### {val.ToString()}"); } } }
public MTPDataResponse ExecuteReadData(uint code, params uint[] parameters) { IPortableDevicePropVariantCollection propVariant = (IPortableDevicePropVariantCollection) new PortableDeviceTypesLib.PortableDevicePropVariantCollection(); foreach (uint parameter in parameters) { if (parameter != 0) { tag_inner_PROPVARIANT vparam1 = new tag_inner_PROPVARIANT(); UintToPropVariant(parameter, out vparam1); propVariant.Add(ref vparam1); } } return(ExecuteReadData(code, propVariant)); }
public void Delete(bool recursive = false) { var objectIdCollection = (IPortableDevicePropVariantCollection) new PortableDevicePropVariantCollection(); var propVariantValue = PropVariantFacade.StringToPropVariant(this.Id); objectIdCollection.Add(ref propVariantValue.Value); IPortableDevicePropVariantCollection results = (IPortableDevicePropVariantCollection) new PortableDevicePropVariantCollection(); // TODO: get the results back and handle failures correctly this.device.deviceContent.Delete(recursive ? PORTABLE_DEVICE_DELETE_WITH_RECURSION : PORTABLE_DEVICE_DELETE_NO_RECURSION, objectIdCollection, ref results); ComTrace.WriteObject(objectIdCollection); }
/// <summary> /// データ受信を伴うMTPオペレーションを実行する /// </summary> /// <param name="device"></param> /// <param name="code"></param> /// <param name="param"></param> /// <returns></returns> private static MtpResponse executeDataReadCommand(PortableDevice device, ushort code, uint[] param, bool noReadResponseParam) { IPortableDeviceValues spResults; int ret; string context; uint size; byte[] bufferIn; IntPtr dataAddress; uint readedSize; IntPtr dataPtr; byte[] bufferOut = null; uint responseCode; uint[] responseParam; // MTPコマンドとパラメータを構築する IPortableDeviceValues mtpCommand = createMtpCommand(code, WpdProperty.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_READ); IPortableDevicePropVariantCollection mtpCommandParam = null; if (param != null) { mtpCommandParam = createMtpCommandParameter(param); mtpCommand.SetIPortableDevicePropVariantCollectionValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, mtpCommandParam); } // リクエストを送信 device.SendCommand(0, mtpCommand, out spResults); // コマンドとパラメータは以後不要なので解放 if (mtpCommandParam != null) { Marshal.ReleaseComObject(mtpCommandParam); } Marshal.ReleaseComObject(mtpCommand); // リクエストの結果を取得する spResults.GetErrorValue(ref WpdProperty.WPD_PROPERTY_COMMON_HRESULT, out ret); if (ret != 0) { // エラーなら終了 Marshal.ReleaseComObject(spResults); return(new MtpResponse(0, null, null)); } // コンテキストを取得する spResults.GetStringValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out context); // データサイズを取得する spResults.GetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, out size); Marshal.ReleaseComObject(spResults); if (size > 0) { // データ受信用のコマンドを構築する bufferIn = new byte[size]; mtpCommand = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); mtpCommand.SetGuidValue(ref WpdProperty.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref WpdProperty.WPD_COMMAND_MTP_EXT_READ_DATA.fmtid); mtpCommand.SetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_COMMON_COMMAND_ID, WpdProperty.WPD_COMMAND_MTP_EXT_READ_DATA.pid); mtpCommand.SetStringValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, context); mtpCommand.SetBufferValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref bufferIn[0], size); mtpCommand.SetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_READ, size); // データを受信する device.SendCommand(0, mtpCommand, out spResults); Marshal.ReleaseComObject(mtpCommand); spResults.GetErrorValue(ref WpdProperty.WPD_PROPERTY_COMMON_HRESULT, out ret); if (ret != 0) { // エラーなら終了 Marshal.ReleaseComObject(spResults); return(new MtpResponse(0, null, null)); } // 受信データのアドレスを取得する dataAddress = Environment.Is64BitProcess ? Marshal.AllocCoTaskMem(8) : Marshal.AllocCoTaskMem(4); spResults.GetBufferValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, dataAddress, out readedSize); dataPtr = Environment.Is64BitProcess ? new IntPtr(Marshal.ReadInt64(dataAddress)) : new IntPtr(Marshal.ReadInt32(dataAddress)); Marshal.FreeCoTaskMem(dataAddress); // 受信データを取得する bufferOut = new byte[size]; Marshal.Copy(dataPtr, bufferOut, 0, bufferOut.Length); Marshal.ReleaseComObject(spResults); Marshal.FreeCoTaskMem(dataPtr); } // DataEndTransfer(レスポンス)を送信する sendEndDataTransfer(device, context, out responseCode, out responseParam, noReadResponseParam); return(new MtpResponse((ushort)responseCode, responseParam, bufferOut)); }
public void Add(PropertyKey key, IPortableDevicePropVariantCollection value) { this.values.SetIPortableDevicePropVariantCollectionValue(ref key, value); }
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); }
private MTPDataResponse ExecuteReadData(uint code, IPortableDevicePropVariantCollection propVariant) { MTPDataResponse resp = 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(); 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); 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; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return(resp); } } catch (Exception) { } 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); } pParameters.Clear(); pResults.Clear(); 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; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return(resp); } } catch (Exception) { } // 24,142,174,9 // 18, 8E 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)); resp.Data = new byte[(int)cbBytesRead]; Marshal.Copy(tmpPtr, resp.Data, 0, (int)cbBytesRead); //for (int i = 0; i < cbBytesRead; i++) //{ // resp.Data[i] = Marshal.ReadByte(tmpPtr, i); //} Marshal.FreeHGlobal(tmpPtr); pinnedArray.Free(); 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 pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return(resp); } uint iValue; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out iValue); if (iValue != 0) { resp.ErrorCode = iValue; return(resp); } } catch { } return(resp); }
public virtual extern void GetIPortableDevicePropVariantCollectionValue([In] ref PropertyKey key, [MarshalAs(UnmanagedType.Interface)] out IPortableDevicePropVariantCollection ppValue);
/// <summary> /// データ送信を伴うMTPオペレーションを実行する /// </summary> /// <param name="device"></param> /// <param name="code"></param> /// <param name="param"></param> /// <param name="data"></param> /// <returns></returns> private static MtpResponse executeDataWriteCommand(PortableDevice device, ushort code, uint[] param, byte[] data, bool noReadResponseParam) { int ret = 1; string context; uint writtenSize; uint responseCode; uint[] responseParam; IPortableDeviceValues spResults; // MTPコマンドとパラメータを構築する IPortableDeviceValues mtpCommand = createMtpCommand(code, WpdProperty.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITH_DATA_TO_WRITE); IPortableDevicePropVariantCollection mtpCommandParam = null; if (param != null) { mtpCommandParam = createMtpCommandParameter(param); mtpCommand.SetIPortableDevicePropVariantCollectionValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_OPERATION_PARAMS, mtpCommandParam); } // 送信するデータのサイズを設定する mtpCommand.SetUnsignedLargeIntegerValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_TOTAL_DATA_SIZE, (ulong)data.Length); // リクエストを送信 device.SendCommand(0, mtpCommand, out spResults); // コマンドとパラメータは以後不要なので解放 if (mtpCommandParam != null) { Marshal.ReleaseComObject(mtpCommandParam); } Marshal.ReleaseComObject(mtpCommand); // リクエストの結果を取得する spResults.GetErrorValue(ref WpdProperty.WPD_PROPERTY_COMMON_HRESULT, out ret); if (ret != 0) { // エラーなら終了 Marshal.ReleaseComObject(spResults); return(new MtpResponse(0, null, null)); } // contextを取得する spResults.GetStringValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, out context); Marshal.ReleaseComObject(spResults); // データ送信用のコマンドを構築する mtpCommand = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); mtpCommand.SetGuidValue(ref WpdProperty.WPD_PROPERTY_COMMON_COMMAND_CATEGORY, ref WpdProperty.WPD_COMMAND_MTP_EXT_WRITE_DATA.fmtid); mtpCommand.SetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_COMMON_COMMAND_ID, WpdProperty.WPD_COMMAND_MTP_EXT_WRITE_DATA.pid); mtpCommand.SetStringValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_CONTEXT, context); mtpCommand.SetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_TO_WRITE, (uint)data.Length); mtpCommand.SetBufferValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_DATA, ref data[0], (uint)data.Length); // データを送信する device.SendCommand(0, mtpCommand, out spResults); Marshal.ReleaseComObject(mtpCommand); // データ送信の結果を取得する spResults.GetErrorValue(ref WpdProperty.WPD_PROPERTY_COMMON_HRESULT, out ret); if (ret != 0) { // エラーなら終了 Marshal.ReleaseComObject(spResults); if (mtpCommandParam != null) { Marshal.ReleaseComObject(mtpCommandParam); } return(new MtpResponse(0, null, null)); } // デバイスが受け取ったデータサイズを取得する spResults.GetUnsignedIntegerValue(ref WpdProperty.WPD_PROPERTY_MTP_EXT_TRANSFER_NUM_BYTES_WRITTEN, out writtenSize); Marshal.ReleaseComObject(spResults); // DataEndTransfer(レスポンス)を送信する sendEndDataTransfer(device, context, out responseCode, out responseParam, noReadResponseParam); return(new MtpResponse((ushort)responseCode, responseParam, null)); }
private MTPDataResponse ExecuteReadData(uint code, IPortableDevicePropVariantCollection propVariant) { MTPDataResponse resp = 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(); 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); 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; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return resp; } } catch (Exception) { } 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); } pParameters.Clear(); pResults.Clear(); 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; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return resp; } } catch (Exception) { } // 24,142,174,9 // 18, 8E 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)); resp.Data = new byte[(int)cbBytesRead]; Marshal.Copy(tmpPtr, resp.Data, 0, (int)cbBytesRead); //for (int i = 0; i < cbBytesRead; i++) //{ // resp.Data[i] = Marshal.ReadByte(tmpPtr, i); //} Marshal.FreeHGlobal(tmpPtr); pinnedArray.Free(); 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 pValue; pResults.GetErrorValue(PortableDevicePKeys.WPD_PROPERTY_COMMON_HRESULT, out pValue); if (pValue != 0) { resp.ErrorCode = (uint)pValue; return resp; } uint iValue; pResults.GetUnsignedIntegerValue(ref PortableDevicePKeys.WPD_PROPERTY_MTP_EXT_RESPONSE_CODE, out iValue); if (iValue != 0) { resp.ErrorCode = iValue; return resp; } } catch { } return resp; }
public extern virtual void SetIPortableDevicePropVariantCollectionValue([In] ref PropertyKey key, [In, MarshalAs(UnmanagedType.Interface)] IPortableDevicePropVariantCollection pValue);