PC端通过USB与wince通信

这两天应朋友要求,做一个上位机的小程序,能够控制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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值