Application Crash with Error 5 when calling DeviceIOControl,...

Discussion in 'Windows 64bit' started by Kerem Gümrükcü, Apr 2, 2007.

  1. 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ü
     
    Kerem Gümrükcü, Apr 2, 2007
    #1
    1. Advertising

  2. "Kerem Gümrükcü" <> schrieb im Newsbeitrag
    news:...
    > 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ü
     
    Kerem Gümrükcü, Apr 2, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerard
    Replies:
    0
    Views:
    1,068
    Gerard
    Jan 2, 2005
  2. John
    Replies:
    0
    Views:
    602
  3. rishil
    Replies:
    1
    Views:
    465
    Brian A
    Jan 27, 2008
  4. megha
    Replies:
    0
    Views:
    1,187
    megha
    Apr 5, 2008
  5. Replies:
    4
    Views:
    25,829
Loading...

Share This Page