Using DeviceIoControl

Hello

I have a Compact Flash Card attached to the PC via a USB SanDisk Card Reader.

I want to perform sector-level IO on this card using DeviceIoControl. Is this possible.

If it is not possible, then what are the alternatives.

Thanks
dranne

DRANNE:

From user mode (I assume), you should be able to achieve this effect,
assuming that you are not using Vista (I think vista just dumped support
for this after Bluepill). That is, if can query the drive geometry to
get the sector size, open an (exclusive) handle to the physical device,
and read or write from whatever offset you wish.

Something like this, replacing “\\.\PhysicalDrive0” with the name of
the physical device of the flash card:

HANDLE hDevice = CreateFile(“\\.\PhysicalDrive0”, // drive to open
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes

DISK_GEOMETRY diskInfo;
DWORD numberOfBytes = 0;

bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer
& diskInfo,
sizeof(diskInfo), // output buffer
& numberOfBytes, // # bytes
returned
(LPOVERLAPPED) NULL); // synchronous I/O

The offset to a given sector X is just X * diskInfo.BytesPerSector.

Hope this helps,

mm

>> xxxxx@hotmail.com 2007-03-13 01:10 >>>
Hello

I have a Compact Flash Card attached to the PC via a USB SanDisk Card
Reader.

I want to perform sector-level IO on this card using DeviceIoControl.
Is this possible.

If it is not possible, then what are the alternatives.

Thanks
dranne


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Thanks.
I have had used this code before and was able to retreive the Drive
Geometry.
But I am unable to do read and write operations on the card.

Some sample code for reading and writing directly on the sectors may help.

Thanks in advance.
dranne

Hello

I am using Windows XP and programming in the user mode.
ReadFile returns me 0 (i.e. no bytes are read) when I try to read sectors on the CF card.

I am new to low-level windows programming. However, I have developed device drivers and file system drivers for Microcontrollers.
Can anybody help.

My code is as follows, am I missing something:

HANDLE hDevice; // handle to the drive to be examined
BOOL bResult; // results flag
DWORD junk; // discard results
DISK_GEOMETRY pdg; // disk drive geometry structure

hDevice = CreateFile(TEXT(“\\.\H:”), // drive
0, // no access to the drive
FILE_SHARE_READ | // share mode
FILE_SHARE_WRITE,
NULL, // default security attributes
OPEN_EXISTING, // disposition
0, // file attributes
NULL); // do not copy file attributes

if (hDevice == INVALID_HANDLE_VALUE) // cannot open the drive
{
return (FALSE);
}

bResult = DeviceIoControl(hDevice, // device to be queried
IOCTL_DISK_GET_DRIVE_GEOMETRY, // operation to perform
NULL, 0, // no input buffer
pdg, sizeof(*pdg), // output buffer
&junk, // # bytes returned
(LPOVERLAPPED) NULL); // synchronous I/O

LPDWORD bytesRead = 0;
char result[512];
bResult = ReadFile(hDevice, result, 512, bytesRead, NULL);

Kindly help.

Regards
dranne

bytesRead need to point to a valid memory location. try this

DWORD bytesRead = 0;
char result[512];
bResult = ReadFile(hDevice, result, sizeof(result), &bytesRead, NULL);

d

The datatype of bytesRead is LPDWORD.
Your mentioned code doesn’t compile.
Can anyone help.

Thanks
dranne

Um, you need to brush up on your C syntax. The type of &bytesRead is DWORD*.

LPDWORD is declared as (in windef.h)
typedef DWORD far *LPDWORD;

far no longer has any meaning, so the type of &bytesRead is LPDWORD. The code should compile just fine, either there was a copying error on your part or something else is amiss.

d

Thanks a lot.
I have been able to compile it. It works fine.
Sorry for the last post.

Regards
dranne

> I want to perform sector-level IO on this card using DeviceIoControl. Is this

Open \.\PhysicalDrive%d and do Read/WriteFile on it.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

Would it still work for Vista?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Wednesday, March 28, 2007 11:02 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Using DeviceIoControl

I want to perform sector-level IO on this card using DeviceIoControl. Is this

Open \.\PhysicalDrive%d and do Read/WriteFile on it.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at http://www.osronline.com/page.cfm?name=ListServer

For instance, to read the MBR tables (including the extended PTs) - yes for
sure.

It will only fail on Vista if you will try to read/write within the
defined partitions
using such a handle, even if you will read/write using
SPTI.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com

