今天刚开始学习RAPI编程,先将今天学习的成果放出来,大家学习。
如果代码有问题,请提出,多谢!
在PC上使用VC6,好久没有用它了。一直在EVC4下编程,呵呵...
RAPI功能的实现,需要rapi.dll和rapi.h。在以下的实现中,Link的动态库是C:/WINDOWS/system32/rapi.dll。没有使用rapi.h头文件,只是将必要的结构定义复制到工程中。如RAPIINIT 的定义。对动态库中的函数,用GetProcAddress()来加载。
1. RAPI初始化
BOOL CVogueRAPI::InitRapi()
{
LPTSTR pDllPath = new TCHAR[MAX_PATH + 10];
GetSystemDirectory(pDllPath, MAX_PATH);
CString strDllPathName(pDllPath);
strDllPathName += "//Rapi.dll";
TRACE("%s/n",strDllPathName); //C:/WINDOWS/system32/rapi.dll
hInst = LoadLibrary(strDllPathName);
if(hInst)
{
//CeRapiInit = (FARPROC) GetProcAddress(hInst, "CeRapiInit");
CeRapiInitEx = (pfnFnucA) GetProcAddress(hInst, "CeRapiInitEx");
CeRapiUninit = (FARPROC) GetProcAddress(hInst, "CeRapiUninit");
CeCreateFile = (pfnFunc0)GetProcAddress(hInst, "CeCreateFile");
CeWriteFile = (pfnFunc1)GetProcAddress(hInst, "CeWriteFile");
CeCloseHandle = (pfnFunc2)GetProcAddress(hInst, "CeCloseHandle");
CeFindFirstFile = (pfnFunc3)GetProcAddress(hInst, "CeFindFirstFile");
CeGetFileSize = (pfnFunc4)GetProcAddress(hInst, "CeGetFileSize");
CeReadFile = (pfnFunc5)GetProcAddress(hInst, "CeReadFile");
CeFindNextFile = (pfnFunc6)GetProcAddress(hInst, "CeFindNextFile");
CeCreateDirectory = (pfnFunc7)GetProcAddress(hInst, "CeCreateDirectory");
CeCreateProcess = (pfnFunc8)GetProcAddress(hInst, "CeCreateProcess");
CeGetSystemInfo = (pfnFunc9)GetProcAddress(hInst, "CeGetSystemInfo");
return TRUE;
}
else
{
FreeLibrary(hInst);
return FALSE;
}
return FALSE;
}
2. 设备与PC连接
BOOL CVogueRAPI::RapiConnectDevice()
{
RAPIINIT struRapiInit; //CeRapiInitEx函数要求的入口参数: 定义在rapi.h中, 为了不include "rapi.h", 将其定义复制到工程中
DWORD dwWaitResult = 0; //等待初始化完成事件的变量
HRESULT hRapiResult = NULL; //CeRapiInitEx的返回HANDLE
if(m_bRapiInitFlag == FALSE) //全局的一个标志,如果初始化成功就不再重复
{
struRapiInit.cbSize = sizeof(RAPIINIT); //填满该结构体仅有的三个成员
struRapiInit.hrRapiInit = NULL; //明知是输出参数也顺手填一下, 以防万一
struRapiInit.heRapiInit = NULL;
hRapiResult = CeRapiInitEx(&struRapiInit); //关键点
dwWaitResult = WaitForSingleObject(struRapiInit.heRapiInit, 2000); //关键点
if(hRapiResult == S_OK && struRapiInit.hrRapiInit == S_OK &&
dwWaitResult != WAIT_TIMEOUT) //三个返回值都判断, 以保证正确性
{
m_bRapiInitFlag = TRUE;
return TRUE;
}
else
{
return FALSE;
}
}
else
{
m_bRapiInitFlag = TRUE;
return TRUE;
}
}
3. 从PC传输文件到设备
BOOL CVogueRAPI::CopyFiletoWinCE(CString csPCFilename, CString csCEFilename)
{
CFile PCFile;
HANDLE hCEFile;
char cTemp[BUFFER_SIZE];
DWORD nbytes = 0;
int iFileLen = 0;
int iFileNum = 0;
int i = 0;
PCFile.Open(csPCFilename, CFile::modeRead |CFile::typeBinary);
iFileLen = PCFile.GetLength();
iFileNum = iFileLen / BUFFER_SIZE;
BSTR bstr = csCEFilename.AllocSysString();
SysFreeString(bstr);
hCEFile = CeCreateFile(bstr, GENERIC_READ | GENERIC_WRITE, 0, NULL,
OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
if(INVALID_HANDLE_VALUE == hCEFile) //文件打开失败
{
return FALSE;
}
for(i = 0;i < iFileNum;i++)
{
memset(cTemp,0,sizeof(char) * BUFFER_SIZE);
if(PCFile.Read(cTemp, BUFFER_SIZE) >= 1)
{
CeWriteFile(hCEFile, cTemp, BUFFER_SIZE, &nbytes, NULL);
}
}
memset(cTemp,0,sizeof(char) * BUFFER_SIZE);
if(PCFile.Read(cTemp, iFileLen % BUFFER_SIZE) >= 1)
{
CeWriteFile(hCEFile, cTemp, iFileLen % BUFFER_SIZE, &nbytes, NULL);
}
CeCloseHandle(hCEFile);
PCFile.Close();
CeRapiUninit();
return TRUE;
}