• RAPI编程之一


    今天刚开始学习RAPI编程,先将今天学习的成果放出来,大家学习。

    如果代码有问题,请提出,多谢!

    在PC上使用VC6,好久没有用它了。一直在EVC4下编程,呵呵...

    RAPI功能的实现,需要rapi.dll和rapi.h。在以下的实现中,Link的动态库是: C:/WINDOWS/system32/rapi.dll。没有使用rapi.h头文件,只是将必要的结构定义复制到工程中。如RAPIINIT 的定义。对动态库中的函数,用GetProcAddress()来加载。

    1. RAPI初始化

     1 BOOL CVogueRAPI::InitRapi()
     2 {
     3  LPTSTR pDllPath = new TCHAR[MAX_PATH + 10];
     4  GetSystemDirectory(pDllPath, MAX_PATH);
     5  CString strDllPathName(pDllPath);
     6  strDllPathName += "//Rapi.dll";
     7 TRACE("%s/n",strDllPathName); //C:/WINDOWS/system32/rapi.dll
     8  hInst = LoadLibrary(strDllPathName);
     9  if(hInst)
    10  {
    11   //CeRapiInit = (FARPROC) GetProcAddress(hInst, "CeRapiInit");
    12   CeRapiInitEx = (pfnFnucA) GetProcAddress(hInst, "CeRapiInitEx");
    13   CeRapiUninit = (FARPROC) GetProcAddress(hInst, "CeRapiUninit");
    14   CeCreateFile = (pfnFunc0)GetProcAddress(hInst, "CeCreateFile");
    15   CeWriteFile = (pfnFunc1)GetProcAddress(hInst, "CeWriteFile");
    16   CeCloseHandle = (pfnFunc2)GetProcAddress(hInst, "CeCloseHandle");
    17   CeFindFirstFile = (pfnFunc3)GetProcAddress(hInst, "CeFindFirstFile");
    18   CeGetFileSize = (pfnFunc4)GetProcAddress(hInst, "CeGetFileSize");
    19   CeReadFile = (pfnFunc5)GetProcAddress(hInst, "CeReadFile");
    20   CeFindNextFile = (pfnFunc6)GetProcAddress(hInst, "CeFindNextFile");
    21   CeCreateDirectory = (pfnFunc7)GetProcAddress(hInst, "CeCreateDirectory");
    22   CeCreateProcess = (pfnFunc8)GetProcAddress(hInst, "CeCreateProcess");
    23   CeGetSystemInfo = (pfnFunc9)GetProcAddress(hInst, "CeGetSystemInfo");
    24   return TRUE;
    25  }
    26  else
    27  {
    28   FreeLibrary(hInst);
    29   return FALSE;
    30  }
    31  return FALSE;
    32 }

    2.  设备与PC连接

     1 BOOL CVogueRAPI::RapiConnectDevice()
     2 {
     3     RAPIINIT struRapiInit;  //CeRapiInitEx函数要求的入口参数: 定义在rapi.h中, 为了不include "rapi.h", 将其定义复制到工程中
     4     DWORD dwWaitResult = 0;  //等待初始化完成事件的变量
     5     HRESULT hRapiResult = NULL; //CeRapiInitEx的返回HANDLE
     6  
     7     if(m_bRapiInitFlag == FALSE) //全局的一个标志,如果初始化成功就不再重复
     8     {
     9         struRapiInit.cbSize = sizeof(RAPIINIT);  //填满该结构体仅有的三个成员
    10         struRapiInit.hrRapiInit = NULL;    //明知是输出参数也顺手填一下, 以防万一
    11         struRapiInit.heRapiInit = NULL;
    12   
    13         hRapiResult = CeRapiInitEx(&struRapiInit);  //关键点
    14   dwWaitResult = WaitForSingleObject(struRapiInit.heRapiInit, 2000);  //关键点
    15   
    16         if(hRapiResult == S_OK && struRapiInit.hrRapiInit == S_OK &&
    17             dwWaitResult != WAIT_TIMEOUT)    //三个返回值都判断, 以保证正确性
    18         {
    19    m_bRapiInitFlag = TRUE;
    20    return TRUE;
    21         }
    22         else
    23         {
    24             return FALSE;
    25         }
    26     }
    27     else
    28     {
    29   m_bRapiInitFlag = TRUE;
    30   return TRUE;
    31     }
    32 }

    3. 从PC传输文件到设备

     1 BOOL CVogueRAPI::CopyFiletoWinCE(CString csPCFilename, CString csCEFilename)
     2 {
     3  CFile PCFile;
     4  HANDLE hCEFile;
     5  char cTemp[BUFFER_SIZE];
     6  DWORD nbytes = 0;
     7  int iFileLen = 0;
     8  int iFileNum = 0;
     9  int i = 0;
    10  PCFile.Open(csPCFilename, CFile::modeRead |CFile::typeBinary);
    11  iFileLen = PCFile.GetLength(); 
    12  iFileNum = iFileLen / BUFFER_SIZE;
    13  BSTR bstr = csCEFilename.AllocSysString(); 
    14  SysFreeString(bstr);
    15  hCEFile = CeCreateFile(bstr, GENERIC_READ | GENERIC_WRITE, 0, NULL,
    16       OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
    17  if(INVALID_HANDLE_VALUE == hCEFile) //文件打开失败
    18  {
    19   return FALSE;
    20  }
    21  
    22  for(i = 0;i < iFileNum;i++)
    23  {
    24   memset(cTemp,0,sizeof(char) * BUFFER_SIZE);
    25   if(PCFile.Read(cTemp, BUFFER_SIZE) >= 1)
    26   {
    27    CeWriteFile(hCEFile, cTemp, BUFFER_SIZE, &nbytes, NULL);
    28   }
    29  }
    30  memset(cTemp,0,sizeof(char) * BUFFER_SIZE);
    31  if(PCFile.Read(cTemp, iFileLen % BUFFER_SIZE) >= 1)
    32  {
    33   CeWriteFile(hCEFile, cTemp, iFileLen % BUFFER_SIZE, &nbytes, NULL);
    34  }
    35  CeCloseHandle(hCEFile);
    36  PCFile.Close();
    37  CeRapiUninit();
    38  return TRUE;
    39 }
  • 相关阅读:
    ES6-01 2018-02-06
    8.1 IO类
    2.4 const限定符
    C++命名空间
    win7系统docker安装ubuntu
    win7安装docker
    wuzhicms 查看模板中的所有可用变量和值
    wuzhicms上传弹出层,如何返回数据到当前页面?
    wuzhicms 无规律推荐位标签的嵌套使用
    【wuzhicms】apache 设置禁止访问某些文件或目录
  • 原文地址:https://www.cnblogs.com/91program/p/5249362.html
Copyright © 2020-2023  润新知