1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
|
[DllImport("coredll.dll", EntryPoint = "DeviceIoControl", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool DeviceIoControl(
IntPtr hDevice,
uint dwIoControlCode,
IntPtr lpInBuffer, // LPVOID lpInBuffer - any input data required for the IOCTL
int nInBufferSize,
byte[] lpOutBuffer,
int nOutBufferSize,
ref uint lpBytesReturned,//ref int lpBytesReturned,
IntPtr lpOverlapped);
[DllImport("coredll.dll")]
public static extern IntPtr FindFirstDevice(
[In] DeviceSearchType searchType,
[In] IntPtr searchParam,
[In, Out] ref DEVMGR_DEVICE_INFORMATION pdi);
[DllImport("coredll.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CreateFile(String lpFileName, UInt32 dwDesiredAccess, UInt32 dwShareMode,
IntPtr lpSecurityAttributes, UInt32 dwCreationDisposition, UInt32 dwFlagsAndAttributes,
IntPtr hTemplateFile);
[DllImport("coredll.dll")]
private static extern bool CloseHandle(IntPtr handle);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
public struct UFN_CLIENT_NAME
{
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)]
public string szName;
}
public IntPtr GetUfnController(string name)
{
IntPtr hUfn = IntPtr.Zero;
DeviceSearchType searchType =
DeviceSearchType.DeviceSearchByDeviceName;
string searchParamString = "";
if (searchParamString == "")
searchParamString = name;
IntPtr pguidBus = Marshal.AllocHGlobal(searchParamString.Length);
pguidBus = Marshal.StringToBSTR(searchParamString);
DEVMGR_DEVICE_INFORMATION pdi = new DEVMGR_DEVICE_INFORMATION();
pdi.dwSize = (uint)Marshal.SizeOf(
typeof(DEVMGR_DEVICE_INFORMATION));
IntPtr hf = FindFirstDevice(//DeviceSearchType.
searchType,
pguidBus,
ref pdi);
//AnalyseDevice(hf, pdi);
if ((int)hf != -1)
{
hUfn = CreateFile(pdi.szBusName,
0x80000000,//GENERIC_READ, // Open for reading
0,//FILE_SHARE_READ, // Share for reading
IntPtr.Zero,//NULL, // No security
3,//OPEN_EXISTING, // Existing file only
0,//FILE_ATTRIBUTE_NORMAL, // Normal file
IntPtr.Zero);//NULL); // No template file
CloseHandle(hf);
}
else
{
MessageBox.Show("FindFirstDevice failed ! : "+Marshal.GetLastWin32Error());
//hUfn = (System.IntPtr)INVALID_HANDLE_VALUE;
}
return hUfn;
}
public void SwitchToMassStorage(String Mode)
{
UFN_CLIENT_NAME ClientName = new UFN_CLIENT_NAME();
uint dwBytes = 0;
ClientName.szName = Mode;
IntPtr pClientInfo = Marshal.StringToBSTR(ClientName.szName);
bool ioctl_ret;
IntPtr getUSBfnHandle = GetUfnController("UFN*");
ioctl_ret = DeviceIoControl(getUSBfnHandle,
IOCTL_UFN_CHANGE_CURRENT_CLIENT,
pClientInfo,
256,
null, // empty lpOutBuffer
0, // no nOutBufferSize
ref dwBytes,
IntPtr.Zero);
if (ioctl_ret)
{
MessageBox.Show("DeviceIoControl success !\n");
}
else
{
MessageBox.Show("DeviceIoControl failed !\n Error: " +
Marshal.GetLastWin32Error());
}
} |
Partager