转载《RAPI 功能示例与说明 》wince与windows之间传输文件

来源:互联网 发布:著名的国内金融数据库 编辑:程序博客网 时间:2024/06/16 03:11

原文地址:http://blog.csdn.net/91program/article/details/7561939。mark一下,感谢91program


先来 RAPI 概述

Windows CE 有了 RAPI 以后,PC 应用程序就能远程管理 Windows CE 设备。
导出函数将处理文件系统、注册表和数据库以及用于查询系统配置的函数。
大多数 RAPI 是 Windows CE API 函数中的副本,只有少数的函数扩展了 API。

RAPI 函数被列在 Windows CE API 参考中,但是将有 PC 应用程序来调用而不是由 Widows CE 应用程序调用。
在函数的名称都有前缀 Ce 来与他们的 Window CE 的副本进行区分;
例如:Windows CE 中的函数 GetStoreInformation 将在该函数的 RAPI 版本中称为 CeGetStoreInformation。

在 Windows CE 应用程序将不会调用 RAPI 函数。

知道大家不想看文字,直接上示例(功能:复制文件到设备上)代码:
RAPI 被封装为一个类,先看头文件。
CeRAPI.h 

[cpp] view plaincopy
  1. // CeRAPI.h: interface for the CeRAPI class.  
  2. //  
  3. //////////////////////////////////////////////////////////////////////  
  4.   
  5. #if !defined(AFX_VOGUERAPI_H__10D13979_C6F2_4922_A4D7_B105644F969F__INCLUDED_)  
  6. #define AFX_VOGUERAPI_H__10D13979_C6F2_4922_A4D7_B105644F969F__INCLUDED_  
  7.   
  8. #if _MSC_VER > 1000  
  9. #pragma once  
  10. #endif // _MSC_VER > 1000  
  11.   
  12. #define BUFFER_SIZE 10240  
  13.   
  14. typedef struct _RAPIINIT  
  15. {   
  16.     DWORD cbSize;   
  17.     HANDLE heRapiInit;   
  18.     HRESULT hrRapiInit;   
  19. }  
  20. RAPIINIT;  
  21.   
  22. typedef struct _CE_FIND_DATA {  
  23.     DWORD    dwFileAttributes;  
  24.     FILETIME ftCreationTime;  
  25.     FILETIME ftLastAccessTime;  
  26.     FILETIME ftLastWriteTime;  
  27.     DWORD    nFileSizeHigh;  
  28.     DWORD    nFileSizeLow;  
  29.     DWORD    dwOID;  
  30.     WCHAR    cFileName[MAX_PATH];  
  31. } CE_FIND_DATA, *LPCE_FIND_DATA;  
  32.   
  33. class CCeRAPI    
  34. {  
  35. public:  
  36.     HINSTANCE hInst;  
  37.   
  38.     typedef HANDLE (FAR PASCAL * pfnFunc0)(LPCWSTRDWORDDWORD, LPSECURITY_ATTRIBUTES, DWORDDWORDHANDLE);  
  39.     typedef BOOL   (FAR PASCAL * pfnFunc1)(HANDLELPCVOIDDWORDLPDWORD, LPOVERLAPPED);  
  40.     typedef BOOL   (FAR PASCAL * pfnFunc2)(HANDLE);   
  41.     typedef HANDLE (FAR PASCAL * pfnFunc3)(LPCWSTR, LPCE_FIND_DATA);  
  42.     typedef DWORD  (FAR PASCAL * pfnFunc4)(HANDLELPDWORD);  
  43.     typedef BOOL   (FAR PASCAL * pfnFunc5)(HANDLELPVOIDDWORDLPDWORD, LPOVERLAPPED);  
  44.     typedef BOOL   (FAR PASCAL * pfnFunc6)(HANDLE, LPCE_FIND_DATA);  
  45.     typedef BOOL   (FAR PASCAL * pfnFunc7)(LPCWSTR, LPSECURITY_ATTRIBUTES);  
  46.     typedef BOOL   (FAR PASCAL * pfnFunc8)(LPCWSTRLPCWSTR, LPSECURITY_ATTRIBUTES, LPSECURITY_ATTRIBUTES, BOOLDWORDLPVOIDLPWSTR, LPSTARTUPINFO, LPPROCESS_INFORMATION);   
  47.     typedef VOID   (FAR PASCAL * pfnFunc9)(LPSYSTEM_INFO);  
  48.     typedef HRESULT (FAR PASCAL *pfnFnucA)(RAPIINIT*);  
  49.     typedef BOOL (FAR PASCAL *pfnFuncB)(LPCWSTR,LPCWSTR,BOOL);  
  50.     typedef DWORD (FAR PASCAL *pfnFuncC)();  
  51.     typedef HRESULT (FAR PASCAL *pfnFuncD)();  
  52.   
  53.     pfnFunc0 CeCreateFile;  
  54.     pfnFunc1 CeWriteFile;  
  55.     pfnFunc2 CeCloseHandle;  
  56.     pfnFunc3 CeFindFirstFile;  
  57.     pfnFunc4 CeGetFileSize;  
  58.     pfnFunc5 CeReadFile;  
  59.     pfnFunc6 CeFindNextFile;  
  60.     pfnFunc7 CeCreateDirectory;  
  61.     pfnFunc8 CeCreateProcess;   
  62.     pfnFunc9 CeGetSystemInfo;  
  63.     FARPROC  CeRapiUninit;  
  64.     //FARPROC  CeRapiInit;  
  65.     pfnFuncB CeCopyFile;  
  66.     pfnFuncC CeGetLastError;  
  67.     pfnFuncD CeRapiGetError;  
  68.   
  69.     //Leo Add  
  70.     pfnFnucA CeRapiInitEx;  
  71.   
  72.     BOOL m_bRapiInitFlag;  
  73.   
  74.     BOOL RapiConnectDevice();  
  75.   
  76.     BOOL CopyFiletoWinCE(CString strFileNamePC, CString strFileNamePPC);  
  77.     BOOL CCeRAPI::CopyFiletoPC(CString csCEFilename,CString csPCFilename);  
  78.     BOOL InitRapi();  
  79. //  CString GetCStringFromFile(CString strFileName);  
  80.     CCeRAPI();  
  81.     virtual ~CCeRAPI();  
  82.   
  83. };  
  84.   
  85. #endif // !defined(AFX_VOGUERAPI_H__10D13979_C6F2_4922_A4D7_B105644F969F__INCLUDED_)  

