當前位置:首頁 > 芯聞號 > 充電吧
[導讀]先來 RAPI 概述Windows CE 有了 RAPI 以后,PC 應用程序就能遠程管理 Windows CE 設備。導出函數將處理文件系統(tǒng)、注冊表和數據庫以及用于查詢系統(tǒng)配置的函數。大多數 RAP

先來 RAPI 概述
Windows CE 有了 RAPI 以后,PC 應用程序就能遠程管理 Windows CE 設備。
導出函數將處理文件系統(tǒng)、注冊表和數據庫以及用于查詢系統(tǒng)配置的函數。
大多數 RAPI 是 Windows CE API 函數中的副本,只有少數的函數擴展了 API。

RAPI 函數被列在 Windows CE API 參考中,但是將有 PC 應用程序來調用而不是由 Widows CE 應用程序調用。
在函數的名稱都有前綴 Ce 來與他們的 Window CE 的副本進行區(qū)分;
例如:Windows CE 中的函數 GetStoreInformation 將在該函數的 RAPI 版本中稱為 CeGetStoreInformation。

在 Windows CE 應用程序將不會調用 RAPI 函數。

知道大家不想看文字,直接上示例(功能:復制文件到設備上)代碼:
RAPI 被封裝為一個類,先看頭文件。
CeRAPI.h?

//?CeRAPI.h:?interface?for?the?CeRAPI?class.
//
//////////////////////////////////////////////////////////////////////

#if?!defined(AFX_VOGUERAPI_H__10D13979_C6F2_4922_A4D7_B105644F969F__INCLUDED_)
#define?AFX_VOGUERAPI_H__10D13979_C6F2_4922_A4D7_B105644F969F__INCLUDED_

#if?_MSC_VER?>?1000
#pragma?once
#endif?//?_MSC_VER?>?1000

#define?BUFFER_SIZE?10240

typedef?struct?_RAPIINIT
{?
	DWORD?cbSize;?
	HANDLE?heRapiInit;?
	HRESULT?hrRapiInit;?
}
RAPIINIT;

typedef?struct?_CE_FIND_DATA?{
????DWORD????dwFileAttributes;
????FILETIME?ftCreationTime;
????FILETIME?ftLastAccessTime;
????FILETIME?ftLastWriteTime;
????DWORD????nFileSizeHigh;
????DWORD????nFileSizeLow;
????DWORD????dwOID;
????WCHAR????cFileName[MAX_PATH];
}?CE_FIND_DATA,?*LPCE_FIND_DATA;

class?CCeRAPI??
{
public:
	HINSTANCE?hInst;

	typedef?HANDLE?(FAR?PASCAL?*?pfnFunc0)(LPCWSTR,?DWORD,?DWORD,?LPSECURITY_ATTRIBUTES,?DWORD,?DWORD,?HANDLE);
	typedef?BOOL???(FAR?PASCAL?*?pfnFunc1)(HANDLE,?LPCVOID,?DWORD,?LPDWORD,?LPOVERLAPPED);
	typedef?BOOL???(FAR?PASCAL?*?pfnFunc2)(HANDLE);	
	typedef?HANDLE?(FAR?PASCAL?*?pfnFunc3)(LPCWSTR,?LPCE_FIND_DATA);
	typedef?DWORD??(FAR?PASCAL?*?pfnFunc4)(HANDLE,?LPDWORD);
	typedef?BOOL???(FAR?PASCAL?*?pfnFunc5)(HANDLE,?LPVOID,?DWORD,?LPDWORD,?LPOVERLAPPED);
	typedef?BOOL???(FAR?PASCAL?*?pfnFunc6)(HANDLE,?LPCE_FIND_DATA);
	typedef?BOOL???(FAR?PASCAL?*?pfnFunc7)(LPCWSTR,?LPSECURITY_ATTRIBUTES);
	typedef?BOOL???(FAR?PASCAL?*?pfnFunc8)(LPCWSTR,?LPCWSTR,?LPSECURITY_ATTRIBUTES,?LPSECURITY_ATTRIBUTES,?BOOL,?DWORD,?LPVOID,?LPWSTR,?LPSTARTUPINFO,?LPPROCESS_INFORMATION);?
	typedef?VOID???(FAR?PASCAL?*?pfnFunc9)(LPSYSTEM_INFO);
	typedef?HRESULT?(FAR?PASCAL?*pfnFnucA)(RAPIINIT*);
	typedef?BOOL?(FAR?PASCAL?*pfnFuncB)(LPCWSTR,LPCWSTR,BOOL);
	typedef?DWORD?(FAR?PASCAL?*pfnFuncC)();
	typedef?HRESULT?(FAR?PASCAL?*pfnFuncD)();

	pfnFunc0?CeCreateFile;
	pfnFunc1?CeWriteFile;
	pfnFunc2?CeCloseHandle;
	pfnFunc3?CeFindFirstFile;
	pfnFunc4?CeGetFileSize;
	pfnFunc5?CeReadFile;
	pfnFunc6?CeFindNextFile;
	pfnFunc7?CeCreateDirectory;
	pfnFunc8?CeCreateProcess;?
	pfnFunc9?CeGetSystemInfo;
	FARPROC??CeRapiUninit;
	//FARPROC??CeRapiInit;
	pfnFuncB?CeCopyFile;
	pfnFuncC?CeGetLastError;
	pfnFuncD?CeRapiGetError;

	//Leo?Add
	pfnFnucA?CeRapiInitEx;

	BOOL?m_bRapiInitFlag;

	BOOL?RapiConnectDevice();

