Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Copy file to Android using IFileOperation::CopyItem, NO ERRORS, not working



Permlink Replies: 6 - Last Post: Nov 11, 2016 11:31 PM Last Post By: Asger Joergensen Threads: [ Previous | Next ]
Asger Joergensen

Posts: 370
Registered: 11/18/08
Copy file to Android using IFileOperation::CopyItem, NO ERRORS, not working
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 3:04 AM
Hi

Im trying to find a way to copy a file to an Android subfolder.

I have the path to the android subfolder: ::{20D04FE0-3AEA-1069-A2D8-08002B30309D}\\\?\usb#vid_17ef&pid_7a31&mi_00#6&6d088e9&0&0000#{6ac27878-a6fa-4155-ba85-f98f491d4f33}\SID-{10001,,4473036800}\MyFolder

And I do get the IShellItem without errors, the path also works in the explorer.
IFileOperation::CopyItem gives no errors either, but the file is NOT copied !

Here is the code I use:
HRESULT CopyItem( PCWSTR pszDest, PCWSTR pszSrcItem, PCWSTR pszNewName )
{
   // Initialize COM as STA.
   //
   HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED | COINIT_DISABLE_OLE1DDE);
   if( SUCCEEDED( hr ) )
   {
      IFileOperation *pfo;
 
      // Create the IFileOperation interface
      //
      hr = CoCreateInstance( CLSID_FileOperation, NULL, CLSCTX_ALL, IID_IFileOperation, (LPVOID*)&pfo );
 
      if( SUCCEEDED( hr ) )
      {
         // Set the operation flags. Turn off all UI from being shown to the user during
         // the operation. This includes error, confirmation, and progress dialogs.
         //
         hr = pfo->SetOperationFlags(FOF_NO_UI);
         if( SUCCEEDED( hr ) )
         {
            // Create an IShellItem from the supplied source path.
            //
            IShellItem *psiFrom = NULL;
 
            hr = SHCreateItemFromParsingName( pszSrcItem, NULL, IID_IShellItem, (LPVOID*)&psiFrom );
 
            if( SUCCEEDED( hr ) )
            {
               IShellItem *psiTo = NULL;
 
               if (NULL != pszDest)
               {
                  // Create an IShellItem from the supplied destination path.
                  //
                  hr = SHCreateItemFromParsingName(pszDest, NULL, IID_IShellItem, (LPVOID*)&psiTo );
 
               }
 
               if( SUCCEEDED( hr ) )
               {
                  // Add the operation
                  //
                  hr = pfo->CopyItem(psiFrom, psiTo, pszNewName, NULL);
 
                  if (NULL != psiTo)
                  {
                     psiTo->Release();
                  }
               }
 
               psiFrom->Release();
            }
 
            if( SUCCEEDED( hr ) )
            {
               // Perform the operation to copy the file.
               //
               hr = pfo->PerformOperations();
            }
         }
 
         // Release the IFileOperation interface.
         //
         pfo->Release();
      }
 
      CoUninitialize();
   }
   return hr;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::ButCopyFileClick(TObject *Sender)
{
    String dst = AppSettings.AndroidRootPath + L"\\MyFolder\\";
    String src = L"H:\\!Projects\\TestsFolder\\test.xml";
 
    HRESULT hr = CopyItem( dst.c_str(), src.c_str(), 0 );
 
    if( SUCCEEDED( hr ) )
      Beep(500,200);
}


I get a Beep every time, but the file is just not present on the Android destination.

P.s. If I change the dst path to something local on pc everything works fine

Hope somebody can tell me what I'm doing wrong or maybe an other way to accomplish my task

Thanks in advance
Best regards
Asger