类的实现文件:CeRAPI.cpp 
[cpp] view plaincopy
  1. // CeRAPI.cpp: implementation of the CeRAPI class.  
  2. //  
  3. //////////////////////////////////////////////////////////////////////  
  4.   
  5. #include "stdafx.h"  
  6. #include "RapiTest.h"  
  7. #include "CeRAPI.h"  
  8.   
  9. #ifdef _DEBUG  
  10. #undef THIS_FILE  
  11. static char THIS_FILE[]=__FILE__;  
  12. #define new DEBUG_NEW  
  13. #endif  
  14.   
  15. //////////////////////////////////////////////////////////////////////  
  16. // Construction/Destruction  
  17. //////////////////////////////////////////////////////////////////////  
  18.   
  19. CCeRAPI::CCeRAPI()   
  20. {  
  21.     m_bRapiInitFlag = FALSE;  
  22. }  
  23. CCeRAPI::~CCeRAPI()  
  24. {  
  25. }  
  26.   
  27. BOOL CCeRAPI::InitRapi()  
  28. {  
  29.     LPTSTR pDllPath = new TCHAR[MAX_PATH + 10];  
  30.     GetSystemDirectory(pDllPath, MAX_PATH);  
  31.     CString strDllPathName(pDllPath);  
  32.     strDllPathName += "\\Rapi.dll";  
  33. TRACE("%s\n",strDllPathName);   //C:\WINDOWS\system32\rapi.dll  
  34.     hInst = LoadLibrary(strDllPathName);  
  35.     if(hInst)  
  36.     {  
  37.         //CeRapiInit = (FARPROC) GetProcAddress(hInst, "CeRapiInit");  
  38.         CeRapiInitEx = (pfnFnucA) GetProcAddress(hInst, "CeRapiInitEx");  
  39.         CeRapiUninit = (FARPROC) GetProcAddress(hInst, "CeRapiUninit");  
  40.         CeCreateFile = (pfnFunc0)GetProcAddress(hInst, "CeCreateFile");  
  41.         CeWriteFile = (pfnFunc1)GetProcAddress(hInst, "CeWriteFile");  
  42.         CeCloseHandle = (pfnFunc2)GetProcAddress(hInst, "CeCloseHandle");  
  43.         CeFindFirstFile = (pfnFunc3)GetProcAddress(hInst, "CeFindFirstFile");  
  44.         CeGetFileSize = (pfnFunc4)GetProcAddress(hInst, "CeGetFileSize");  
  45.         CeReadFile = (pfnFunc5)GetProcAddress(hInst, "CeReadFile");  
  46.         CeFindNextFile = (pfnFunc6)GetProcAddress(hInst, "CeFindNextFile");  
  47.         CeCreateDirectory = (pfnFunc7)GetProcAddress(hInst, "CeCreateDirectory");  
  48.         CeCreateProcess = (pfnFunc8)GetProcAddress(hInst, "CeCreateProcess");  
  49.         CeGetSystemInfo = (pfnFunc9)GetProcAddress(hInst, "CeGetSystemInfo");  
  50.         CeCopyFile = (pfnFuncB)GetProcAddress(hInst, "CeCopyFile");  
  51.         CeGetLastError = (pfnFuncC)GetProcAddress(hInst, "CeGetLastError");  
  52.         CeRapiGetError = (pfnFuncD)GetProcAddress(hInst, "CeRapiGetError");  
  53.   
  54.         return TRUE;  
  55.     }  
  56.     else  
  57.     {  
  58.         FreeLibrary(hInst);  
  59.   
  60.         return FALSE;  
  61.     }  
  62.   
  63.     return FALSE;  
  64. }  
  65.   
  66. BOOL CCeRAPI::CopyFiletoPC(CString csCEFilename,CString csPCFilename)  
  67. {  
  68.     HANDLE hCEFile;  
  69.     DWORD dwFileSize = 0;  
  70.     int iFileBlockSize = 0;  
  71.     DWORD dwBytes = 0;  
  72.     char cBlockSize[BUFFER_SIZE];  
  73.     int i = 0;  
  74.     DWORD dwSizeRet = 0 ;  
  75.   
  76.     CFile PCFile;  
  77.   
  78.     BSTR bstr = csCEFilename.AllocSysString();    
  79.     SysFreeString(bstr);  
  80.   
  81.     hCEFile = CeCreateFile(bstr, GENERIC_READ, 0, NULL,  
  82.             OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);  
  83.     if(INVALID_HANDLE_VALUE == hCEFile)  
  84.     {  
  85.         TRACE("==Fail to open device file:%d\n",GetLastError());  
  86.         return FALSE;  
  87.     }  
  88.     dwSizeRet = CeGetFileSize(hCEFile,&dwFileSize);  
  89.     TRACE("==Read file size return value:%d\n",dwSizeRet);  
  90.     dwFileSize = dwSizeRet + (dwFileSize << 32);  
  91.   
  92.     iFileBlockSize = dwFileSize / BUFFER_SIZE;  
  93.   
  94.     PCFile.Open(csPCFilename,CFile::modeReadWrite | CFile::typeBinary | CFile::modeCreate);  
  95.   
  96.     for(i = 0;i < iFileBlockSize;i++)  
  97.     {  
  98.         memset(cBlockSize,0,sizeof(char) * BUFFER_SIZE);  
  99.         CeReadFile(hCEFile,cBlockSize,BUFFER_SIZE,&dwBytes,NULL);  
  100.         PCFile.Write(cBlockSize,BUFFER_SIZE);  
  101.     }  
  102.     if(0 != dwFileSize % BUFFER_SIZE)  
  103.     {  
  104.         memset(cBlockSize,0,sizeof(char) * BUFFER_SIZE);  
  105.         CeReadFile(hCEFile,cBlockSize,dwFileSize % BUFFER_SIZE,&dwBytes,NULL);  
  106.         PCFile.Write(cBlockSize,dwFileSize % BUFFER_SIZE);  
  107.     }  
  108.   
  109.     CeCloseHandle(hCEFile);  
  110.     PCFile.Close();  
  111.     return TRUE;  
  112. }  
  113. BOOL CCeRAPI::CopyFiletoWinCE(CString csPCFilename, CString csCEFilename)  
  114. {  
  115.     CFile PCFile;  
  116.     HANDLE hCEFile;  
  117.     char cTemp[BUFFER_SIZE];  
  118.     DWORD dwBytes = 0;  
  119.     int iFileLen = 0;  
  120.     int iFileNum = 0;  
  121.     int i = 0;  
  122.   
  123.     PCFile.Open(csPCFilename, CFile::modeRead |CFile::typeBinary);  
  124.     iFileLen = PCFile.GetLength();   
  125.     iFileNum = iFileLen / BUFFER_SIZE;  
  126.   
  127.     BSTR bstr = csCEFilename.AllocSysString();   
  128.     SysFreeString(bstr);  
  129.   
  130.     hCEFile = CeCreateFile(bstr, GENERIC_READ | GENERIC_WRITE, 0, NULL,  
  131.             OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);  
  132.     if(INVALID_HANDLE_VALUE == hCEFile) //文件打开失败  
  133.     {  
  134.         return FALSE;  
  135.     }  
  136.       
  137.     for(i = 0;i < iFileNum;i++)  
  138.     {  
  139.         memset(cTemp,0,sizeof(char) * BUFFER_SIZE);  
  140.         if(PCFile.Read(cTemp, BUFFER_SIZE) >= 1)  
  141.         {  
  142.             CeWriteFile(hCEFile, cTemp, BUFFER_SIZE, &dwBytes, NULL);  
  143.         }  
  144.     }  
  145.     if(0 != iFileLen % BUFFER_SIZE)  
  146.     {  
  147.         memset(cTemp,0,sizeof(char) * BUFFER_SIZE);  
  148.         if(PCFile.Read(cTemp, iFileLen % BUFFER_SIZE) >= 1)  
  149.         {  
  150.             CeWriteFile(hCEFile, cTemp, iFileLen % BUFFER_SIZE, &dwBytes, NULL);  
  151.         }  
  152.     }  
  153.   
  154.     CeCloseHandle(hCEFile);  
  155.     PCFile.Close();  
  156.   
  157.     return TRUE;  
  158. }  
  159.   
  160. // CString CVogueRAPI::GetCStringFromFile(CString strFileName)  
  161. // {  
  162. //  CString strOut;  
  163. //  LPSTR pText = NULL;  
  164. //  int iLen;  
  165. //  CFile f;  
  166. //  if (f.Open(strFileName, CFile::modeReadWrite))  
  167. //  {  
  168. //      iLen = f.GetLength();  
  169. //      pText = new char[iLen + 1];  
  170. //      f.Read(pText, iLen);  
  171. //      pText[iLen] = '\0';  
  172. //      CString str(pText);  
  173. //      strOut = str;  
  174. //      delete pText;  
  175. //      f.Close();  
  176. //  }  
  177. //  else  
  178. //  {  
  179. //      strOut = "Error";  
  180. //  }  
  181. //  return strOut;  
  182. // }  
  183.   
  184. BOOL CCeRAPI::RapiConnectDevice()  
  185. {  
  186.     RAPIINIT struRapiInit;      //这个是CeRapiInitEx函数要求的入口参数: 定义在rapi.h中, 为了不include "rapi.h", 将其定义复制到工程中  
  187.     DWORD dwWaitResult = 0;     //等待初始化完成事件的变量  
  188.     HRESULT hRapiResult = NULL; //CeRapiInitEx的返回HANDLE  
  189.       
  190.     if(m_bRapiInitFlag == FALSE)    //全局的一个标志,如果初始化成功就不再重复  
  191.     {  
  192.         struRapiInit.cbSize = sizeof(RAPIINIT);     //填满该结构体仅有的三个成员  
  193.         struRapiInit.hrRapiInit = NULL;             //明知是输出参数也顺手填一下, 以防万一  
  194.         struRapiInit.heRapiInit = NULL;  
  195.           
  196.         hRapiResult = CeRapiInitEx(&struRapiInit);      //关键点  
  197.         dwWaitResult = WaitForSingleObject(struRapiInit.heRapiInit, 2000);      //关键点  
  198.           
  199.         if(hRapiResult == S_OK && struRapiInit.hrRapiInit == S_OK &&  
  200.             dwWaitResult != WAIT_TIMEOUT)    //三个返回值都判断, 以保证正确性  
  201.         {  
  202.             m_bRapiInitFlag = TRUE;  
  203.             return TRUE;  
  204.         }  
  205.         else  
  206.         {  
  207.             return FALSE;  
  208.         }  
  209.     }  
  210.     else  
  211.     {  
  212.         m_bRapiInitFlag = TRUE;  
  213.         return TRUE;  
  214.     }  
  215. }  

