no

Copy a file from desktop/pc to a pda or mobile device and vice versa using RAPI API

Objective: -To copy a file from desktop to a pda device and vice versa using RAPI API. -http://msdn.microsoft.com/en-us/library/aa921197.a...

Objective:
-To copy a file from desktop to a pda device and vice versa using RAPI API.
-http://msdn.microsoft.com/en-us/library/aa921197.aspx

Requirement:
-microsoft activesync must be installed
-of course either .net framework 2 or greater

To implement a RAPI API functionalities we need to define 2 class. One is the enumeration of structures, functions, interface that we will use and the other is the class that will implement the actual copy function.

I will just define the 2 classes then:

/**
 * @author edwardpantojalegaspi
 * @since 2009.09.15
 * */

using System.Runtime.InteropServices;
using System;

namespace Ipiel.Framework.RAPI
{
 public class RapiApi
 {
  [StructLayout(LayoutKind.Sequential)]
   public struct RAPIINIT
  {
   public int cbSize;
   public int heRapiInit;
   public int hrRapiInit;
  };

  public static int FILE_ATTRIBUTE_NORMAL = 128;
  public static int INVALID_HANDLE_VALUE = -1;

  public static int S_OK = 0;
  public static int GENERIC_READ = -2147483648;
  public static int GENERIC_WRITE = -1073741824;  
  public static int CREATE_NEW = 1;
  public static int CREATE_ALWAYS = 2;
  public static int OPEN_EXISTING = 3;
  public static int OPEN_ALWAYS = 4;
  public static int TRUNCATE_EXISTING = 5;
  
  public static int ERROR_FILE_EXISTS = 80;
  public static int ERROR_INVALID_PARAMETER = 87;
  public static int ERROR_DISK_FULL = 112;
  public static int ERROR_FILE_NOT_FOUND = 2;

  [DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
  public static extern int WaitForSingleObject(int handle, int milliseconds);

  [DllImport("Kernel32.dll", EntryPoint = "RtlZeroMemory", SetLastError = false)]
  public static extern void ZeroMemory(IntPtr dest, int size);

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeRapiInit();

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeRapiInitEx(ref RAPIINIT pRapiInit);

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeCreateFile(string lpfilename, int dwDesiredAccess, int dwShareMode, int lpSecurityAttributes, int dwCreationDisposition, int dwFlagsAndAttributes, int hTemplatefile);

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeGetFileSize(int hFile, int lpFileSizeHigh);

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeReadFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToRead, ref int lpNumberOfBytesRead, int lpOverlapped);

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeWriteFile(int hFile, byte[] lpBuffer, int nNumberOfBytesToWrite, ref int lpNumberOfBytesWritten, int lpOverlapped);

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeCloseHandle(int hobject);

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeRapiUninit();

  [DllImport("rapi.dll", CharSet = CharSet.Unicode)]
  public static extern int CeDeleteFile(string DeviceFileName);
 }
}

And the actual class that implements the copy from desktop to mobile and vice versa function:

/**
 * @author edwardpantojalegaspi
 * @since 2009.09.15
 * */

using System;
using System.IO;
using System.Windows.Forms;

namespace Ipiel.Framework.RAPI
{
 /// 
 /// Summary description for Rapi.
 /// 
 public class Rapi
 {
  public Rapi()
  {
   
  }

