Hi,

i have some (usermode)code that must read the disk geometry for some
other functions. This is not a 64bit question but i hope someone
can help me. Whenever i call the code, i get a Application
crash and debugger say Error Code 5. But i dont know why this
happens. This Code is not the Final Code and this one looks error prone,
but i "should" work, but in fact it does not (psTargetString is a Pointer
to a CString Object and works fine, lpszPhysicalDrive is something
like "C:" and also works fine, handles are ok,...). The Crash comes
when calling the DeviceIOControl(...) not after, not before, right then:


HANDLE hStorageDevice = NULL;
TCHAR lpszPhysicalDrive[10];

ZeroMemory(lpszPhysicalDrive,sizeof(TCHAR)*10);
strcat(lpszPhysicalDrive,"\\\\.\\");
strcat(lpszPhysicalDrive,lpszDriveLetter);

DISK_GEOMETRY dg;
ZeroMemory(&dg,sizeof(dg));

psTargetString->Insert(psTargetString->GetLength(),"Drive Layout:\r\n");

hStorageDevice = CreateFile(lpszPhysicalDrive,0,FILE_SHARE_READ |
FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0, NULL);

if(hStorageDevice != INVALID_HANDLE_VALUE){


if(DeviceIoControl(hStorageDevice,
IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&dg,sizeof(dg),NULL,NULL) == FALSE){


this->AppendLastError(psTargetString);

CloseHandle(hStorageDevice);

}else{


TCHAR lpszDGInfo[MAX_PATH];
ZeroMemory(lpszDGInfo,sizeof(TCHAR)*MAX_PATH);

wsprintf(lpszDGInfo,
"Bytes per Sector: %ul\r\n"
"Cylinders: %ul\r\n"
"Media Type(See Info above!): %ul\r\n"
"Sectors per Track: %ul\r\n"
"Tracks per Cylinder: %ul\r\n",
dg.BytesPerSector,
dg.Cylinders,
dg.MediaType,
dg.SectorsPerTrack,
dg.TracksPerCylinder);



psTargetString->Insert(psTargetString->GetLength(),lpszDGInfo);
}


}else{

this->AppendLastError(psTargetString);
}


CloseHandle(hStorageDevice);

Do you have any idea? Application ensures to run in administrative
privilleges,
before it even starts. Thanks in advance,...


Best regards

Kerem Gümrükcü

Re: Application Crash with Error 5 when calling DeviceIOControl,... by Kerem

Kerem
Mon Apr 02 11:09:01 CDT 2007


"Kerem Gümrükcü" <kareem114@hotmail.com> schrieb im Newsbeitrag
news:O1gWUHTdHHA.4216@TK2MSFTNGP02.phx.gbl...
> Hi,
>
> i have some (usermode)code that must read the disk geometry for some
> other functions. This is not a 64bit question but i hope someone
> can help me. Whenever i call the code, i get a Application
> crash and debugger say Error Code 5. But i dont know why this
> happens. This Code is not the Final Code and this one looks error prone,
> but i "should" work, but in fact it does not (psTargetString is a Pointer
> to a CString Object and works fine, lpszPhysicalDrive is something
> like "C:" and also works fine, handles are ok,...). The Crash comes
> when calling the DeviceIOControl(...) not after, not before, right then:
>
>
> HANDLE hStorageDevice = NULL;
> TCHAR lpszPhysicalDrive[10];
>
> ZeroMemory(lpszPhysicalDrive,sizeof(TCHAR)*10);
> strcat(lpszPhysicalDrive,"\\\\.\\");
> strcat(lpszPhysicalDrive,lpszDriveLetter);
>
> DISK_GEOMETRY dg;
> ZeroMemory(&dg,sizeof(dg));
>
> psTargetString->Insert(psTargetString->GetLength(),"Drive Layout:\r\n");
>
> hStorageDevice = CreateFile(lpszPhysicalDrive,0,FILE_SHARE_READ |
> FILE_SHARE_WRITE,NULL,OPEN_EXISTING,0, NULL);
>
> if(hStorageDevice != INVALID_HANDLE_VALUE){
>
>
> if(DeviceIoControl(hStorageDevice,
> IOCTL_DISK_GET_DRIVE_GEOMETRY,NULL,0,&dg,sizeof(dg),NULL,NULL) ==
> FALSE){
>
>
> this->AppendLastError(psTargetString);
>
> CloseHandle(hStorageDevice);
>
> }else{
>
>
> TCHAR lpszDGInfo[MAX_PATH];
> ZeroMemory(lpszDGInfo,sizeof(TCHAR)*MAX_PATH);
>
> wsprintf(lpszDGInfo,
> "Bytes per Sector: %ul\r\n"
> "Cylinders: %ul\r\n"
> "Media Type(See Info above!): %ul\r\n"
> "Sectors per Track: %ul\r\n"
> "Tracks per Cylinder: %ul\r\n",
> dg.BytesPerSector,
> dg.Cylinders,
> dg.MediaType,
> dg.SectorsPerTrack,
> dg.TracksPerCylinder);
>
>
>
> psTargetString->Insert(psTargetString->GetLength(),lpszDGInfo);
> }
>
>
> }else{
>
> this->AppendLastError(psTargetString);
> }
>
>
> CloseHandle(hStorageDevice);
>
> Do you have any idea? Application ensures to run in administrative
> privilleges,
> before it even starts. Thanks in advance,...
>
>
> Best regards
>
> Kerem Gümrükcü
>


Hi,

ok i found it, it was the 6th Parameter for the DeviceIOControl, it had to
be a pointer to a DWORD, but i used a NULL, and of course this raised
an ACCESS_DENIED,....

Best regards

Kerem Gümrükcü