调试示例:
[cpp] view plaincopy
  1. // RapiTestDlg.cpp : implementation file  
  2. //  
  3.   
  4. #include "stdafx.h"  
  5. #include "RapiTest.h"  
  6. #include "RapiTestDlg.h"  
  7. #include "CeRapi.h"  
  8.   
  9. #ifdef _DEBUG  
  10. #define new DEBUG_NEW  
  11. #undef THIS_FILE  
  12. static char THIS_FILE[] = __FILE__;  
  13. #endif  
  14.   
  15. BOOL gbInitRAPI = FALSE;  
  16.   
  17. /////////////////////////////////////////////////////////////////////////////  
  18. // CRapiTestDlg dialog  
  19.   
  20. CRapiTestDlg::CRapiTestDlg(CWnd* pParent /*=NULL*/)  
  21.     : CDialog(CRapiTestDlg::IDD, pParent)  
  22. {  
  23.     //{{AFX_DATA_INIT(CRapiTestDlg)  
  24.         // NOTE: the ClassWizard will add member initialization here  
  25.     //}}AFX_DATA_INIT  
  26.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32  
  27.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);  
  28. }  
  29.   
  30. void CRapiTestDlg::DoDataExchange(CDataExchange* pDX)  
  31. {  
  32.     CDialog::DoDataExchange(pDX);  
  33.     //{{AFX_DATA_MAP(CRapiTestDlg)  
  34.         // NOTE: the ClassWizard will add DDX and DDV calls here  
  35.     //}}AFX_DATA_MAP  
  36. }  
  37.   
  38. BEGIN_MESSAGE_MAP(CRapiTestDlg, CDialog)  
  39.     //{{AFX_MSG_MAP(CRapiTestDlg)  
  40.     ON_WM_SYSCOMMAND()  
  41.     ON_WM_PAINT()  
  42.     ON_WM_QUERYDRAGICON()  
  43.     ON_BN_CLICKED(ID_CONNECT, OnConnect)  
  44.     ON_BN_CLICKED(IDC_PCTODEVICE, OnPCToDevice)  
  45.     ON_BN_CLICKED(IDC_EXITAPP, OnExitapp)  
  46.     ON_BN_CLICKED(IDC_DEVICETOPC, OnDeviceToPC)  
  47.     ON_WM_DESTROY()  
  48.     //}}AFX_MSG_MAP  
  49. END_MESSAGE_MAP()  
  50.   
  51. /////////////////////////////////////////////////////////////////////////////  
  52. // CRapiTestDlg message handlers  
  53.   
  54. BOOL CRapiTestDlg::OnInitDialog()  
  55. {  
  56.     CDialog::OnInitDialog();  
  57.       
  58.     /////////////////////////////////////  
  59.     CEdit *pDesktop = (CEdit *)GetDlgItem(IDC_DESKTOP_FILE);  
  60.     CEdit *pCE = (CEdit *)GetDlgItem(IDC_CE_FILE);  
  61.     CButton *pToDevice = (CButton *)GetDlgItem(IDC_PCTODEVICE);  
  62.     CButton *pToPC = (CButton *)GetDlgItem(IDC_DEVICETOPC);  
  63.     pDesktop->SetWindowText(_T("F:\\NAVoice.exe"));  
  64.     pDesktop->EnableWindow(FALSE);  
  65.     pCE->SetWindowText(_T("\\NAVoice.exe"));  
  66.     pCE->EnableWindow(FALSE);  
  67.     pToDevice->EnableWindow(FALSE);  
  68.     pToPC->EnableWindow(FALSE);  
  69.     /////////////////////////////////////  
  70.       
  71.     return TRUE;  // return TRUE  unless you set the focus to a control  
  72. }  
  73.   
  74. void CRapiTestDlg::OnSysCommand(UINT nID, LPARAM lParam)  
  75. {  
  76.     CDialog::OnSysCommand(nID, lParam);  
  77. }  
  78.   
  79. // If you add a minimize button to your dialog, you will need the code below  
  80. //  to draw the icon.  For MFC applications using the document/view model,  
  81. //  this is automatically done for you by the framework.  
  82.   
  83. void CRapiTestDlg::OnPaint()   
  84. {  
  85.     if (IsIconic())  
  86.     {  
  87.         CPaintDC dc(this); // device context for painting  
  88.   
  89.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);  
  90.   
  91.         // Center icon in client rectangle  
  92.         int cxIcon = GetSystemMetrics(SM_CXICON);  
  93.         int cyIcon = GetSystemMetrics(SM_CYICON);  
  94.         CRect rect;  
  95.         GetClientRect(&rect);  
  96.         int x = (rect.Width() - cxIcon + 1) / 2;  
  97.         int y = (rect.Height() - cyIcon + 1) / 2;  
  98.   
  99.         // Draw the icon  
  100.         dc.DrawIcon(x, y, m_hIcon);  
  101.     }  
  102.     else  
  103.     {  
  104.         CDialog::OnPaint();  
  105.     }  
  106. }  
  107.   
  108. // The system calls this to obtain the cursor to display while the user drags  
  109. //  the minimized window.  
  110. HCURSOR CRapiTestDlg::OnQueryDragIcon()  
  111. {  
  112.     return (HCURSOR) m_hIcon;  
  113. }  
  114.   
  115. void CRapiTestDlg::OnConnect()   
  116. {  
  117.     gbInitRAPI = rapi.InitRapi();   //打开rapi.dll,导出API  
  118.     if(TRUE == gbInitRAPI)  
  119.     {  
  120.         BOOL bSuccess = rapi.RapiConnectDevice();   //连接设备  
  121.         if(FALSE == bSuccess)  
  122.         {  
  123.             TRACE("==连接设备错误!\n");  
  124.         }  
  125.         else  
  126.         {  
  127.             CEdit *pDesktop = (CEdit *)GetDlgItem(IDC_DESKTOP_FILE);  
  128.             CEdit *pCE = (CEdit *)GetDlgItem(IDC_CE_FILE);  
  129.             CButton *pToDevice  = (CButton *)GetDlgItem(IDC_PCTODEVICE);  
  130.             CButton *pToPC = (CButton *)GetDlgItem(IDC_DEVICETOPC);  
  131.             pDesktop->EnableWindow(TRUE);  
  132.             pCE->EnableWindow(TRUE);  
  133.             pToDevice->EnableWindow(TRUE);  
  134.             pToPC->EnableWindow(TRUE);  
  135.         }  
  136.     }  
  137. }  
  138.   
  139. /* 
  140.  *在设备连接成功后,进行文件传输 
  141. */  
  142. void CRapiTestDlg::OnPCToDevice()   
  143. {  
  144.     CEdit *pDesktop = (CEdit *)GetDlgItem(IDC_DESKTOP_FILE);  
  145.     CEdit *pCEDevice = (CEdit *)GetDlgItem(IDC_CE_FILE);  
  146.     CString csDesktopFile;  
  147.     CString csCEFile;  
  148.   
  149.     pDesktop->GetWindowText(csDesktopFile);  
  150.     pCEDevice->GetWindowText(csCEFile);  
  151.     if(csCEFile.GetLength() > 1 && csDesktopFile.GetLength() > 1)  
  152.     {  
  153.         CString strOut;  
  154.   
  155.         if(TRUE == rapi.CopyFiletoWinCE(csDesktopFile, csCEFile))  
  156.         {  
  157.             strOut.Format(_T("File %s was transferred to WinCE as %s:"), csDesktopFile, csCEFile);  
  158.             AfxMessageBox(strOut);  
  159.         }  
  160.         else  
  161.         {  
  162.             strOut.Format(_T("文件打开失败!"));  
  163.             AfxMessageBox(strOut);  
  164.         }  
  165.     }  
  166.     else  
  167.     {  
  168.         AfxMessageBox(_T("Problem with filename's.... \nTry Again!!!!"));  
  169.         pDesktop->SetWindowText(_T(""));  
  170.         pCEDevice->SetWindowText(_T(""));  
  171.         pDesktop->SetFocus();  
  172.     }  
  173. }  
  174.   
  175. void CRapiTestDlg::OnExitapp()   
  176. {  
  177.     // TODO: Add your control notification handler code here  
  178.     PostQuitMessage(0);   
  179. }  
  180.   
  181. void CRapiTestDlg::OnDeviceToPC()   
  182. {  
  183.     // TODO: Add your control notification handler code here  
  184.     CEdit *pDesktop = (CEdit *)GetDlgItem(IDC_DESKTOP_FILE);  
  185.     CEdit *pCEDevice = (CEdit *)GetDlgItem(IDC_CE_FILE);  
  186.     CString csDesktopFile;  
  187.     CString csCEFile;  
  188.       
  189.     pDesktop->GetWindowText(csDesktopFile);  
  190.     pCEDevice->GetWindowText(csCEFile);  
  191.   
  192.     if(csCEFile.GetLength() > 1 && csDesktopFile.GetLength() > 1)  
  193.     {  
  194.         CString strOut;  
  195.           
  196.         if(TRUE == rapi.CopyFiletoPC(csCEFile,csDesktopFile))  
  197.         {  
  198.             strOut.Format(_T("File %s was transferred to PC as %s:"),csCEFile,csDesktopFile);  
  199.             AfxMessageBox(strOut);  
  200.         }  
  201.         else  
  202.         {  
  203.             strOut.Format(_T("文件打开失败!"));  
  204.             AfxMessageBox(strOut);  
  205.         }  
  206.     }  
  207.     else  
  208.     {  
  209.         AfxMessageBox(_T("Problem with filename's.... \nTry Again!!!!"));  
  210.         pDesktop->SetWindowText(_T(""));  
  211.         pCEDevice->SetWindowText(_T(""));  
  212.         pDesktop->SetFocus();  
  213.     }  
  214. }  
  215.   
  216. void CRapiTestDlg::OnDestroy()   
  217. {  
  218.     CDialog::OnDestroy();  
  219.       
  220.     // TODO: Add your message handler code here  
  221.     if(TRUE == gbInitRAPI)  
  222.     {  
  223.         rapi.CeRapiUninit();  
  224.     }  
  225. }  
  226.   
  227. //DEL void CRapiTestDlg::OnButton1()   
  228. //DEL {  
  229. //DEL   // TODO: Add your control notification handler code here  
  230. //DEL   BOOL bRet = FALSE;  
  231. //DEL   
  232. //DEL   bRet = rapi.CeCopyFile(L"\\CELongPress.txt",L"\\CELongPress2.txt",FALSE);  
  233. //DEL   if(FALSE == bRet)  
  234. //DEL   {   //成功  
  235. //DEL       TRACE("==CeCopyFile failure 1!\n");  
  236. //DEL   }  
  237. //DEL     
  238. //DEL   bRet = rapi.CeCopyFile(L"F:\\CELongPress.txt",L"\\CELongPress3.txt",FALSE);  
  239. //DEL   if(FALSE == bRet)  
  240. //DEL   {   //失败:3 - 系统找不到指定的路径。  
  241. //DEL       TRACE("==CeCopyFile failure 2!\n");  
  242. //DEL   }  
  243. //DEL     
  244. //DEL   bRet = rapi.CeCopyFile(L"\\CELongPress.txt",L"F:\\CELongPress2.txt",FALSE);  
  245. //DEL   if(FALSE == bRet)  
  246. //DEL   {   //失败:3 - 系统找不到指定的路径。  
  247. //DEL       TRACE("==CeCopyFile failure 3!\n");  
  248. //DEL   }  
  249. //DEL }  

