//--------------------------------------------------------------------------------------------------------------------------------------------------
最近发现有好多人在问WINCE下USB Serial和USB Mass Storage切换的问题;
既然做过,我就把程序给传上来,开源开源;
该程序是跑在WINCE6的板子上,其它地方我没有测试过;
不排版了;
// Udisk.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <windows.h>
#include <commctrl.h>
#include <usbfntypes.h>
#include <usbfn.h>
#include <svsutil.hxx>
#include <storemgr.h>
HANDLE GetUfncontroller()
{
HANDLE hUfn = NULL;
BYTE rgbGuidBuffer[sizeof(GUID) + 4];
LPGUID pguidBus = (LPGUID)rgbGuidBuffer;
LPCTSTR pszBusGuid = _T("E2BDC372-598F-4619-BC50-54B3F7848D35");
int iErr = _stscanf(pszBusGuid, SVSUTIL_GUID_FORMAT, SVSUTIL_PGUID_ELEMENTS(&pguidBus));
if (iErr == 0 || iErr == EOF)
return INVALID_HANDLE_VALUE;
DEVMGR_DEVICE_INFORMATION di;
memset(&di, 0, sizeof(di));
di.dwSize = sizeof(di);
HANDLE hf = FindFirstDevice(DeviceSearchByGuid, pguidBus, &di);
if (hf != INVALID_HANDLE_VALUE)
{
hUfn = CreateFile(di.szBusName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
CloseHandle(hf);
}
else
{
hUfn = INVALID_HANDLE_VALUE;
}
return hUfn;
}
int _tmain(int argc, _TCHAR* argv[])
{
HANDLE hDisk = NULL;
UFN_CLIENT_NAME uname;
UFN_CLIENT_INFO uinfo;
DWORD dwReturn = 0;
_tcscpy(uname.szName, L"Mass_Storage_Class");
hDisk = GetUfncontroller();
if (hDisk == INVALID_HANDLE_VALUE)
{
RETAILMSG(1, (TEXT("invalid usb handle, error = %d/r/n"), GetLastError()));
return 0;
}
if (DeviceIoControl(hDisk, IOCTL_UFN_GET_CURRENT_CLIENT, NULL, 0, &uinfo, sizeof(uinfo), &dwReturn, 0) && (dwReturn == sizeof(uinfo)))
{
if (_tcscmp(L"Mass_Storage_Class", uinfo.szName) == 0)
_tcscpy(uname.szName, L"Serial_Class");
}
if (!DeviceIoControl(hDisk, IOCTL_UFN_CHANGE_CURRENT_CLIENT, &uname, sizeof(uname), NULL, 0, NULL, NULL))
{
RETAILMSG(1, (TEXT("deviceiocontrol failed, error = %d/r/n"), GetLastError()));
return 0;
}
return 0;
}
本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/bearbrotherji/archive/2009/07/17/4357137.aspx