проблема с IOCTL_DISK_GET_DRIVE_LAYOUT

Тема в разделе "WASM.WIN32", создана пользователем Cneg, 18 окт 2007.

  1. Cneg

    Cneg New Member

    Публикаций:
    0
    Регистрация:
    28 мар 2007
    Сообщения:
    78
    Код (Text):
    1. #include <windows.h>
    2. #include <winioctl.h>
    3. #include <stdio.h>
    4. #include <iostream>
    5. //#include "ntdddisc.h"
    6. using namespace std;
    7.  
    8. void ErrorExit(LPTSTR lpszFunction)
    9. {
    10.     TCHAR szBuf[80];
    11.     LPVOID lpMsgBuf;
    12.     DWORD dw = GetLastError();
    13.  
    14.     FormatMessage(
    15.         FORMAT_MESSAGE_ALLOCATE_BUFFER |
    16.         FORMAT_MESSAGE_FROM_SYSTEM,
    17.         NULL,
    18.         dw,
    19.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    20.         (LPTSTR) &lpMsgBuf,
    21.         0, NULL );
    22.  
    23.     wsprintf(szBuf,
    24.         "%s failed with error %d: %s",
    25.         lpszFunction, dw, lpMsgBuf);
    26.  
    27.     MessageBox(NULL, szBuf, "Error", MB_OK);
    28.  
    29.     LocalFree(lpMsgBuf);
    30.     ExitProcess(dw);
    31. }
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35.   PARTITION_INFORMATION pinf;
    36.   DRIVE_LAYOUT_INFORMATION dli;
    37.   int size_dli = sizeof(dli)+(7*sizeof(pinf));
    38.   BOOL bResult, pResult;
    39.   HANDLE hDevice1, hDevice2;
    40.   DWORD junk2;
    41.   LPDWORD junk1;
    42.  
    43.   hDevice1 = CreateFile("\\\\.\\PHYSICALDRIVE0",
    44.                     GENERIC_READ,
    45.                     FILE_SHARE_READ | FILE_SHARE_WRITE,
    46.                     NULL,
    47.                     OPEN_EXISTING,
    48.                     0,
    49.                     NULL);
    50.  
    51.   if (hDevice1 == INVALID_HANDLE_VALUE)
    52.   {
    53.     return (FALSE);
    54.   }
    55.  
    56.   hDevice2 = CreateFile("\\\\.\\G:",
    57.                     GENERIC_READ,
    58.                     FILE_SHARE_READ | FILE_SHARE_WRITE,
    59.                     NULL,
    60.                     OPEN_EXISTING,
    61.                     0,
    62.                     NULL);
    63.  
    64.   if (hDevice2 == INVALID_HANDLE_VALUE)
    65.   {
    66.     return (FALSE);
    67.   }
    68.  
    69.   pResult = DeviceIoControl(hDevice1,
    70.                             IOCTL_DISK_GET_DRIVE_LAYOUT,
    71.                             NULL, 0,
    72.                             &dli, size_dli,
    73.                             (LPDWORD)&junk1,
    74.                             (LPOVERLAPPED) NULL);
    75.  
    76.   bResult = DeviceIoControl(hDevice2,
    77.                             IOCTL_DISK_GET_PARTITION_INFO,
    78.                             NULL, 0,
    79.                             &pinf, sizeof(pinf),
    80.                             &junk2,
    81.                             NULL);
    82.  
    83.   CloseHandle(hDevice1);
    84.   CloseHandle(hDevice2);
    85.  
    86. //  if (bResult)
    87. //  {
    88. //    printf("StartingOffset:\t\t%I64d\n", pinf.StartingOffset);
    89. //    printf("PartitionLength:\t%I64d\n", pinf.PartitionLength);
    90. //    cout << "HiddenSectors:\t\t" << (ULONG)pinf.HiddenSectors << endl;
    91. //    cout << "PartitionNumber:\t" << (ULONG)pinf.PartitionNumber << endl;
    92. //    cout << "PartitionType:\t\t" << (ULONG)pinf.PartitionType << endl;
    93. //    cout << "BootIndicator:\t\t" << (ULONG)pinf.BootIndicator << endl;
    94. //    cout << "RecognizedPartition:\t" << (ULONG)pinf.RecognizedPartition << endl;
    95. //    cout << "RewritePartition:\t" << (ULONG)pinf.RewritePartition << endl;
    96. //  }
    97. //  else
    98. //  {
    99. //  ErrorExit(0);
    100. //  }
    101.   if (pResult)
    102.   {
    103.       printf("Number of partiton = %ld\n", (ULONG) dli.PartitionCount);
    104. //    cout << "PartitionCount:\t" << (ULONG)dli.PartitionEntry[1].PartitionNumber << endl;
    105.   }
    106.     else
    107.     {
    108.         ErrorExit(0);
    109.     }
    110.  
    111.  
    112. //  return ((int)bResult);
    113. }
    Пытаюсь получить информацию о разделах (всего их у меня 7, включая сам жёсткий диск) используя IOCTL_DISK_GET_DRIVE_LAYOUT. Выдаётся ошибка 998: Неверная попытка доступа к адресу памяти... Не могу вкурить, моск растерян совершенно
     
  2. Cneg

    Cneg New Member

    Публикаций:
    0
    Регистрация:
    28 мар 2007
    Сообщения:
    78
    Код (Text):
    1. #include <windows.h>
    2. #include <winioctl.h>
    3. #include <stdio.h>
    4. #include <iostream>
    5. //#include "ntdddisc.h"
    6. using namespace std;
    7.  
    8. void ErrorExit(LPTSTR lpszFunction)
    9. {
    10.     TCHAR szBuf[80];
    11.     LPVOID lpMsgBuf;
    12.     DWORD dw = GetLastError();
    13.  
    14.     FormatMessage(
    15.         FORMAT_MESSAGE_ALLOCATE_BUFFER |
    16.         FORMAT_MESSAGE_FROM_SYSTEM,
    17.         NULL,
    18.         dw,
    19.         MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
    20.         (LPTSTR) &lpMsgBuf,
    21.         0, NULL );
    22.  
    23.     wsprintf(szBuf,
    24.         "%s failed with error %d: %s",
    25.         lpszFunction, dw, lpMsgBuf);
    26.  
    27.     MessageBox(NULL, szBuf, "Error", MB_OK);
    28.  
    29.     LocalFree(lpMsgBuf);
    30.     ExitProcess(dw);
    31. }
    32.  
    33. int main(int argc, char *argv[])
    34. {
    35.   PARTITION_INFORMATION pinf;
    36.   DRIVE_LAYOUT_INFORMATION dli;
    37.   int size_dli = sizeof(dli)+(7*sizeof(pinf));
    38.  
    39.   char * temp = new char[4096];
    40.  
    41.   BOOL bResult, pResult;
    42.   HANDLE hDevice1, hDevice2;
    43.   DWORD junk2;
    44.   LPDWORD junk1;
    45.  
    46.   hDevice1 = CreateFile("\\\\.\\PHYSICALDRIVE0",
    47.                     GENERIC_READ,
    48.                     FILE_SHARE_READ | FILE_SHARE_WRITE,
    49.                     NULL,
    50.                     OPEN_EXISTING,
    51.                     0,
    52.                     NULL);
    53.  
    54.   if (hDevice1 == INVALID_HANDLE_VALUE)
    55.   {
    56.     return (FALSE);
    57.   }
    58.  
    59.   hDevice2 = CreateFile("\\\\.\\G:",
    60.                     GENERIC_READ,
    61.                     FILE_SHARE_READ | FILE_SHARE_WRITE,
    62.                     NULL,
    63.                     OPEN_EXISTING,
    64.                     0,
    65.                     NULL);
    66.  
    67.   if (hDevice2 == INVALID_HANDLE_VALUE)
    68.   {
    69.     return (FALSE);
    70.   }
    71.  
    72.   pResult = DeviceIoControl(hDevice1,
    73.                             IOCTL_DISK_GET_DRIVE_LAYOUT,
    74.                             NULL, 0,
    75.                             (LPVOID) temp, (DWORD) 4096,
    76.                             (LPDWORD)&junk1,
    77.                             (LPOVERLAPPED) NULL);
    78.  
    79.   bResult = DeviceIoControl(hDevice2,
    80.                             IOCTL_DISK_GET_PARTITION_INFO,
    81.                             NULL, 0,
    82.                             &pinf, sizeof(pinf),
    83.                             &junk2,
    84.                             NULL);
    85.  
    86.   CloseHandle(hDevice1);
    87.   CloseHandle(hDevice2);
    88.  
    89. //  if (bResult)
    90. //  {
    91. //    printf("StartingOffset:\t\t%I64d\n", pinf.StartingOffset);
    92. //    printf("PartitionLength:\t%I64d\n", pinf.PartitionLength);
    93. //    cout << "HiddenSectors:\t\t" << (ULONG)pinf.HiddenSectors << endl;
    94. //    cout << "PartitionNumber:\t" << (ULONG)pinf.PartitionNumber << endl;
    95. //    cout << "PartitionType:\t\t" << (ULONG)pinf.PartitionType << endl;
    96. //    cout << "BootIndicator:\t\t" << (ULONG)pinf.BootIndicator << endl;
    97. //    cout << "RecognizedPartition:\t" << (ULONG)pinf.RecognizedPartition << endl;
    98. //    cout << "RewritePartition:\t" << (ULONG)pinf.RewritePartition << endl;
    99. //  }
    100. //  else
    101. //  {
    102. //  ErrorExit(0);
    103. //  }
    104.   if (pResult)
    105.   {
    106. //    DRIVE_LAYOUT_INFORMATION *ptr = (DRIVE_LAYOUT_INFORMATION *)dli;
    107.       cout<<"partcount"<<(DRIVE_LAYOUT_INFORMATION *)dli.PartitionCount;
    108. //    printf("Number of partiton = %ld\n", (ULONG) dli.PartitionCount);
    109. //    cout << "PartitionCount:\t" << (ULONG)dli.PartitionEntry[1].PartitionNumber << endl;
    110.   }
    111.     else
    112.     {
    113.         ErrorExit(0);
    114.     }
    115.  
    116.  
    117. //  return ((int)bResult);
    118. }
    Проблему с памятью вроде решил, теперь пишет:
    Run-Time Check Failure #3 - The variable 'dli' is being used without being defined
    Вроде dli определял.......?
     
  3. Cneg

    Cneg New Member

    Публикаций:
    0
    Регистрация:
    28 мар 2007
    Сообщения:
    78
    кажется проблема связана с тем что 1й раздел скрытый и windows его не монтирует... Видимо вместо CreateFile надо использовать что-то на уровне ядра, например NtOpen... Если пользовать CreateFile то программка не увидит разделов ext2/ext3 и иже с ними, неродными для Windows
     
  4. diamond

    diamond New Member

    Публикаций:
    0
    Регистрация:
    21 май 2004
    Сообщения:
    507
    Адрес:
    Russia
    Да ни при чём здесь скрытые разделы. DeviceIoControl записывает данные туда, куда ей сказали это делать, то есть в буфер temp. А переменная dli с буфером temp никак не связана. Правильный код был бы типа
    Код (Text):
    1. DRIVE_LAYOUT_INFORMATION* dlip;
    2. ...
    3. dlip = (DRIVE_LAYOUT_INFORMATION*)temp;
    4. cout << dlip->PartitionCount;