Freigeben über


CoreCon API – Part II

Let’s see some ways to do send/receive files to device and some process related functions. CoreCon provides a class called FileDeployer using which we can do file transfers across device and desktop. You should have established connection (Device.Connect()) to use the FileDeployer methods.

 

Device myDevice = GetPPC05Emulator();

myDevice.Connect();

FileDeployer fileDeployer = myDevice.GetFileDeployer();

fileDeployer.SendFile("foo.txt",@"%CSIDL_PROGRAM_FILES%\foo.txt");

 

This deploys the foo.txt (present in current directory) to “\Program files” folder in device. Note that you can use CSIDL paths in the both source and destination file names. CSIDL values provide a unique system-independent way to identify special folders used frequently by applications, but which may not have the same name or location on any given system. Similarly there are APIs to do receive file and deploy bunch of files (using package) at a time. I will be discussing about the packages in detail in following blogs.

 

 RemoteProcess class gives us a way to start/stop process. Below code snippet starts a new process. Waits for some time (5 seconds), List all the process running and kills the started process.

Device myDevice = GetPPC05Emulator();

myDevice.Connect();

 

//Start the calculator process in device

RemoteProcess remoteProc = myDevice.GetRemoteProcess();

bool bStarted = remoteProc.Start(@"%CSIDL_WINDOWS%\calc.exe", " ");

//List all running process

Collection<RemoteProcess> remoteProcCollection = myDevice.GetRunningProcesses();

int num = 0;

foreach (RemoteProcess rproc in remoteProcCollection)

{

num++;

      System.Console.WriteLine(num.ToString() + ". " + rproc.FileName);

}

//Kill the process

if (remoteProc.HasExited() == false)

{

remoteProc.Kill();

}

 

Let’s put all these together and write a small utility to do file listing in device. We will have a native app(FileList.exe) in device which will iterate the specified directory and print file names in a log. Our app will deploy this filelist.exe, run it with various parameters, get the log file and print in the console.

 

Create a Smartdevice console app targeting PPC 05. Copy paste the below code.

 

Source for FileList.cpp

/*

 * Gets the list of files/directories present int the path specified in cmd line argument.

 */

int _tmain(int argc, _TCHAR* argv[])

{

      int iRetVal = 0;

      if(argc <=1)

      {

            //No args passed

            return -1;

      }

      DeleteFile(L"\\foo.txt");

      HANDLE hLog = CreateFile(L"\\foo.txt",GENERIC_WRITE,0,NULL,OPEN_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);

      if(hLog == INVALID_HANDLE_VALUE)

      {

            //Error in creating log file

            return -1;

      }

      //Search the Directory

      TCHAR* szPath = new TCHAR[_tcslen(argv[1]) + 4];

      _tcscpy(szPath, argv[1]);

      _tcscat(szPath, L"\\*");

      WIN32_FIND_DATA findData;

      HANDLE hSearchFiles = INVALID_HANDLE_VALUE;

      hSearchFiles = FindFirstFile(szPath, &findData);

      if(hSearchFiles != INVALID_HANDLE_VALUE)

      {

            do

            {

                  TCHAR* szBuffer = new TCHAR[_tcslen(findData.cFileName) + 4];

                  _tcscpy(szBuffer, findData.cFileName );

                  _tcscat(szBuffer,L"\r\n");

                  DWORD numBytes;

                  WriteFile(hLog, szBuffer, _tcslen(szBuffer)*sizeof(TCHAR), &numBytes, NULL);

                  delete[] szBuffer;

            }while(FindNextFile(hSearchFiles, &findData)) ;

      }

      else

      {

            iRetVal = -1;

      }

      //Clean up

      if(szPath != NULL)

      {

            delete[] szPath;

            szPath = NULL;

      }

      if(hLog != INVALID_HANDLE_VALUE)

            CloseHandle(hLog);

      if(hSearchFiles != INVALID_HANDLE_VALUE)

            CloseHandle(hSearchFiles);

     

      return iRetVal;

}

 

Create a C# console application. Copy paste the below code in program.cs.

Place the FileList.exe in the same directory as output directory.

 

 

using System;

using System.Collections.Generic;

using System.Text;

using Microsoft.SmartDevice.Connectivity;

using System.Collections.ObjectModel;

namespace sample1

{

    class Program

    {

        static void Main(string[] args)

        {

            Device myDevice = GetPPC05Emulator();

            //Connect to the device.

            myDevice.Connect();

            FileDeployer fileDeployer = myDevice.GetFileDeployer();

            fileDeployer.SendFile("FileList.exe", @"%CSIDL_PROGRAM_FILES%\FileList.exe");

           

            //Start FileList process in device

            RemoteProcess remoteProc = myDevice.GetRemoteProcess();

            bool bStarted = remoteProc.Start(@"%CSIDL_PROGRAM_FILES%\FileList.exe", @"\Program files");

            if (bStarted == true)

            {

                while (remoteProc.HasExited() == false)

                {

                    System.Threading.Thread.Sleep(2000);

                }

            }

            int exitCode = remoteProc.GetExitCode();

            if (exitCode == 0)

            {

                fileDeployer.ReceiveFile(@"\foo.txt", "Filelist.txt");

            }

        }

        static Device GetPPC05Emulator()

        {

            DatastoreManager dsmgrObj = new DatastoreManager(1033);

            ObjectId ppc05PlatObjID = new ObjectId("4118C335-430C-497f-BE48-11C3316B135E");

            ObjectId PPC05EmuID = new ObjectId("25D984D9-0DFE-4DB1-A5A0-9A4F660BF2CE");

            Platform ppc05Plat = dsmgrObj.GetPlatform(ppc05PlatObjID);

            Device ppc05Emu = ppc05Plat.GetDevice(PPC05EmuID);

            return ppc05Emu;

        }

    }

}

 

 

Now, this utility could be used to do file listing of device.

 

--

Anand R

Comments

  • Anonymous
    December 07, 2007
    The Visual Studio for Devices team has been posting a very interesting and informative series on the

  • Anonymous
    December 07, 2007
    The Visual Studio for Devices team has been posting a very interesting and informative series on the