最后来一点函数的说明吧
1.初始化RAPI
调用 RAPI 函数之前,你必须调用函数 CeRapiInit 或 CeRapiInitEx 来初始化 RAPI 库。
这两个函数的区别在于 CeRapiInit 会阻塞(等待与 Windows CE 设备连接成功),而 CeRapiInitEx 不会等待连接成功。
两个函数的原形如下:
HRESULT CeRapiIni(void);
HRESULT CeRapiInitEx(RAPIINIT *pRapiInit);


typedef struct _RAPIINIT 
{
    DWORD cbSize;
    HANDLE heRapiInit;
    STDAPI hrRapiInit;
} RAPIINIT;
在调用 CeRapiInitEx 之前,必须填写 cbSize 字段。


2.结束 RAPI 会话
结束所有必要的 RAPI 调用时,你应该调用下面这个函数来进行清除。
函数的原形如下:
HRESULT CeRapiUninit(void);
这个函数很好地关闭与远程设备地 RAPI 通信。如果 RAPI 会话没有被初始化 CeRapiUninit 将返回 E_FAIL。


3.预定义的 RAPI 函数
RAPI 服务包括很多预定义的 RAPI 函数,这些在连接时,PC 端复制 Windows CE 函数。
如GetStoreInformation 将把对象存储哭得大小和空间返回到 Windows CE 程序,CeGetStoreInformation 将把关于连接的 Windows CE 设备的相同信息返回到基于 PC 的应用程序。