  /// 
  /// Copies a file from Desktop to PDA.
  /// 
  /// desktop source file  /// mobile destination file  /// true if file is transferred
  public bool CopyFilePCtoPDA(String filePath, String desFile) 
  {
   bool flag = true;
   FileStream sourceFile = null;

   try 
   {
    RapiApi.RAPIINIT ri = new RapiApi.RAPIINIT();
    ri.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(ri);

    int hr = RapiApi.CeRapiInitEx(ref ri);
    if (ri.hrRapiInit == RapiApi.S_OK) 
    {
     sourceFile = new FileStream(filePath, FileMode.Open);
     int Handle = RapiApi.CeCreateFile(desFile, RapiApi.GENERIC_WRITE, 0, 0, RapiApi.CREATE_ALWAYS, RapiApi.FILE_ATTRIBUTE_NORMAL, 0);

     byte[] bytes = new byte[sourceFile.Length];
     int numBytesToRead = (int)sourceFile.Length;
     int numBytesRead = 0;
     int lpNumberofBytesWritten = 0;

     while (numBytesToRead > 0)
     {
      // Read may return anything from 0 to numBytesToRead.
      int n = sourceFile.Read(bytes, numBytesRead, numBytesToRead);

      // Break when the end of the file is reached.
      if (n == 0)
       break;

      numBytesRead += n;
      numBytesToRead -= n;
     }
     numBytesToRead = bytes.Length;
   
     RapiApi.CeWriteFile(Handle, bytes, numBytesToRead, ref lpNumberofBytesWritten, 0);
     RapiApi.CeCloseHandle(Handle);
    }
    else 
    {
     //timeout error
     flag = false;
    }
   } 
   catch(Exception e) 
   {
    //error found
    RapiApi.CeRapiUninit();
    flag = false;
   }
   finally 
   {
    RapiApi.CeRapiUninit();
    if(sourceFile != null)
     sourceFile.Close();
   }
   return flag;
  }
  
  
  public delegate void CopyFileCallBack(int bytesCopied, int totalBytes);
  /// 
  /// CallBack to avoid cross-threads.
  /// 
  /// actual bytes copied  /// total bytes to be copied  private void CallBack(int bytesCopied, int totalBytes)
  {
   //you can implement a progressbar here
   //if not null update the progress bytes
   /*if ((progressBar != null) && (bytesCopied > 0 || totalBytes > 0))
   {
    progressBar.Maximum = totalBytes;
    progressBar.Value = bytesCopied;
   }
   progressBar.Refresh();*/
  }

  /// 
  /// Copies file from PDA to Desktop
  /// 
  /// desktop destination file  /// mobile source file  public void CopyPDAtoPC(string deskTopFileName, string PDAFileName)
  {
   //enable the progress bar
   progressBar.Enabled = true;
   progressBar.Visible = true;
   progressBar.Show();

   CopyPDAtoPC(deskTopFileName, PDAFileName, null);
  }

  /// 
  /// Copies file from PDA to Desktop
  /// 
  /// desktop destination file  /// mobile source file  ///   public void CopyPDAtoPC(string deskTopFileName, string PDAFileName, CopyFileCallBack cb)
  {
   int Handle;
   int bufferSize = 32768;
   byte[] lpBuffer = new byte[bufferSize + 1];
   System.IO.FileStream outFile;

   try
   {
    #region If no PDA connected connection timeout
    RapiApi.RAPIINIT ri = new RapiApi.RAPIINIT();
    ri.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(ri);

    int hr = RapiApi.CeRapiInitEx(ref ri);
    if (ri.hrRapiInit == RapiApi.S_OK)
    {
     //initialized file for reading
     outFile = new System.IO.FileStream(deskTopFileName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
     int lpNumberofBytesRead = bufferSize;
     Handle = RapiApi.CeCreateFile(PDAFileName, RapiApi.GENERIC_READ, 0, 0, RapiApi.OPEN_EXISTING, RapiApi.FILE_ATTRIBUTE_NORMAL, 0);

     int fileSize = RapiApi.CeGetFileSize(Handle, 0);
     int bytesToRead = bufferSize;
     int bytesRead = 0;

     while (lpNumberofBytesRead > 0)
     {
      RapiApi.CeReadFile(Handle, lpBuffer, bytesToRead, ref lpNumberofBytesRead, 0);
      outFile.Write(lpBuffer, 0, lpNumberofBytesRead);
      bytesRead += lpNumberofBytesRead;
      if ((cb != null))
      {
       cb(bytesRead, fileSize);
      }
      else
      {
       CallBack(bytesRead, fileSize);
      }
     }
     outFile.Close();
     RapiApi.CeCloseHandle(Handle);
    }
    else
    {
     throw new Exception("Timeout - No Device");
    }
    #endregion
   }
   catch (Exception ex)
   {
    throw ex;
   }
   finally
   {
    //disble the progress bar
    //progressBar.Enabled = false;
    //progressBar.Visible = false;
    RapiApi.CeRapiUninit();
   }            
  }

  /// 
  /// Dispose this object.
  /// 
  public void Dispose() 
  {
   RapiApi.CeRapiUninit();
  }
 }
}

Post a Comment Default Comments

item