public static void wpdEnum(string strDeviceId) { PortableDeviceApiLib.IPortableDeviceValues clientInfo = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); { clientInfo.SetStringValue(ref WPD_CLIENT_NAME, szClientName); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_MAJOR_VERSION, dwClientVersionMajor); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_MINOR_VERSION, dwClientVersionMinor); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_REVISION, dwClientRevision); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_SECURITY_QUALITY_OF_SERVICE, SECURITY_IMPERSONATION); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_DESIRED_ACCESS, GENERIC_READ); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_SHARE_MODE, FILE_SHARE_READ | FILE_SHARE_WRITE); } { //Console.WriteLine(strDeviceId); PortableDeviceApiLib.PortableDevice dev = new PortableDeviceApiLib.PortableDevice(); dev.Open(strDeviceId, clientInfo); PortableDeviceApiLib.IPortableDeviceContent content = null; dev.Content(out content); if (null != content) { for (uint count = 0; count < 30; ++count) { Console.WriteLine("enumrate start"); countContent = 0; bool result = wpdEnumContent(WPD_DEVICE_OBJECT_ID, content); Console.WriteLine("enumrate end: count count={0}", countContent); System.Threading.Thread.Sleep(1 * 1000); if (false == result) { break; } } content = null; } dev.Close(); dev = null; } }
static bool wpdEnumContent(string strObjectId, PortableDeviceApiLib.IPortableDeviceContent content) { if (null == strObjectId) { return(false); } if (null == content) { return(false); } //Console.WriteLine(strObjectId); bool result = true; PortableDeviceApiLib.IEnumPortableDeviceObjectIDs enumObj = null; const uint dwFlags = 0; const PortableDeviceApiLib.IPortableDeviceValues filter = null; try { content.EnumObjects(dwFlags, strObjectId, filter, out enumObj); } catch (System.Exception e) { if (e is System.Runtime.InteropServices.COMException) { System.Runtime.InteropServices.COMException ex = (System.Runtime.InteropServices.COMException)e; Console.WriteLine("EnumObjects, hr={0:X}", (uint)ex.ErrorCode); } else { Console.WriteLine(e.Message); } //Console.WriteLine(e.StackTrace); return(false); } if (null == enumObj) { return(false); } string[] strObjectIdArray = new string[MY_FETCH_COUNT]; uint nFetched = 0; do { try { enumObj.Next(MY_FETCH_COUNT, strObjectIdArray, ref nFetched); } catch (System.Exception e) { if (e is System.Runtime.InteropServices.COMException) { System.Runtime.InteropServices.COMException ex = (System.Runtime.InteropServices.COMException)e; Console.WriteLine("enumObj Next, hr={0:X}", (uint)ex.ErrorCode); } else { Console.WriteLine(e.Message); } //Console.WriteLine(e.StackTrace); result = false; nFetched = 0; break; } if (0 < nFetched) { countContent += nFetched; for (uint index = 0; index < nFetched; ++index) { result = wpdEnumContent(strObjectIdArray[index], content); if (false == result) { break; } } if (false == result) { nFetched = 0; break; } } } while (0 < nFetched); enumObj = null; return(result); }
static void Main(string[] args) { PortableDeviceApiLib.IPortableDeviceValues clientInfo = (PortableDeviceApiLib.IPortableDeviceValues) new PortableDeviceTypesLib.PortableDeviceValues(); { clientInfo.SetStringValue(ref WPD_CLIENT_NAME, szClientName); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_MAJOR_VERSION, dwClientVersionMajor); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_MINOR_VERSION, dwClientVersionMinor); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_REVISION, dwClientRevision); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_SECURITY_QUALITY_OF_SERVICE, SECURITY_IMPERSONATION); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_DESIRED_ACCESS, GENERIC_READ); clientInfo.SetUnsignedIntegerValue(ref WPD_CLIENT_SHARE_MODE, FILE_SHARE_READ | FILE_SHARE_WRITE); } for (uint loopCount = 0; loopCount < 100; ++loopCount) { PortableDeviceApiLib.PortableDeviceManager devMgr = new PortableDeviceApiLib.PortableDeviceManager(); devMgr.RefreshDeviceList(); string strDeviceId = null; uint nDeviceCount = 1; // 1 is allowed. over 2, leak CoTaskMemFree at marshall System.Diagnostics.Debug.Assert(1 == nDeviceCount); devMgr.GetDevices(ref strDeviceId, ref nDeviceCount); System.Console.WriteLine("DeviceCount={0}", nDeviceCount); if (0 < nDeviceCount) { //Console.WriteLine(strDeviceId); PortableDeviceApiLib.PortableDevice dev = new PortableDeviceApiLib.PortableDevice(); dev.Open(strDeviceId, clientInfo); PortableDeviceApiLib.IPortableDeviceContent content = null; dev.Content(out content); if (null != content) { for (uint count = 0; count < 30; ++count) { Console.WriteLine("enumrate start"); countContent = 0; bool result = wpdEnumContent(WPD_DEVICE_OBJECT_ID, content); Console.WriteLine("enumrate end: count count={0}", countContent); if (false == result) { break; } System.Threading.Thread.Sleep(1 * 1000); } content = null; } dev.Close(); dev = null; } devMgr = null; System.Threading.Thread.Sleep(1 * 1000); } }