3.1 RAPI 系统信息函数
大多数函数都是 Windows CE 函数的副本,除了 CeGetPassWord 和 CeRapiInvoke 系统信息函数。


函数的原形如下:
STDAPI_(VOID) CeGetSystemInfo(LPSYSTEM_INFO lpSystemInfo);
功能: 返回当前系统信息


STDAPI_(INT) CeGetSystemMetrics(INT nIndex);
功能: 获取Windows元素的尺寸和系统设置


STDAPI_(BOOL) CeGetVersionEx(LPCEOSVERSIONINFO lpVersionInformation);
功能: 获取当前运行的操作系统版本的扩展信息


STDAPI_(BOOL) CeGetSystemPowerStatusEx(PSYSTEM_POWER_STATUS_EX pstatus,BOOL fUpdate);
功能: 获取电池状态


STDAPI_(void) CeGlobalMemoryStatus(LPMEMORYSTATUS lpmst);
功能: 获取系统物理内存和虚拟内存信息


STDAPI_(BOOL) CeGetStoreInformation(LPSTORE_INFORMATION lpsi);
功能: 获取存储器信息并填入STORE_INFORMATION结构




说明: 以下函数偶只列出函数名和功能,不再给出原型。有需要的请查 MSDN。
3.2 RAPI文件和目录管理函数
下面的列表显示的是RAPI文件管理函数,这个列表说明几乎所有可用于Windows CE应用程序的文件函数都可以用于基于PC的程序。


