public void Refresh() { if (this.Count > 0) { this.Clear(); } IPortableDeviceProperties properties = null; this.content.Properties(out properties); IPortableDeviceValues values = null; properties.GetValues(this.objectID, null, out values); var count = 0U; values.GetCount(ref count); var key = new _tagpropertykey(); var propVar = new tag_inner_PROPVARIANT(); for (var i = 0U; i < count; i++) { values.GetAt(i, ref key, ref propVar); this.Add(WpdProperty.Create(key, propVar)); } }
// private static string GetObjectName(IPortableDeviceContent deviceContent, string objectId) // { // IPortableDeviceProperties deviceProperties; // deviceContent.Properties(out deviceProperties); // // var keyCollection = (IPortableDeviceKeyCollection)new PortableDeviceTypesLib.PortableDeviceKeyCollectionClass(); // keyCollection.Add(ref PortableDevicePropertyKeys.WPD_OBJECT_NAME); // // IPortableDeviceValues deviceValues; // deviceProperties.GetValues(objectId, keyCollection, out deviceValues); // // string name; // deviceValues.GetStringValue(ref PortableDevicePropertyKeys.WPD_OBJECT_NAME, out name); // // return name; // } private static IEnumerable <string> GetObjectProperties(IPortableDeviceContent deviceContent, string objectId) { IPortableDeviceProperties deviceProperties; deviceContent.Properties(out deviceProperties); IPortableDeviceValues deviceValues; deviceProperties.GetValues(objectId, null, out deviceValues); var properties = new List <string>(); uint valueCount = 0; deviceValues.GetCount(ref valueCount); for (uint i = 0; i < valueCount; i++) { var key = new _tagpropertykey(); var value = new tag_inner_PROPVARIANT(); deviceValues.GetAt(i, ref key, ref value); properties.Add( String.Format("[{0}, {1}] : {2}", key.fmtid, key.pid, PropVariant.FromValue(value).AsString())); } return(properties); }
/// <summary> /// データ転送が無いオペレーションを実行する /// </summary> /// <param name="device"></param> /// <param name="code"></param> /// <param name="param"></param> /// <returns></returns> private static MtpResponse executeNoDataCommand(PortableDevice device, ushort code, uint[] param) { IPortableDeviceValues spResults; int ret; uint responseCode; uint[] responseParam; // MTPコマンドとパラメータを構築する IPortableDeviceValues mtpCommand = createMtpCommand(code, WpdProperty.WPD_COMMAND_MTP_EXT_EXECUTE_COMMAND_WITHOUT_DATA_PHASE); 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.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); return(new MtpResponse((ushort)responseCode, responseParam, null)); }
public static PropVariant FromValue(tag_inner_PROPVARIANT value) { IntPtr ptrValue = Marshal.AllocHGlobal(Marshal.SizeOf(value)); Marshal.StructureToPtr(value, ptrValue, false); // // Marshal the pointer into our C# object // return (PropVariant)Marshal.PtrToStructure(ptrValue, typeof(PropVariant)); }
/// <summary> /// インスタンスを初期化します。 /// </summary> /// <param name="key">識別子。</param> /// <param name="info">プロパティ値の情報。</param> /// <param name="values">値。</param> internal WpdPropertyValue( _tagpropertykey key, tag_inner_PROPVARIANT info, IPortableDeviceValues values ) { this.Key = key; this.HasValue = true; switch( ( PropVariantTypes )info.vt ) { case PropVariantTypes.VT_BOOL: this.Type = WpdPropertyValueType.Bool; this.ValueBool = WpdPropertyValue.ReadBool( key, values ); break; case PropVariantTypes.VT_DATE: this.Type = WpdPropertyValueType.DateTime; this.ValueDateTime = WpdPropertyValue.ReadDateTime( key, values ); break; case PropVariantTypes.VT_CLSID: this.Type = WpdPropertyValueType.Guid; this.ValueGuid = WpdPropertyValue.ReadGuid( key, values ); break; case PropVariantTypes.VT_I4: this.Type = WpdPropertyValueType.Int32; this.ValueInt32 = WpdPropertyValue.ReadInt32( key, values ); break; case PropVariantTypes.VT_I8: this.Type = WpdPropertyValueType.Int64; this.ValueInt64 = WpdPropertyValue.ReadInt64( key, values ); break; case PropVariantTypes.VT_BSTR: case PropVariantTypes.VT_LPSTR: case PropVariantTypes.VT_LPWSTR: this.Type = WpdPropertyValueType.String; this.ValueString = WpdPropertyValue.ReadString( key, values ); break; case PropVariantTypes.VT_UI4: this.Type = WpdPropertyValueType.UInt32; this.ValueUInt32 = WpdPropertyValue.ReadUInt32( key, values ); break; case PropVariantTypes.VT_UI8: this.Type = WpdPropertyValueType.UInt64; this.ValueUInt64 = WpdPropertyValue.ReadUInt64( key, values ); break; default: this.Type = WpdPropertyValueType.Unknown; this.HasValue = false; break; } }
/// <summary> /// インスタンスを初期化します。 /// </summary> /// <param name="key">識別子。</param> /// <param name="info">プロパティ値の情報。</param> /// <param name="values">値。</param> internal WpdPropertyValue(_tagpropertykey key, tag_inner_PROPVARIANT info, IPortableDeviceValues values) { this.Key = key; this.HasValue = true; switch (( PropVariantTypes )info.vt) { case PropVariantTypes.VT_BOOL: this.Type = WpdPropertyValueType.Bool; this.ValueBool = WpdPropertyValue.ReadBool(key, values); break; case PropVariantTypes.VT_DATE: this.Type = WpdPropertyValueType.DateTime; this.ValueDateTime = WpdPropertyValue.ReadDateTime(key, values); break; case PropVariantTypes.VT_CLSID: this.Type = WpdPropertyValueType.Guid; this.ValueGuid = WpdPropertyValue.ReadGuid(key, values); break; case PropVariantTypes.VT_I4: this.Type = WpdPropertyValueType.Int32; this.ValueInt32 = WpdPropertyValue.ReadInt32(key, values); break; case PropVariantTypes.VT_I8: this.Type = WpdPropertyValueType.Int64; this.ValueInt64 = WpdPropertyValue.ReadInt64(key, values); break; case PropVariantTypes.VT_BSTR: case PropVariantTypes.VT_LPSTR: case PropVariantTypes.VT_LPWSTR: this.Type = WpdPropertyValueType.String; this.ValueString = WpdPropertyValue.ReadString(key, values); break; case PropVariantTypes.VT_UI4: this.Type = WpdPropertyValueType.UInt32; this.ValueUInt32 = WpdPropertyValue.ReadUInt32(key, values); break; case PropVariantTypes.VT_UI8: this.Type = WpdPropertyValueType.UInt64; this.ValueUInt64 = WpdPropertyValue.ReadUInt64(key, values); break; default: this.Type = WpdPropertyValueType.Unknown; this.HasValue = false; break; } }
public static PropVariant FromValue(tag_inner_PROPVARIANT value) { IntPtr ptrValue = Marshal.AllocHGlobal(Marshal.SizeOf(value)); Marshal.StructureToPtr(value, ptrValue, false); // // Marshal the pointer into our C# object // return((PropVariant)Marshal.PtrToStructure(ptrValue, typeof(PropVariant))); }
/// <summary> /// tag_inner_PROPVARIANT型からuintの値を取得する /// </summary> /// <param name="value"></param> /// <returns></returns> private static uint getUintValue(tag_inner_PROPVARIANT value) { uint ret = 1; IPortableDeviceValues pdValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); pdValues.SetValue(ref WpdProperty.WPD_OBJECT_ID, ref value); pdValues.GetUnsignedIntegerValue(ref WpdProperty.WPD_OBJECT_ID, out ret); Marshal.ReleaseComObject(pdValues); return(ret); }
/// <summary> /// uint型のtag_inner_PROPVARIANTを生成する /// </summary> /// <param name="value"></param> /// <returns></returns> private static tag_inner_PROPVARIANT createPropVariant(uint value) { tag_inner_PROPVARIANT propVariant = new tag_inner_PROPVARIANT(); IPortableDeviceValues pdValues = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); pdValues.SetUnsignedIntegerValue(ref WpdProperty.WPD_OBJECT_ID, value); pdValues.GetValue(ref WpdProperty.WPD_OBJECT_ID, out propVariant); Marshal.ReleaseComObject(pdValues); return(propVariant); }
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)); }
/// <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); }
/// <summary> /// Extract device capabilities /// </summary> /// <param name="portableDeviceClass"></param> internal void ExtractDeviceCapabilities(PortableDeviceClass portableDeviceClass) { if (portableDeviceClass == null) { throw new ArgumentNullException("portableDeviceClass"); } try { PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); if (capabilities == null) { Trace.WriteLine("Cannot extract capabilities from device"); throw new PortableDeviceException("Cannot extract capabilities from device"); } IPortableDevicePropVariantCollection functionalCategories; capabilities.GetFunctionalCategories(out functionalCategories); if (functionalCategories == null) { throw new PortableDeviceException("Failed to extract functionnal categories"); } uint countCategories = 1; functionalCategories.GetCount(ref countCategories); tag_inner_PROPVARIANT values = new tag_inner_PROPVARIANT(); PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); string categoryName; Guid currentGuid; for (uint i = 0; i < countCategories; i++) { functionalCategories.GetAt(i, ref values); pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FUNCTIONAL_CATEGORIES, ref values); pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FUNCTIONAL_CATEGORIES, out categoryName); currentGuid = new Guid(categoryName); this.functionalCategories.Add(currentGuid, new FunctionalCategory( portableDeviceClass, currentGuid, PortableDeviceHelpers.GetKeyNameFromGuid(currentGuid))); } } catch (Exception ex) { throw new PortableDeviceException("Error on extract device capabilities", ex); } }
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); }
/// <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); }
private void ExtractContentType(PortableDeviceClass portableDeviceClass, Guid functionalCategory) { if (portableDeviceClass == null) { throw new ArgumentNullException("portableDeviceClass"); } try { PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); if (capabilities == null) { System.Diagnostics.Trace.WriteLine("Cannot extract capabilities from device"); throw new PortableDeviceException("Cannot extract capabilities from device"); } PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); //Functional objects variables IPortableDevicePropVariantCollection contentTypes; uint countObjects = 1; tag_inner_PROPVARIANT values = new tag_inner_PROPVARIANT(); string contentTypeName; Guid currentContentTypeGuid; capabilities.GetSupportedContentTypes(ref functionalCategory, out contentTypes); contentTypes.GetCount(ref countObjects); for (uint i = 0; i < countObjects; i++) { contentTypes.GetAt(i, ref values); pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_CONTENT_TYPES, ref values); pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_CONTENT_TYPES, out contentTypeName); currentContentTypeGuid = new Guid(contentTypeName); this.contentTypes.Add(currentContentTypeGuid, new ContentType( portableDeviceClass, currentContentTypeGuid, PortableDeviceHelpers.GetKeyNameFromGuid(currentContentTypeGuid))); } } catch (Exception ex) { throw new PortableDeviceException("Error on extract functional object", ex); } }
private void ObjectIDToPropVariant(string objectID, out tag_inner_PROPVARIANT tag) { var values = (IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); values.SetStringValue(ref GlobalVar.WPD_OBJECT_ID, objectID); try { values.GetValue(ref GlobalVar.WPD_OBJECT_ID, out tag); } finally { Marshal.ReleaseComObject(values); } }
/// <summary> /// uint型のtag_inner_PROPVARIANTを生成する /// </summary> /// <param name="value"></param> /// <returns></returns> private static tag_inner_PROPVARIANT createPropVariant(uint value) { PropVariant pvValue = new PropVariant(); pvValue.variantType = 19; // UInt32 pvValue.longValue = (long)value; IntPtr ptrValue = Marshal.AllocHGlobal(Marshal.SizeOf(pvValue)); Marshal.StructureToPtr(pvValue, ptrValue, false); tag_inner_PROPVARIANT ipValue = (tag_inner_PROPVARIANT)Marshal.PtrToStructure(ptrValue, typeof(tag_inner_PROPVARIANT)); Marshal.FreeHGlobal(ptrValue); return(ipValue); }
private IAudioEndpointVolume GetAudioEndpointVolume() { MMDeviceEnumeratorClass devEnum = new MMDeviceEnumeratorClass(); IMMDevice endPoint; devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, out endPoint); Marshal.ReleaseComObject(devEnum); Guid IID_IAudioEndpointVolume = new Guid(IID.IAudioEndpointVolume); tag_inner_PROPVARIANT prop = new tag_inner_PROPVARIANT(); IntPtr ppInterface; endPoint.Activate(ref IID_IAudioEndpointVolume, CLSCTX_ALL, ref prop, out ppInterface); IAudioEndpointVolume endPointVol = Marshal.GetObjectForIUnknown(ppInterface) as IAudioEndpointVolume; return(endPointVol); }
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; }
/// <summary> /// インスタンスを初期化します。 /// </summary> /// <param name="values">イベント データのコレクション。</param> internal WpdEventArgs(IPortableDeviceValues values) { uint count = 0; values.GetCount(ref count); if (count < 1) { this.Values = new List <WpdPropertyValue>(0); return; } this.Values = new List <WpdPropertyValue>(( int )count); var key = new _tagpropertykey(); var info = new tag_inner_PROPVARIANT(); for (uint i = 0; i < count; ++i) { values.GetAt(i, ref key, ref info); var value = new WpdPropertyValue(key, info, values); if (value.Key.Equals(WpdProperties.WPD_EVENT_PARAMETER_PNP_DEVICE_ID)) { this.DeviceId = value.ValueString; } else if (value.Key.Equals(WpdProperties.WPD_OBJECT_ID)) { this.ObjectId = value.ValueString; } else if (value.Key.Equals(WpdProperties.WPD_OBJECT_PARENT_ID)) { this.ParentObjectId = value.ValueString; } else if (value.Key.Equals(WpdProperties.WPD_EVENT_PARAMETER_EVENT_ID)) { if (value.ValueGuid != null) { this.Type = TypeConvertUtility.GuidToEventType(value.ValueGuid.Value); } } else { this.Values.Add(value); } } }
/// <summary> /// インスタンスを初期化します。 /// </summary> /// <param name="values">イベント データのコレクション。</param> internal WpdEventArgs( IPortableDeviceValues values ) { uint count = 0; values.GetCount( ref count ); if( count < 1 ) { this.Values = new List< WpdPropertyValue >( 0 ); return; } this.Values = new List< WpdPropertyValue >( ( int )count ); var key = new _tagpropertykey(); var info = new tag_inner_PROPVARIANT(); for( uint i = 0; i < count; ++i ) { values.GetAt( i, ref key, ref info ); var value = new WpdPropertyValue( key, info, values ); if( value.Key.Equals( WpdProperties.WPD_EVENT_PARAMETER_PNP_DEVICE_ID ) ) { this.DeviceId = value.ValueString; } else if( value.Key.Equals( WpdProperties.WPD_OBJECT_ID ) ) { this.ObjectId = value.ValueString; } else if( value.Key.Equals( WpdProperties.WPD_OBJECT_PARENT_ID ) ) { this.ParentObjectId = value.ValueString; } else if( value.Key.Equals( WpdProperties.WPD_EVENT_PARAMETER_EVENT_ID ) ) { if( value.ValueGuid != null ) { this.Type = TypeConvertUtility.GuidToEventType( value.ValueGuid.Value ); } } else { this.Values.Add( value ); } } }
private void ExtractSupportedFormat(PortableDeviceClass portableDeviceClass, Guid contentType) { if (portableDeviceClass == null) { throw new PortableDeviceException(""); } PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); if (capabilities == null) { System.Diagnostics.Trace.WriteLine("Cannot extract capabilities from device"); throw new PortableDeviceException("Cannot extract capabilities from device"); } PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); //Functional objects variables IPortableDevicePropVariantCollection formats; uint countObjects = 1; tag_inner_PROPVARIANT values = new tag_inner_PROPVARIANT(); string formatName; Guid currentFormat; capabilities.GetSupportedFormats(ref contentType, out formats); formats.GetCount(ref countObjects); for (uint i = 0; i < countObjects; i++) { formats.GetAt(i, ref values); pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FORMATS, ref values); pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FORMATS, out formatName); currentFormat = new Guid(formatName); this.formats.Add(currentFormat, PortableDeviceHelpers.GetKeyNameFromGuid(currentFormat)); } }
/// <summary> /// オブジェクトのプロパティ値を列挙します。 /// </summary> /// <param name="id">オブジェクトの識別子。</param> /// <param name="properties">プロパティ情報。</param> /// <returns>プロパティ値のコレクション。</returns> internal static IEnumerable <WpdPropertyValue> EnumValues(string id, IPortableDeviceProperties properties) { IPortableDeviceKeyCollection keys; properties.GetSupportedProperties(id, out keys); IPortableDeviceValues values; properties.GetValues(id, keys, out values); uint count = 0; values.GetCount(ref count); var key = new _tagpropertykey(); var info = new tag_inner_PROPVARIANT(); for (uint i = 0; i < count; ++i) { values.GetAt(i, ref key, ref info); yield return(new WpdPropertyValue(key, info, values)); } }
private void ExtractContentType(PortableDeviceClass portableDeviceClass, Guid functionalCategory) { if (portableDeviceClass == null) throw new ArgumentNullException("portableDeviceClass"); try { PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); if (capabilities == null) { System.Diagnostics.Trace.WriteLine("Cannot extract capabilities from device"); throw new PortableDeviceException("Cannot extract capabilities from device"); } PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); //Functional objects variables IPortableDevicePropVariantCollection contentTypes; uint countObjects = 1; tag_inner_PROPVARIANT values = new tag_inner_PROPVARIANT(); string contentTypeName; Guid currentContentTypeGuid; capabilities.GetSupportedContentTypes(ref functionalCategory, out contentTypes); contentTypes.GetCount(ref countObjects); for (uint i = 0; i < countObjects; i++) { contentTypes.GetAt(i, ref values); pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_CONTENT_TYPES, ref values); pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_CONTENT_TYPES, out contentTypeName); currentContentTypeGuid = new Guid(contentTypeName); this.contentTypes.Add(currentContentTypeGuid, new ContentType( portableDeviceClass, currentContentTypeGuid, PortableDeviceHelpers.GetKeyNameFromGuid(currentContentTypeGuid))); } } catch (Exception ex) { throw new PortableDeviceException("Error on extract functional object", ex); } }
public Hashtable GetDeviceProperties(string deviceId, string objectId) { Hashtable properties = new Hashtable(); PortableDeviceClass deviceClass = new PortableDeviceClass(); IPortableDeviceContent deviceContent; deviceClass.Open(deviceId, _hClientDeviceValues); deviceClass.Content(out deviceContent); IPortableDeviceProperties deviceProperties; deviceContent.Properties(out deviceProperties); IPortableDeviceValues deviceValues; deviceProperties.GetValues(objectId, null, out deviceValues); uint devicePropertyCount = 0; deviceValues.GetCount(ref devicePropertyCount); for (uint i = 0; i < devicePropertyCount; i++) { _tagpropertykey tagPropertyKey = new _tagpropertykey(); tag_inner_PROPVARIANT tagPropVariant = new tag_inner_PROPVARIANT(); deviceValues.GetAt(i, ref tagPropertyKey, ref tagPropVariant); IntPtr ptrValue = Marshal.AllocHGlobal(Marshal.SizeOf(tagPropVariant)); Marshal.StructureToPtr(tagPropVariant, ptrValue, false); PropVariant pvValue = (PropVariant)Marshal.PtrToStructure(ptrValue, typeof(PropVariant)); if (pvValue.variantType == VariantTypes.VT_LPWSTR) { string stringValue = Marshal.PtrToStringUni(pvValue.pointerValue); properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), stringValue); } else if (pvValue.variantType == VariantTypes.VT_DATE) { DateTime datetime = DateTime.FromOADate(pvValue.dateValue); properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), datetime); } else if (pvValue.variantType == VariantTypes.VT_UI4) { uint intValue = pvValue.byteValue; properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), intValue); } else if (pvValue.variantType == VariantTypes.VT_UI8) { long intValue = pvValue.longValue; properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), intValue); } else if (pvValue.variantType == VariantTypes.VT_UINT) { long intValue = pvValue.longValue; properties.Add(PropertyManager.GetKeyName(tagPropertyKey.pid, tagPropertyKey.fmtid), intValue); } } return(properties); }
/// <summary> /// Extract device capabilities /// </summary> /// <param name="portableDeviceClass"></param> internal void ExtractDeviceCapabilities(PortableDeviceClass portableDeviceClass) { if (portableDeviceClass == null) throw new ArgumentNullException("portableDeviceClass"); try { PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); if (capabilities == null) { Trace.WriteLine("Cannot extract capabilities from device"); throw new PortableDeviceException("Cannot extract capabilities from device"); } IPortableDevicePropVariantCollection functionalCategories; capabilities.GetFunctionalCategories(out functionalCategories); if (functionalCategories == null) { throw new PortableDeviceException("Failed to extract functionnal categories"); } uint countCategories = 1; functionalCategories.GetCount(ref countCategories); tag_inner_PROPVARIANT values = new tag_inner_PROPVARIANT(); PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); string categoryName; Guid currentGuid; for (uint i = 0; i < countCategories; i++) { functionalCategories.GetAt(i, ref values); pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FUNCTIONAL_CATEGORIES, ref values); pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FUNCTIONAL_CATEGORIES, out categoryName); currentGuid = new Guid(categoryName); this.functionalCategories.Add(currentGuid, new FunctionalCategory( portableDeviceClass, currentGuid, PortableDeviceHelpers.GetKeyNameFromGuid(currentGuid))); } } catch (Exception ex) { throw new PortableDeviceException("Error on extract device capabilities", ex); } }
/// <summary> /// Extract event supported by device /// </summary> /// <param name="portableDeviceClass"></param> internal void ExtractEvents(PortableDeviceClass portableDeviceClass) { PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); PortableDeviceApiLib.IPortableDevicePropVariantCollection events; capabilities.GetSupportedEvents(out events); uint countEvents = 0; events.GetCount(ref countEvents); PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); tag_inner_PROPVARIANT evt = new tag_inner_PROPVARIANT(); Guid eventName; IPortableDeviceValues eventOptions; PortableDeviceEventDescription eventDescription; for (uint i = 0; i < countEvents; i++) { events.GetAt(i, ref evt); pValues.SetValue(ref PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID, ref evt); pValues.GetGuidValue(ref PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID, out eventName); eventDescription = new PortableDeviceEventDescription(eventName, PortableDeviceHelpers.GetKeyNameFromGuid(eventName)); //Retrieve options try { // Event option isn't always present, so ... eventOptions = (PortableDeviceApiLib.IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); capabilities.GetEventOptions(ref eventName, out eventOptions); //eventOptions.GetBoolValue(ref PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT, out isAutoPlayEvent); //eventOptions.GetBoolValue(ref PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT, out isBroadcastEvent); //eventDescription.AddOptions(new PortableDeviceEventOption() //{ // Guid = PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT.fmtid, // Name = PortableDeviceHelpers.GetKeyNameFromGuid(PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT.fmtid), // Value = isBroadcastEvent, // ValueType = TypeCode.Boolean //}); //eventDescription.AddOptions(new PortableDeviceEventOption() //{ // Guid = PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT.fmtid, // Name = PortableDeviceHelpers.GetKeyNameFromGuid(PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT.fmtid), // Value = isAutoPlayEvent, // ValueType = TypeCode.Boolean //}); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } this.events.Add(eventDescription, PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID); } }
private IAudioEndpointVolume GetAudioEndpointVolume() { MMDeviceEnumeratorClass devEnum = new MMDeviceEnumeratorClass(); IMMDevice endPoint; devEnum.GetDefaultAudioEndpoint(EDataFlow.eRender, ERole.eConsole, out endPoint); Marshal.ReleaseComObject(devEnum); Guid IID_IAudioEndpointVolume = new Guid(IID.IAudioEndpointVolume); tag_inner_PROPVARIANT prop = new tag_inner_PROPVARIANT(); IntPtr ppInterface; endPoint.Activate(ref IID_IAudioEndpointVolume, CLSCTX_ALL, ref prop, out ppInterface); IAudioEndpointVolume endPointVol = Marshal.GetObjectForIUnknown(ppInterface) as IAudioEndpointVolume; return endPointVol; }
public byte[] ExecuteReadBigDataWriteToFile(int code, int param1, int param2, TransferCallback callback, string fileName) { // 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); tag_inner_PROPVARIANT vparam1 = new tag_inner_PROPVARIANT(); tag_inner_PROPVARIANT vparam2 = new tag_inner_PROPVARIANT(); if (param1 > -1) { UintToPropVariant((uint)param1, out vparam1); propVariant.Add(ref vparam1); } if (param2 > -1) { UintToPropVariant((uint)param2, out vparam2); propVariant.Add(ref vparam2); } 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 ExecuteReadBigDataWriteToFile(code, param1, param2, callback, fileName); } } } 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; byte[] res = new byte[(int)tmpBufferSize]; bool cont = true; var hFile = WinApi.CreateFile(fileName, (uint)WinApi.DesiredAccess.GENERIC_WRITE, (uint)WinApi.ShareMode.FILE_SHARE_WRITE, IntPtr.Zero, (uint)WinApi.CreationDisposition.CREATE_ALWAYS, (uint)WinApi.FlagsAndAttributes.FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); 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) return null; } 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)); var natOverlap = new NativeOverlapped() { OffsetLow = (int)offset }; IntPtr written = new IntPtr(); WinApi.WriteFile(hFile, tmpPtr, (int)cbBytesRead, written, ref natOverlap); //for (int i = 0; i < cbBytesRead; i++) //{ // res[offset + i] = Marshal.ReadByte(tmpPtr, i); //} Marshal.FreeHGlobal(tmpPtr); pinnedArray.Free(); offset += cbBytesRead; } while (cont); WinApi.CloseHandle(hFile); 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 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); }
public MTPDataResponse ExecuteReadDataEx(int code, uint param1, uint param2, uint param3) { IPortableDevicePropVariantCollection propVariant = (IPortableDevicePropVariantCollection)new PortableDeviceTypesLib.PortableDevicePropVariantCollection(); tag_inner_PROPVARIANT vparam1 = new tag_inner_PROPVARIANT(); tag_inner_PROPVARIANT vparam2 = new tag_inner_PROPVARIANT(); tag_inner_PROPVARIANT vparam3 = new tag_inner_PROPVARIANT(); UintToPropVariant(param1, out vparam1); propVariant.Add(ref vparam1); UintToPropVariant(param2, out vparam2); propVariant.Add(ref vparam2); UintToPropVariant(param3, out vparam3); propVariant.Add(ref vparam3); return ExecuteReadData(code, propVariant); }
/// <summary> /// オブジェクトのプロパティ値を列挙します。 /// </summary> /// <param name="id">オブジェクトの識別子。</param> /// <param name="properties">プロパティ情報。</param> /// <returns>プロパティ値のコレクション。</returns> internal static IEnumerable<WpdPropertyValue> EnumValues( string id, IPortableDeviceProperties properties ) { IPortableDeviceKeyCollection keys; properties.GetSupportedProperties( id, out keys ); IPortableDeviceValues values; properties.GetValues( id, keys, out values ); uint count = 0; values.GetCount( ref count ); var key = new _tagpropertykey(); var info = new tag_inner_PROPVARIANT(); for( uint i = 0; i < count; ++i ) { values.GetAt( i, ref key, ref info ); yield return new WpdPropertyValue( key, info, values ); } }
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; }
public static IWpdProperty Create(_tagpropertykey key, tag_inner_PROPVARIANT variant) { var ptrValue = Marshal.AllocHGlobal(Marshal.SizeOf(variant)); try { Marshal.StructureToPtr(variant, ptrValue, false); var pv = WpdProperty.MarshalToStructure <PROPVARIANT>(ptrValue); var ve = (VarEnum)pv.vt; switch (ve) { case VarEnum.VT_I1: return(new WpdProperty <sbyte>(key, variant, ve, pv.AsSByte())); case VarEnum.VT_UI1: return(new WpdProperty <byte>(key, variant, ve, pv.AsByte())); case VarEnum.VT_I2: return(new WpdProperty <short>(key, variant, ve, pv.AsInt16())); case VarEnum.VT_UI2: return(new WpdProperty <ushort>(key, variant, ve, pv.AsUInt16())); case VarEnum.VT_I4: case VarEnum.VT_INT: case VarEnum.VT_ERROR: return(new WpdProperty <int>(key, variant, ve, pv.AsInt32())); case VarEnum.VT_UI4: case VarEnum.VT_UINT: return(new WpdProperty <uint>(key, variant, ve, pv.AsUInt32())); case VarEnum.VT_I8: return(new WpdProperty <long>(key, variant, ve, pv.AsInt64())); case VarEnum.VT_UI8: return(new WpdProperty <ulong>(key, variant, ve, pv.AsUInt64())); case VarEnum.VT_R4: return(new WpdProperty <float>(key, variant, ve, pv.AsFloat())); case VarEnum.VT_R8: return(new WpdProperty <double>(key, variant, ve, pv.AsDouble())); case VarEnum.VT_BOOL: return(new WpdProperty <bool>(key, variant, ve, pv.AsBool())); case VarEnum.VT_CY: return(new WpdProperty <decimal>(key, variant, ve, pv.AsDecimal())); case VarEnum.VT_DATE: case VarEnum.VT_FILETIME: return(new WpdProperty <DateTime>(key, variant, ve, pv.AsDateTime())); case VarEnum.VT_BSTR: case VarEnum.VT_LPSTR: case VarEnum.VT_LPWSTR: return(new WpdProperty <string>(key, variant, ve, pv.AsString())); case VarEnum.VT_BLOB: return(new WpdProperty <byte[]>(key, variant, ve, pv.AsByteBuffer())); case VarEnum.VT_UNKNOWN: return(new WpdProperty <object>(key, variant, ve, pv.AsIUnknown())); case VarEnum.VT_DISPATCH: case VarEnum.VT_PTR: return(new WpdProperty <IntPtr>(key, variant, ve, pv.AsIntPtr())); case VarEnum.VT_CLSID: return(new WpdProperty <Guid>(key, variant, ve, pv.AsGuid())); case VarEnum.VT_EMPTY: return(new WpdProperty <object>(key, variant, ve, null)); default: return(new WpdProperty <object>(key, variant, ve, pv.AsIntPtr())); } } finally { Marshal.FreeHGlobal(ptrValue); } }
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); }
private void ExtractSupportedFormat(PortableDeviceClass portableDeviceClass, Guid contentType) { if (portableDeviceClass == null) throw new PortableDeviceException(""); PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); if (capabilities == null) { System.Diagnostics.Trace.WriteLine("Cannot extract capabilities from device"); throw new PortableDeviceException("Cannot extract capabilities from device"); } PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues)new PortableDeviceTypesLib.PortableDeviceValuesClass(); //Functional objects variables IPortableDevicePropVariantCollection formats; uint countObjects = 1; tag_inner_PROPVARIANT values = new tag_inner_PROPVARIANT(); string formatName; Guid currentFormat; capabilities.GetSupportedFormats(ref contentType, out formats); formats.GetCount(ref countObjects); for (uint i = 0; i < countObjects; i++) { formats.GetAt(i, ref values); pValues.SetValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FORMATS, ref values); pValues.GetStringValue(ref PortableDevicePKeys.WPD_COMMAND_CAPABILITIES_GET_SUPPORTED_FORMATS, out formatName); currentFormat = new Guid(formatName); this.formats.Add(currentFormat, PortableDeviceHelpers.GetKeyNameFromGuid(currentFormat)); } }
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; }
/// <summary> /// Extract event supported by device /// </summary> /// <param name="portableDeviceClass"></param> internal void ExtractEvents(PortableDeviceClass portableDeviceClass) { PortableDeviceApiLib.IPortableDeviceCapabilities capabilities; portableDeviceClass.Capabilities(out capabilities); PortableDeviceApiLib.IPortableDevicePropVariantCollection events; capabilities.GetSupportedEvents(out events); uint countEvents = 0; events.GetCount(ref countEvents); PortableDeviceApiLib.IPortableDeviceValues pValues = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); tag_inner_PROPVARIANT evt = new tag_inner_PROPVARIANT(); Guid eventName; IPortableDeviceValues eventOptions; PortableDeviceEventDescription eventDescription; for (uint i = 0; i < countEvents; i++) { events.GetAt(i, ref evt); pValues.SetValue(ref PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID, ref evt); pValues.GetGuidValue(ref PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID, out eventName); eventDescription = new PortableDeviceEventDescription(eventName, PortableDeviceHelpers.GetKeyNameFromGuid(eventName)); //Retrieve options try { // Event option isn't always present, so ... eventOptions = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValuesClass(); capabilities.GetEventOptions(ref eventName, out eventOptions); //eventOptions.GetBoolValue(ref PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT, out isAutoPlayEvent); //eventOptions.GetBoolValue(ref PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT, out isBroadcastEvent); //eventDescription.AddOptions(new PortableDeviceEventOption() //{ // Guid = PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT.fmtid, // Name = PortableDeviceHelpers.GetKeyNameFromGuid(PortableDevicePKeys.WPD_EVENT_OPTION_IS_BROADCAST_EVENT.fmtid), // Value = isBroadcastEvent, // ValueType = TypeCode.Boolean //}); //eventDescription.AddOptions(new PortableDeviceEventOption() //{ // Guid = PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT.fmtid, // Name = PortableDeviceHelpers.GetKeyNameFromGuid(PortableDevicePKeys.WPD_EVENT_OPTION_IS_AUTOPLAY_EVENT.fmtid), // Value = isAutoPlayEvent, // ValueType = TypeCode.Boolean //}); } catch (Exception ex) { Debug.WriteLine(ex.Message); Debug.WriteLine(ex.StackTrace); } this.events.Add(eventDescription, PortableDevicePKeys.WPD_EVENT_PARAMETER_EVENT_ID); } }
public void DeleteFile(NwdPortableDeviceFile file) { Connect(); IPortableDeviceContent content; this._device.Content(out content); var variant = new tag_inner_PROPVARIANT(); StringToPropVariant(file.Id, out variant); IPortableDevicePropVariantCollection objectIds = new PortableDeviceTypesLib.PortableDevicePropVariantCollection() as IPortableDevicePropVariantCollection; objectIds.Add(variant); content.Delete(0, objectIds, null); Disconnect(); }