WinCE设置USB模式立即生效的办法
作者:互联网
#include <usbfnioctl.h>
#define PSZ_REG_SERIAL_CLASS_CLIENT_DRIVER _T("Serial_Class")
#define PSZ_REG_MASS_STORAGE_CLASS_CLIENT_DRIVER _T("Mass_Storage_Class")
/**************************************************************************
* function : get USB mode : activesync or mass storage
* parameters : N/A
* returns : TRUE : mass storage , FALSE : activesync
**************************************************************************/
BOOL GetUSBMode()
{
BOOL bRet = FALSE;
HANDLE hHandle = CreateFile(L"UFN1:",FILE_WRITE_ATTRIBUTES|GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hHandle != INVALID_HANDLE_VALUE)
{
DWORD dwBytes=0;
UFN_CLIENT_INFO ufnCInfo;
DeviceIoControl(hHandle, IOCTL_UFN_GET_CURRENT_CLIENT, NULL, 0, &ufnCInfo, sizeof(UFN_CLIENT_INFO), &dwBytes, 0);
CloseHandle(hHandle);
if(_tcsncmp(ufnCInfo.szName, PSZ_REG_SERIAL_CLASS_CLIENT_DRIVER, _tcslen(PSZ_REG_SERIAL_CLASS_CLIENT_DRIVER)))
{
bRet = TRUE;
}
}
return FALSE;
}
/**************************************************************************
* function : set USB mode : activesync or mass storage
* parameters : TRUE : mass storage , FALSE : activesync
* returns : success : TRUE , failed : FALSE
**************************************************************************/
BOOL SetUSBMode(BOOL bIsUsbMode)
{
BOOL bRet = FALSE;
HANDLE hHandle = CreateFile(L"UFN1:",FILE_WRITE_ATTRIBUTES|GENERIC_READ|GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,NULL);
if(hHandle != INVALID_HANDLE_VALUE)
{
UFN_CLIENT_NAME ufnClientName;
if(bIsUsbMode)
{
_tcsncpy(ufnClientName.szName,PSZ_REG_MASS_STORAGE_CLASS_CLIENT_DRIVER,_tcslen(PSZ_REG_MASS_STORAGE_CLASS_CLIENT_DRIVER));
ufnClientName.szName[_tcslen(PSZ_REG_MASS_STORAGE_CLASS_CLIENT_DRIVER)] = L'\0';
}
else
{
_tcsncpy(ufnClientName.szName,PSZ_REG_SERIAL_CLASS_CLIENT_DRIVER,_tcslen(PSZ_REG_SERIAL_CLASS_CLIENT_DRIVER));
ufnClientName.szName[_tcslen(PSZ_REG_SERIAL_CLASS_CLIENT_DRIVER)] = L'\0';
}
DWORD dwBytes;
bRet = DeviceIoControl(hHandle, IOCTL_UFN_CHANGE_CURRENT_CLIENT, ufnClientName.szName,sizeof(UFN_CLIENT_NAME), NULL, 0, &dwBytes,0);
CloseHandle(hHandle);
return bRet;
}
return bRet;
}
要使重启后继续有效还需要修改以下注册表:
[HKEY_LOCAL_MACHINE\Drivers\USB\FunctionDrivers]
"DefaultClientDriver"="Serial_Class"
;改为Mass_Storage_Class为存储
标签:USB,WinCE,DRIVER,CLASS,PSZ,CLIENT,生效,hHandle,REG 来源: https://blog.51cto.com/u_15298588/3034250