deviceiocontrol

Freeze on SerialPort.Open / DeviceIoControl / GetcommState with usbser.sys

江枫思渺然 提交于 2019-12-19 04:03:10
问题 I have a C program which opens a handle to a COM port, writes some bytes to it, reads some bytes out, then closes the handle and exits. However, when I run the program like 10 times in a row, it starts to take very long to complete the GetCommState function and to get stuck in the SetCommState function. The same thing happens in C# with a simple SerialPort object. The only fix I could find is reconnecting the device to the port. Is there some more elegant way to get rid of this freeze? Is it

Physical disk size not correct (IoCtlDiskGetDriveGeometry)

℡╲_俬逩灬. 提交于 2019-12-17 19:11:15
问题 I use the code below to get the physical disk size , but the size returned is not correct. I've checked the size with other tools. The code below reports Total disk space: 8.249.955.840 bytes and it should be Total disk space: 8.254.390.272 bytes How can I retrieve the actual/correct physical disk size? Tested on USB drives and normal hard drives. The code is long, here separate it in parts to show. The structure: [StructLayout(LayoutKind.Sequential)] internal struct DiskGeometry { public

Identifying system reserved drive using DeviceIoControl function in C++

大城市里の小女人 提交于 2019-12-13 06:07:31
问题 I am trying to identify if a drive is system reserved drive( PhysicalDrive0 or C-Drive ) using DeviceIoControl function. However my code is always returning true for all the drives. HANDLE hDevice; // handle to the drive to be examined BOOL bResult; // results flag DWORD junk; // discard results PARTITION_INFORMATION_MBR *pdg hDevice = CreateFile(TEXT("\\\\.\\C:"), GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL); bResult = DeviceIoControl(

SetWindowsHookEx for DeviceIOControl, what hookid to be used?

时光怂恿深爱的人放手 提交于 2019-12-12 04:09:41
问题 HHOOK WINAPI SetWindowsHookEx( _In_ int idHook, _In_ HOOKPROC lpfn, _In_ HINSTANCE hMod, _In_ DWORD dwThreadId ); On MSDN listed available idHook values, there are: WH_CALLWNDPROC WH_CALLWNDPROCRET WH_CBT WH_DEBUG WH_FOREGROUNDIDLE WH_GETMESSAGE WH_JOURNALPLAYBACK WH_JOURNALRECORD WH_KEYBOARD WH_KEYBOARD_LL WH_MOUSE WH_MOUSE_LL WH_MSGFILTER WH_SHELL WH_SYSMSGFILTER So, what idHook should be used for hook DeviceIOControl function (for console application)? Or may i'd use some other hook method

Error partitioning and formatting USB flash drive in C++

对着背影说爱祢 提交于 2019-12-12 02:38:58
问题 I'm stuck attempting to re-partition and format a USB flash drive using C++, any help would be great! The goal is to re-partition any arbitrary flash drive with a single partition taking the entire space and formatted FAT32 (later options NTFS and EXFAT). This will be done in batch, hopefully with 50+ devices at once, so drive letter access is not an option. I'm able to create a partition, but when I try IOCTL_DISK_SET_PARTITION_INFO_EX to set the format type, it is failing with 0x32, ERROR

DeviceIoControl for SCSI INQUIRY command returns error 50

徘徊边缘 提交于 2019-12-11 18:25:54
问题 I am trying to access a USB scanner through the IOCTL commands. This is on Windows 7. I did not deal with IOCTL coding before, so I first tried the following snippet based on what I could find with a quick search. #include "stdafx.h" #include <stddef.h> #include <Windows.h> #include <ntddscsi.h> #include <usbscan.h> typedef struct { SCSI_PASS_THROUGH spt; BYTE sense[18]; BYTE data[36]; } SPTSD; LPTSTR ErrorMessage(DWORD error) { LPTSTR errorText = NULL; FormatMessage( FORMAT_MESSAGE_FROM

Why does DeviceIoControl fail with “Incorrect Function”

心已入冬 提交于 2019-12-11 05:39:43
问题 I am trying to communicate with my USB driver. I am able to get a handle, but once I use DeviceIoControl it fails, GetLastError() says error is an incorrect function. I am stumped on how to debug this. I am using XP 32bit machine. Handle = CREATEFILE( DevicePath1, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL); if (INVALID_HANDLE_VALUE == Handle) { printf("INVALIDHANDLE USB\n"); return PHNFCSTVAL(CID_NFC_DAL, NFCSTATUS_INVALID_DEVICE); } else {

Why does my C#/pinvoke DeviceIoControl call return 0 bytes read with garbage data?

China☆狼群 提交于 2019-12-10 10:04:00
问题 I have an unmanaged C++ Windows console app that works fine. I want it in C#. I have done DllImport statements for the necessary Kernel32.dll symbols: [StructLayout(LayoutKind.Sequential)] internal struct DiskGeometry { public long Cylinders; public int MediaType; public int TracksPerCylinder; public int SectorsPerTrack; public int BytesPerSector; } internal static class NativeMethods { internal const uint FileAccessGenericRead = 0x80000000; internal const uint FileShareWrite = 0x2; internal

Creating multiple partitions on USB using C#

血红的双手。 提交于 2019-12-06 06:09:53
问题 Iam trying to use DeviceIOControl to create multiple partiions in USB. It is always creating only one partition. Here is my source code [DllImport("kernel32.dll", SetLastError = true)] static extern IntPtr CreateFile(string lpFileName, uint dwDesiredAccess, uint dwShareMode, IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint dwFlagsAndAttributes, IntPtr hTemplateFile); [DllImport("kernel32")] static extern int CloseHandle(IntPtr handle); [DllImport("kernel32")] private static

Why does my C#/pinvoke DeviceIoControl call return 0 bytes read with garbage data?

六月ゝ 毕业季﹏ 提交于 2019-12-05 22:26:09
I have an unmanaged C++ Windows console app that works fine. I want it in C#. I have done DllImport statements for the necessary Kernel32.dll symbols: [StructLayout(LayoutKind.Sequential)] internal struct DiskGeometry { public long Cylinders; public int MediaType; public int TracksPerCylinder; public int SectorsPerTrack; public int BytesPerSector; } internal static class NativeMethods { internal const uint FileAccessGenericRead = 0x80000000; internal const uint FileShareWrite = 0x2; internal const uint FileShareRead = 0x1; internal const uint CreationDispositionOpenExisting = 0x3; internal