这两天应朋友要求,做一个上位机的小程序,能够控制wince读写U盘.以前没做过wince的东东,于是网上搜索一下,发现了rapi.dll这个东西。
动态获取dll里面的函数。
hInst = LoadLibrary(sPath);
if (hInst)
{
CeRapiInit = (FARPROC) GetProcAddress(hInst, "CeRapiInit");
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");
}
else
{
FreeLibrary(hInst);
}
这是函数定义原型
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;
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);
pfnFunc0 CeCreateFile;
pfnFunc1 CeWriteFile;
pfnFunc2 CeCloseHandle;
pfnFunc3 CeFindFirstFile;
pfnFunc4 CeGetFileSize;
pfnFunc5 CeReadFile;
pfnFunc6 CeFindNextFile;
pfnFunc7 CeCreateDirectory;
pfnFunc8 CeCreateProcess;
pfnFunc9 CeGetSystemInfo;
FARPROC CeRapiUninit;
FARPROC CeRapiInit;
在msdn里可以找到这些函数的详细信息。
于是写了一个向USB写文件的一个函数
//传输数据到WINCE某一特定文件
BOOL CUsb::UsbWriteDataToCe(BYTE* data, ULONG len)
{
CString sCeFileName = "[url=file:test.txt]//test.txt[/url]";
BSTR bstr = sCeFileName.AllocSysString();
SysFreeString(bstr);
CeRapiInit();
HANDLE h;
DWORD nBytes;
h = CeCreateFile(bstr, GENERIC_WRITE, 0, NULL,
CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
while(len > 0)
{
if (len >= BUFFER_SIZE)
nBytes = BUFFER_SIZE;
else
nBytes = len;
CeWriteFile(h, data, nBytes, &nBytes, NULL);
len -= nBytes;
data += nBytes;
}
CeCloseHandle(h);
CeRapiUninit();
return 1;
}
//从wince某一特定文件读数
BOOL CUsb::UsbReadDataFromCe(BYTE* data, ULONG* nLen)
{
CString sCeFileName = "[url=file:test.txt]//test.txt[/url]";
BSTR bstr = sCeFileName.AllocSysString();
SysFreeString(bstr);
CeRapiInit();
HANDLE h;
DWORD nBytes;
DWORD len = 10000;
h = CeCreateFile(bstr, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
CeGetFileSize(h, &len);
len = 5;
if (len < *nLen)
*nLen = len;
len = *nLen;
while(len > 0)
{
if (len >= BUFFER_SIZE)
nBytes = BUFFER_SIZE;
else
nBytes = len;
CeReadFile(h, data, nBytes, &nBytes, NULL);
len -= nBytes;
data += nBytes;
}
CeCloseHandle(h);
CeRapiUninit();
return 1;
}
PC端通过USB与wince通信
最新推荐文章于 2021-05-27 06:04:34 发布