	BOOL?CopyFiletoWinCE(CString?strFileNamePC,?CString?strFileNamePPC);
	BOOL?CCeRAPI::CopyFiletoPC(CString?csCEFilename,CString?csPCFilename);
	BOOL?InitRapi();
//	CString?GetCStringFromFile(CString?strFileName);
	CCeRAPI();
	virtual?~CCeRAPI();

};

#endif?//?!defined(AFX_VOGUERAPI_H__10D13979_C6F2_4922_A4D7_B105644F969F__INCLUDED_)


類的實現文件:CeRAPI.cpp?

//?CeRAPI.cpp:?implementation?of?the?CeRAPI?class.
//
//////////////////////////////////////////////////////////////////////

#include?"stdafx.h"
#include?"RapiTest.h"
#include?"CeRAPI.h"

#ifdef?_DEBUG
#undef?THIS_FILE
static?char?THIS_FILE[]=__FILE__;
#define?new?DEBUG_NEW
#endif

//////////////////////////////////////////////////////////////////////
//?Construction/Destruction
//////////////////////////////////////////////////////////////////////

CCeRAPI::CCeRAPI()?
{
	m_bRapiInitFlag?=?FALSE;
}
CCeRAPI::~CCeRAPI()
{
}

BOOL?CCeRAPI::InitRapi()
{
	LPTSTR?pDllPath?=?new?TCHAR[MAX_PATH?+?10];
	GetSystemDirectory(pDllPath,?MAX_PATH);
	CString?strDllPathName(pDllPath);
	strDllPathName?+=?"\Rapi.dll";
TRACE("%sn",strDllPathName);	//C:WINDOWSsystem32rapi.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");
		CeCopyFile?=?(pfnFuncB)GetProcAddress(hInst,?"CeCopyFile");
		CeGetLastError?=?(pfnFuncC)GetProcAddress(hInst,?"CeGetLastError");
		CeRapiGetError?=?(pfnFuncD)GetProcAddress(hInst,?"CeRapiGetError");

		return?TRUE;
	}
	else
	{
		FreeLibrary(hInst);

		return?FALSE;
	}

	return?FALSE;
}

BOOL?CCeRAPI::CopyFiletoPC(CString?csCEFilename,CString?csPCFilename)
{
	HANDLE?hCEFile;
	DWORD?dwFileSize?=?0;
	int?iFileBlockSize?=?0;
	DWORD?dwBytes?=?0;
	char?cBlockSize[BUFFER_SIZE];
	int?i?=?0;
	DWORD?dwSizeRet?=?0?;

	CFile?PCFile;

	BSTR?bstr?=?csCEFilename.AllocSysString();??
	SysFreeString(bstr);

	hCEFile?=?CeCreateFile(bstr,?GENERIC_READ,?0,?NULL,
		????OPEN_EXISTING,?FILE_ATTRIBUTE_NORMAL,?NULL);
	if(INVALID_HANDLE_VALUE?==?hCEFile)
	{
		TRACE("==Fail?to?open?device?file:%dn",GetLastError());
		return?FALSE;
	}
	dwSizeRet?=?CeGetFileSize(hCEFile,&dwFileSize);
	TRACE("==Read?file?size?return?value:%dn",dwSizeRet);
	dwFileSize?=?dwSizeRet?+?(dwFileSize?<<?32);

	iFileBlockSize?=?dwFileSize?/?BUFFER_SIZE;

	PCFile.Open(csPCFilename,CFile::modeReadWrite?|?CFile::typeBinary?|?CFile::modeCreate);

	for(i?=?0;i?<?iFileBlockSize;i++)
	{
		memset(cBlockSize,0,sizeof(char)?*?BUFFER_SIZE);
		CeReadFile(hCEFile,cBlockSize,BUFFER_SIZE,&dwBytes,NULL);
		PCFile.Write(cBlockSize,BUFFER_SIZE);
	}
	if(0?!=?dwFileSize?%?BUFFER_SIZE)
	{
		memset(cBlockSize,0,sizeof(char)?*?BUFFER_SIZE);
		CeReadFile(hCEFile,cBlockSize,dwFileSize?%?BUFFER_SIZE,&dwBytes,NULL);
		PCFile.Write(cBlockSize,dwFileSize?%?BUFFER_SIZE);
	}

	CeCloseHandle(hCEFile);
	PCFile.Close();
	return?TRUE;
}
BOOL?CCeRAPI::CopyFiletoWinCE(CString?csPCFilename,?CString?csCEFilename)
{
	CFile?PCFile;
	HANDLE?hCEFile;
	char?cTemp[BUFFER_SIZE];
	DWORD?dwBytes?=?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,?&dwBytes,?NULL);
		}
	}
	if(0?!=?iFileLen?%?BUFFER_SIZE)
	{
		memset(cTemp,0,sizeof(char)?*?BUFFER_SIZE);
		if(PCFile.Read(cTemp,?iFileLen?%?BUFFER_SIZE)?>=?1)
		{
			CeWriteFile(hCEFile,?cTemp,?iFileLen?%?BUFFER_SIZE,?&dwBytes,?NULL);
		}
	}

	CeCloseHandle(hCEFile);
	PCFile.Close();

	return?TRUE;
}

//?CString?CVogueRAPI::GetCStringFromFile(CString?strFileName)
//?{
//?	CString?strOut;
//?	LPSTR?pText?=?NULL;
//?	int?iLen;
//?	CFile?f;
//?	if?(f.Open(strFileName,?CFile::modeReadWrite))
//?	{
//?		iLen?=?f.GetLength();
//?		pText?=?new?char[iLen?+?1];
//?		f.Read(pText,?iLen);
//?		pText[iLen]?=?'