0

We have 3rd party program to Download, delete and upload *.db files from Android to Windows. Everything work prefectly but with Windows 10 1703, the Upload stopped working (I mean From Windows 10 to the Android Device). My error is in french but the translation must be : "Attempted read or write protected memory. This often [...]". The strange part is that the Download and Delete still work. Oh, and everything works with Windows 10 1607.

The error pop at the line : "targetStream.Write(buffer, (int)optimalTransferSizeBytes, pcbWritten); "

I was looking to upload the file in many folder on my Android Devide, but nothing work. Also, I tryed few type of device, LG phone, S7 phone, CT4 Device.

I'm looking into Phone permission and Windows 10 Permissions, trying to run with admin rights or compatibility modes. But I just cannot figure why it stopped working.

I'm really lost and some answer or guideline might be very usefull. Thanks a lot!

There is the Upload code (from Windows to Android) using PortableDeviceApiLib.dll :

public void TransferContentToDevice(string fileName, string parentObjectId) {

        IPortableDeviceContent content;
        this._device.Content(out content);

        parentObjectId = parentObjectId + "";

        IPortableDeviceValues values =
            GetRequiredPropertiesForContentType(fileName, parentObjectId);

        PortableDeviceApiLib.IStream tempStream;
        uint optimalTransferSizeBytes = 0;
        content.CreateObjectWithPropertiesAndData(
            values,
            out tempStream,
            ref optimalTransferSizeBytes,
            null);

        System.Runtime.InteropServices.ComTypes.IStream targetStream =
            (System.Runtime.InteropServices.ComTypes.IStream)tempStream;
        try
        {

            using (var sourceStream =
                new FileStream(fileName, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[optimalTransferSizeBytes];
                int bytesRead;

                do
                {
                    bytesRead = sourceStream.Read(buffer, 0, (int)optimalTransferSizeBytes);

                    IntPtr pcbWritten = IntPtr.Zero;

                    if (bytesRead < (int)optimalTransferSizeBytes)
                    {

                        targetStream.Write(buffer, bytesRead, pcbWritten);
                    }
                    else
                    {                                

                        targetStream.Write(buffer, (int)optimalTransferSizeBytes, pcbWritten);     

                    }

                } while (bytesRead > 0);
            }

            targetStream.Commit(0);
        }
        finally
        {
            Marshal.ReleaseComObject(tempStream);
        }
    }
Math
  • 389
  • 2
  • 4
  • 14
  • Thanks to "ben Hopkins", on another post on StackOverflow I have my answer. I have, for some reason, to run the TransferContentToDevice function in a new thread. All my thanks to this awesome human! – Math Jul 07 '17 at 14:17
  • https://stackoverflow.com/questions/43980646/system-accessviolationexception-when-copying-data-to-portable-device-after-insta – Math Jul 07 '17 at 14:18

0 Answers0