示例#1
0
        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);
            }
        }