CeCopyFile
功能: 复制文件


CeCreateDirectory
功能: 创建目录


CeCreateFile
功能: 创建,打开文件、管道、通讯资源、磁盘设备或者控制台。返回一个句柄用来访问对象。


CeDeleteFile
功能: 删除文件


CeFindAllFiles
功能: 从指定的Windows CE目录中获取所有文件和目录的信息,并且复制到一个包含CE_FIND_DATA结构的数组中


CeFindFirstFile
功能: 在目录中查找匹配给定文件名的一个文件


CeFindClose
功能: 关闭指定的查找句柄,CeFindFirstFile和CeFindNextFile 函数用这个句柄查找文件


CeFindNextFile
功能: 从上一次访问的CeFindFirstFile继续查找文件


CeGetFileAttributes
功能: 返回指定文件或目录的属性


CeGetFileSize
功能: 获取指定文件的字节大小


CeGetFileTime
功能: 获取文件创建日期时间,最后访问日期时间和最后修改日期时间


CeMoveFile
功能: 移动(重命名)一个文件或者目录


CeReadFile
功能: 从文件指针处读取文件数据


CeWriteFile
功能: 从文件指针处写入文件数据


注:有一个新函数 CeFindAllFiles,它对 Windows CE 应用程序不可用。
0 0
原创粉丝点击