I.You

Hi there.

I posted several times like this problem...

At last, I tested by using MSDN example code:

http://msdn2.microsoft.com/en-us/library/aa363147.aspx

I copied this and built in VS2005.

However, the same failure appeared.

...

hDevice = CreateFile("\\\\.\\PhysicalDrive0", // drive to open

...

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

...

The value of hDevice is -1, and then returned (by FALSE).

I just created new win32 console application project, and copied and pasted, then built it.

I did not set project properties at all.

Needing any project configuration

Please Help me.



Re: Visual C++ Language [Severe Problem] CreateFile fails in VS2005 (Help me!)

Marius Bancila

And what exactly is PhysicalDrive0




Re: Visual C++ Language [Severe Problem] CreateFile fails in VS2005 (Help me!)

TilakGopi

Hi,

The following code works fine on my machine.You can see o/p at the end of the code also.

If this same code doesn't work on ur machine, sorry frnd i can't help u.

#include "stdafx.h"

#include <windows.h>

#include <winioctl.h>

#include <stdio.h>

BOOL GetDriveGeometry(DISK_GEOMETRY *pdg)

{

HANDLE hDevice; // handle to the drive to be examined

BOOL bResult; // results flag

DWORD junk; // discard results

hDevice = CreateFile(_T("\\\\.\\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

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

CloseHandle(hDevice);

return (bResult);

}

int main(int argc, char *argv[])

{

DISK_GEOMETRY pdg; // disk drive geometry structure

BOOL bResult; // generic results flag

ULONGLONG DiskSize; // size of the drive, in bytes

bResult = GetDriveGeometry (&pdg);

if (bResult)

{

printf("Cylinders = %I64d\n", pdg.Cylinders);

printf("Tracks/cylinder = %ld\n", (ULONG) pdg.TracksPerCylinder);

printf("Sectors/track = %ld\n", (ULONG) pdg.SectorsPerTrack);

printf("Bytes/sector = %ld\n", (ULONG) pdg.BytesPerSector);

DiskSize = pdg.Cylinders.QuadPart * (ULONG)pdg.TracksPerCylinder *

(ULONG)pdg.SectorsPerTrack * (ULONG)pdg.BytesPerSector;

printf("Disk size = %I64d (Bytes) = %I64d (Gb)\n", DiskSize,

DiskSize / (1024 * 1024 * 1024));

}

else

{

printf ("GetDriveGeometry failed. Error %ld.\n", GetLastError ());

}

return ((int)bResult);

}

output:

----------

Cylinders = 9730
Tracks/cylinder = 255
Sectors/track = 63
Bytes/sector = 512
Disk size = 80031974400 (Bytes) = 74 (Gb)
Press any key to continue . . .

Thanx & Regards,

Ch.T.Gopi Kumar.






Re: Visual C++ Language [Severe Problem] CreateFile fails in VS2005 (Help me!)

I.You

Thanks Marius and TilakGopi

> And what exactly is PhysicalDrive0
Hard disk0/Partition1

I want to access to raw disk directly and to read some data witten to sector.

> If this same code doesn't work on ur machine, sorry frnd i can't help u.

How did you build it i.e. what is your build environment e.g. on win32 console appl project in VS2005, etc

Thank you.





Re: Visual C++ Language [Severe Problem] CreateFile fails in VS2005 (Help me!)

TilakGopi

Ya,

It's a win32 console application project in VS2005.

Thanx & Regards,

Ch.T.Gopi Kumar






Re: Visual C++ Language [Severe Problem] CreateFile fails in VS2005 (Help me!)

I.You

Thanks a lot.

Helpful for me.

Regards.

I.You





Re: Visual C++ Language [Severe Problem] CreateFile fails in VS2005 (Help me!)

Vinod N N

Hi,

I am new to this forum. Ihave a doubt regarding disk space.

I want to check whether a blank CD is inserted into the CD drive. In the case of disks with data, we can use GetFreeSpace() or GetDiskFreeSpaceEx(). But in the case of blank CD, these functions behave in the way as if there is no media. I also tried with the above code that u had given. The result is the same.

This is my code portion:

DWORD dwBytesReturned = 0;
DISK_GEOMETRY DiskGeometry;

// Open device
HANDLE hDevice = NULL;
hDevice = CreateFile( _T( "\\\\.\\G:" ),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
NULL,
OPEN_EXISTING,
0,
NULL);
if(hDevice == INVALID_HANDLE_VALUE)
{
AfxMessageBox( "Invalid handle..." );
return;
}
// Error handling

CString csOutput = _T( "" );
// Query device
if(::DeviceIoControl(hDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY,
NULL,
0,
&DiskGeometry,
sizeof(DiskGeometry),
&dwBytesReturned,
NULL) == FALSE)
{
AfxMessageBox( "Error!!!" );// Control goes here for blank CD and no media
}
else
{
AfxMessageBox( "Success..." );// In case of media with data
csOutput.Format( "Cylinders = %lf", DiskGeometry.Cylinders );
AfxMessageBox( csOutput );
csOutput.Format( "Bytes/Sector = %d", DiskGeometry.BytesPerSector );
AfxMessageBox( csOutput );
csOutput.Format( "Sectors/Track = %d", DiskGeometry.SectorsPerTrack );
AfxMessageBox( csOutput );
csOutput.Format( "Tracks/Cylinder = %d", DiskGeometry.TracksPerCylinder );
AfxMessageBox( csOutput );
}

// Close device
BOOL bRet = CloseHandle(hDevice);

Only thing I want to know is, whether a blank CD is in the drive( Differentiate b/w blank media and no media ) .

Can anyone pls suggest a solution for this