Edited by: Asger Joergensen on Nov 11, 2016 12:23 PM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Copy file to Android using IFileOperation::CopyItem, NO ERRORS, notworking [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 11:35 AM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

And I do get the IShellItem without errors, the path also works
in the explorer. IFileOperation::CopyItem gives no errors either,
but the file is NOT copied !

After PerformOperations() returns, call GetAnyOperationsAborted() to check
if the copy was cancelled:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb775769.aspx

Call this method after IFileOperation::PerformOperations returns.

You should call IFileOperation::GetAnyOperationsAborted regardless of whether
IFileOperation::PerformOperations returned a success or failure code. A success
code can be returned even if the operation was stopped by the user or the
system.

--
Remy Lebeau (TeamB)
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Copy file to Android using IFileOperation::CopyItem, NO ERRORS, notworking [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 3:20 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy

Remy Lebeau (TeamB) wrote:

After PerformOperations() returns, call GetAnyOperationsAborted() to check
if the copy was cancelled:

https://msdn.microsoft.com/en-us/library/windows/desktop/bb775769.aspx

Yes it is aborted, but not by any user.
Is there anything I can do about that ?

I have now tested on two different phones and it is the same outcome.

Thanks
Best regards
Asger

Edited by: Asger Joergensen on Nov 12, 2016 1:31 AM
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Copy file to Android using IFileOperation::CopyItem, NO ERRORS,notworking [Edit] [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 5:27 PM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

Yes it is aborted, but not by any user.

Then it is being "silently cancelled by the system", which is allowed. But
there is no way to discover from IFileOperation WHY it is being aborted.

I have now tested on two different phones and it is the same outcome.

Then it is probably an issue with the USB drivers. On the other hand, you
are using an outlandishly complicated path to access the remote folder.
You should not need to do that.

--
Remy Lebeau (TeamB)
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Copy file to Android using IFileOperation::CopyItem, NO ERRORS,notworking [Edit] [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 6:22 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
HI Remy

Remy Lebeau (TeamB) wrote:
Asger wrote:

Yes it is aborted, but not by any user.

Then it is being "silently cancelled by the system", which is allowed. But
there is no way to discover from IFileOperation WHY it is being aborted.

I have now tested on two different phones and it is the same outcome.

Then it is probably an issue with the USB drivers. On the other hand, you
are using an outlandishly complicated path to access the remote folder.
You should not need to do that.

It is the path I get when using ShellFolder->GetDisplayNameOf(....// SHGDN_FORPARSING
and it is the only one that works as I mention in my other post:

Human readable and reliable path to an android subfolder ?

The strange thing is that if I use the explorer I can easily copy files, create folders
and the path that is visible in the explorer is also human readable.

But there is a difference, there is no normal right-click menu, in the explorer and
double clicking on a jpg file, does not start my standard image viewer, drag and drop
to apps doesn't work either.

So my guess is that windows are doing something else behind the scene, but
that is just my guess.

Have you ever made it work ?

Thanks for your help
Best regards
Asger
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Copy file to Android using IFileOperation::CopyItem, NOERRORS,notworking [Edit] [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 6:46 PM   in response to: Asger Joergensen in response to: Asger Joergensen
Asger wrote:

The strange thing is that if I use the explorer I can easily copy
files, create folders and the path that is visible in the explorer is
also human readable.

Human-readable paths are not obtained using SHGDN_FORPARSING. Use SHGDN_NORMAL
or SHGDN_FORADDRESSBAR instead.

But there is a difference, there is no normal right-click menu, in the
explorer and double clicking on a jpg file, does not start my standard
image viewer, drag and drop to apps doesn't work either.

You are likely accessing the device through a virtual folder implemented
as a shell namespace extension. That would allow the user to interact with
the device through Explorer, but not be treated as a normal filesystem folder.

Have you ever made it work ?

Never tried.

--
Remy Lebeau (TeamB)
Asger Joergensen

Posts: 370
Registered: 11/18/08
Re: Copy file to Android using IFileOperation::CopyItem, NOERRORS,notworking [Edit] [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Nov 11, 2016 6:54 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Hi Remy

Remy Lebeau (TeamB) wrote:
Asger wrote:

The strange thing is that if I use the explorer I can easily copy
files, create folders and the path that is visible in the explorer is
also human readable.

Human-readable paths are not obtained using SHGDN_FORPARSING.

It works fine for normal path, i got it from the very old (BCB5) virtual listview example .

Use SHGDN_NORMAL
or SHGDN_FORADDRESSBAR instead.

I'l try the last one.


But there is a difference, there is no normal right-click menu, in the
explorer and double clicking on a jpg file, does not start my standard
image viewer, drag and drop to apps doesn't work either.

You are likely accessing the device through a virtual folder implemented
as a shell namespace extension. That would allow the user to interact with
the device through Explorer, but not be treated as a normal filesystem folder.

Exactly I just got this path when using OpenTextFileDialog to fetch a txt file
on my Android phone

C:\Users\username\AppData\Local\Microsoft\Windows\Temporary Internet Files\Content.IE5\JJOB9LFN\14-15_0001[1].txt

both Vista file dialogs does not pass the path, but only the filename, so they don't
work at all, the open OpenTextFileDialog kind off works, but SaveTextFileDialog
cant find the file, so there is definitely something fishy going on.


Have you ever made it work ?

Never tried.

I, for one, would love to see you fix it ;-)

Thanks for helping
Best regards
Asger

Edited by: Asger Joergensen on Nov 12, 2016 4:08 AM

Edited by: Asger Joergensen on Nov 12, 2016 8:29 AM
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02