“Anton A. Kolomyeytsev” wrote in message
news:xxxxx@ntdev…
Would it still work for Vista?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S. Shatskih
Sent: Wednesday, March 28, 2007 11:02 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Using DeviceIoControl

> I want to perform sector-level IO on this card using DeviceIoControl. Is this

Open \.\PhysicalDrive%d and do Read/WriteFile on it.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

It depends on what build of Vista. Read will, I believe, in all cases.
Write will not, at least as of the RTM version. I think a little blue
pill caused some change.

>> xxxxx@rocketdivision.com 2007-03-28 15:22 >>>
Would it still work for Vista?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S.
Shatskih
Sent: Wednesday, March 28, 2007 11:02 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Using DeviceIoControl

I want to perform sector-level IO on this card using DeviceIoControl.
Is this

Open \.\PhysicalDrive%d and do Read/WriteFile on it.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

We’re doing iSCSI target mapping the whole disk as iSCSI volume. In
pre-Vista
we just run under Admin account. Now we need to load legacy driver opening
volume for us.
What changed? How did it increase security?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Martin O’Brien
Sent: Thursday, March 29, 2007 6:58 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Using DeviceIoControl

It depends on what build of Vista. Read will, I believe, in all cases.
Write will not, at least as of the RTM version. I think a little blue
pill caused some change.

>> xxxxx@rocketdivision.com 2007-03-28 15:22 >>>
Would it still work for Vista?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S.
Shatskih
Sent: Wednesday, March 28, 2007 11:02 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Using DeviceIoControl

I want to perform sector-level IO on this card using DeviceIoControl.
Is this

Open \.\PhysicalDrive%d and do Read/WriteFile on it.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Search this list, this issue has been discussed here a lot.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-282280-
xxxxx@lists.osr.com] On Behalf Of Anton A. Kolomyeytsev
Sent: Friday, March 30, 2007 6:14 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Using DeviceIoControl

We’re doing iSCSI target mapping the whole disk as iSCSI volume. In
pre-Vista
we just run under Admin account. Now we need to load legacy driver
opening
volume for us.
What changed? How did it increase security?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Martin O’Brien
Sent: Thursday, March 29, 2007 6:58 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Using DeviceIoControl

It depends on what build of Vista. Read will, I believe, in all cases.
Write will not, at least as of the RTM version. I think a little blue
pill caused some change.

>>> xxxxx@rocketdivision.com 2007-03-28 15:22 >>>
Would it still work for Vista?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S.
Shatskih
Sent: Wednesday, March 28, 2007 11:02 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Using DeviceIoControl

> I want to perform sector-level IO on this card using DeviceIoControl.
Is this

Open \.\PhysicalDrive%d and do Read/WriteFile on it.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer

Thank you for pointing, but we already have a solution :slight_smile:

The question was rather theoretic - what was need of creating extra level of
privileges (above Admin).

A

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Mark Roddy
Sent: Saturday, March 31, 2007 1:46 AM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Using DeviceIoControl

Search this list, this issue has been discussed here a lot.

-----Original Message-----
From: xxxxx@lists.osr.com [mailto:bounce-282280-
xxxxx@lists.osr.com] On Behalf Of Anton A. Kolomyeytsev
Sent: Friday, March 30, 2007 6:14 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Using DeviceIoControl

We’re doing iSCSI target mapping the whole disk as iSCSI volume. In
pre-Vista
we just run under Admin account. Now we need to load legacy driver
opening
volume for us.
What changed? How did it increase security?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Martin O’Brien
Sent: Thursday, March 29, 2007 6:58 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] Using DeviceIoControl

It depends on what build of Vista. Read will, I believe, in all cases.
Write will not, at least as of the RTM version. I think a little blue
pill caused some change.

>>> xxxxx@rocketdivision.com 2007-03-28 15:22 >>>
Would it still work for Vista?

Regards,
Anton A. Kolomyeytsev

CEO, Rocket Division Software

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Maxim S.
Shatskih
Sent: Wednesday, March 28, 2007 11:02 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] Using DeviceIoControl

> I want to perform sector-level IO on this card using DeviceIoControl.
Is this

Open \.\PhysicalDrive%d and do Read/WriteFile on it.


Maxim Shatskih, Windows DDK MVP
StorageCraft Corporation
xxxxx@storagecraft.com
http://www.storagecraft.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

To unsubscribe, visit the List Server section of OSR Online at
http://www.osronline.com/page.cfm?name=ListServer