ªþ¿ý¤»¡@¹êÅéºÏºÐ»PÅÞ¿èºÏºÐ


²¤¶

²³©Ò¬Òª¾¡A¤@­Ó¹êÅéºÏºÐ¥i¥H¦A¥Ñ¤À³Îµ{¦¡³Ì¦h¤À³Î¦¨¤T­Ó¥D­n¤À³Î©M¤@­Ó©µ¦ù¤À³Î¡C±z¥i¥H¦b Windows XP §@·~¨t²Î¤¤¡A§â·Æ¹«´å¼Ð²¾¨ì¡u§Úªº¹q¸£¡v¤W¡A¦A«ö¤U·Æ¹«¥kÁä¡A¿ï¡uºÞ²z¡v¡AµM«á¦b¼u¥Xªºµøµ¡¥ª¥b³¡¡AÂI¿ï¡uºÏºÐºÞ²z¡v¡Aµøµ¡¥k¥b³¡´N·|¥X²{¤U¹Ïªºµe­±¡A¨ä¹ê¤U¹Ï¬O¤p¤ì°¸ªº®à¤W«¬¹q¸£ªºµwºÐ¸ê®Æ¡C

ŪªÌ¥i¥H¬Ý±o¥X¨Ó¡A¤p¤ì°¸¦@¦³¤T­Ó¹êÅéºÏºÐ ( physical disk)¡A¥H¬õ®Ø¼Ð¥X¡A¤À§O¬O¡GºÏºÐ 0¡BºÏºÐ 1¡BºÏºÐ 2¡CºÏºÐ 0 ¤S³Q¤À³Î¦¨¤T­Ó¥D­n¤À³Î»P¤@­Ó©µ¦ù¤À³Î¡C¥D­n¤À³Î¤W¥b³¡¥H²`ÂŦâªí¥Ü¡AºÏºÐ¼ÐÅÒ ( volumn ) ¤À§O¬O¡uMS-DOS_6_20¡v¡B¡uXP_PRO¡v¡B¡uXP64_PRO¡v¡C©µ¦ù¤À³Î¥Hºñ®Ø®Ø¦í¡A¤S³Q¤À³Î¬°¤T­ÓÅÞ¿èºÏºÐ¡A¨C­ÓÅÞ¿èºÏºÐ¤W¥b³¡¥HÂŦâªí¥Ü¡A¨ä¤¤¤@­ÓªººÏºÐ¼ÐÅÒ¬O¡uDATA_1¡v¡A¥t¥~¨â­Ó³Q Windows XP »{¬°¬O¡u¤£©úªººÏºÐ¤À³Î¡v¡A¦]¬°³o¨â­Ó¤À³Î¬O¡uLinux¡v§@·~¨t²Î©Ò¦û¾Ú¡C

¨C­Ó¤À³Î§¡¬O¬°¤FÀx¦s¸ê®Æ¡BÀɮצӹº¤À¡A¦Ó¨CºØ§@·~¨t²Î¦b¸ê®Æ¡BÀɮתº¦w±Æ¤è¦¡¦³©Ò¤£¦P¡A³oºØ¦bºÏºÐ¤¤¦w±Æ¦s©ñ¸ê®Æ©ÎÀɮתº¤è¦¡ºÙ¬°Àɮרt²Î ( file system )¡A¨Ò¦p DOS ¥H FAT12¡BFAT16 ¤è¦¡¦w±Æ¨Ã¦s¨úÀÉ®× ( ¦³Ãö FAT12¡BFAT16 ½Ð°Ñ¦Ò²Õ¦X»y¨¥ªº³nºÐ¤ù¡BµwºÐ(2)FAT )¡FWindows 9x/Me ¥i¥H¨Ï¥Î FAT12¡BFAT16 ©M FAT32 ¤TºØÀɮרt²Î¡FWindows NT/XP °£¤F¤W­±©Ò»¡ªº FAT12¡BFAT16¡BFAT32 ¤§¥~¡AÁÙ¥i¨Ï¥Î NTFS¡FLinux «h±`¨Ï¥Î EXT2¡BEXT3 Àɮרt²Î¡C

¦b Windows §@·~¨t²ÎùØ¡A¨C­Ó¥D­n¤À³Î©M©µ¦ù¤À³ÎùتºÅÞ¿èºÏºÐ³£·|³Q½á¤©¤@­ÓºÏºÐ¾÷¥N¸¹¡A¨Ò¦p¡G¡uC:¡v¡B¡uD:¡v¡K¡Kµ¥µ¥¡A·íµM¦pªG¬O Windows ¤£»{±oªº¤À³Î©Î¬O Windows ¤£»{±oªºÀɮרt²Î¡A´N¤£·|³Q½á¤©ºÏºÐ¾÷¥N¸¹¡C¹³¡uC:¡v¡B¡uD:¡v¡K¡Kµ¥³o¨ÇºÏºÐ¾÷¥N¸¹©Ò¥NªíªººÏºÐ¡A´N¥s°µ¡uÅÞ¿èºÏºÐ¡v( logical driver )¡C¦Ó¦b XP ¥H¤Î§ó«á´Áªº Windows §@·~¨t²ÎªºÅÞ¿èºÏºÐ¡A¤S¦³¥t¤@¼h¤£¦Pªº·N¸q¡A¨º´N¬O°ÊºAºÏºÐ¡CXP ¥i¥H§â¼Æ­Ó¤À³Î¦X¨ÖÅܦ¨¤@­ÓÅÞ¿èºÏºÐ¡A¦Ó¶È¥Î¤@­ÓºÏºÐ¾÷¥N¸¹ªí¥Ü¡C¨C­Ó¤À³Î§¡¬°³sÄòªººÏ°Ï¡A¦b MSDN ¤¤ºÙ¬°¡uextent¡v¡A¼Æ­Ó¡uextent¡v¥i¦X¨Ö¬°¤@­ÓÅÞ¿èºÏºÐ¡A³o¨Ç¡uextent¡v¥i¯à¤À¥¬¦b¤£¦P¹êÅéºÏºÐ¡A¤]¥i¥H¤£¬O³sÄòªººÏ°Ï ( ·íµM¦P¤@­Ó¡uextent¡vªººÏ°Ï¬O³sÄòªº )¡C

¥Ñ¤W­±ªº¤ÀªR¥iª¾¡A¤@­Ó¹êÅéºÏºÐ¥i¥H¦³¤@­Ó¥H¤WªºÅÞ¿èºÏºÐ¡A¦Ó¥»³¹©Ò­n°Q½×ªº¬O¦C¥X¹êÅéºÏºÐ©Ò¥]§tªºÅÞ¿èºÏºÐ¡C


­ì²z

¦b³o¤@³¹ùØ¡A¤p¤ì°¸±Nµo®i¥X¤@­Ó°Æµ{¦¡¡AGetLogicalDriveFromPhysicalDrive¡A¯à§â¹q¸£¤Wªº¨C¤@­Ó¹êÅéºÏºÐ©Ò¥]§tªºÅÞ¿èºÏºÐÅã¥Ü¥X¨Ó¡C

¦b Win32 API ùØ¡A¦³­Ó¥s°µ DeviceIoControl ªº API ¥i¥H¿é¤JÅÞ¿èºÏºÐ¥N¸¹¦Ó¶Ç¦^¦¹ÅÞ¿èºÏºÐ¬O¦ì©ó­þ­Ó¹êÅéºÏºÐ¤W¡F¦ý¬O¦ü¥G¬O¨S¦³¶Ç¤J¹êÅéºÏºÐ¡A¦Ó±o¨ì¸Ó¹êÅéºÏºÐ¥]§t­þ¨ÇÅÞ¿èºÏºÐ¡C¤p¤ì°¸ªº·Qªk«Ü²³æ¡A¨º´N¬O§Q¥Î Win32 ¥t¤@­Ó API¡AGetLogicalDriveStrings¡A§ä¥X©Ò¦³ªºÅÞ¿èºÏºÐ¾÷¥N¸¹¡A¦A§â³o¨ÇÅÞ¿èºÏºÐ¥N¸¹¤@¤@¥N¤J DeviceIoControl ùØ¡A´N¥i¥H±o¨ì¨C­ÓÅÞ¿èºÏºÐÄÝ©ó­þ¤@­Ó¹êÅéºÏºÐ¡A¦A©ó¤@¶ô°O¾ÐÅ餤¡A¨Ì¾Ú¹êÅéºÏºÐ½s¸¹¤Àªù§OÃþ§â¦¹ÅÞ¿èºÏºÐ¦s¤J¡C¥ý»¡¤@»¡ GetLogicalDriveFromPhysicalDrive ·|©I¥sªº Win32 API¡C

GetLogicalDriveStrings API

GetLogicalDriveStrings ¥i¥H§â¹q¸£¤Wªº©Ò¦³ÅÞ¿èºÏºÐ¡A¥]§t³nºÐ¾÷¡BµwºÐ¾÷¡B¥úºÐ¾÷¡BÀH¨­ºÐµ¥µ¥¡A¦s¤J¨ì«ü©wªº¦ì§}ùØ¡C¥¦ªº­ì«¬¬O¡G

        INVOKE  GetLogicalDriveStrings,nBufferLength,lpBuffer

¥ý»¡²Ä¤G­Ó°Ñ¼Æ¡AlpBuffer¡A¬O¤@­Ó¦ì§}«ü¼Ð¡A«ü¦V¤@­Ó½w½Ä°Ï°ì¡AGetLogicalDriveStrings ±N·|§â©Ò¦³ªºÅÞ¿èºÏºÐ¥N¸¹¶ñ¤J¦¹¦ì§}©Ò«üªº½w½Ä°ÏùØ¡C²Ä¤@­Ó°Ñ¼Æ¡AnBufferLength¡A¬O½w½Ä°Ïªº¤j¤p¡C³o¨â­Ó°Ñ¼Æ¥²¶·¦b©I¥s GetLogicalDriveStrings «e´N¤w³]©w¦n¡C¦pªG¦¨¥\ªº¨ú±oÅÞ¿èºÏºÐ¥N¸¹¡A«h EAX ¬°ÅÞ¿èºÏºÐ¥N¸¹ªºÁ`ªø«×¡A¦ý¤£¥]§t³Ì«áªº¦r¤¸¡ANULL¡F¦pªG©Ò nBufferLength ©Ò«ü©wªº½w½Ä°Ï¤£°÷¤j¡AEAX ·|¶Ç¦^©Ò»Ý½w½Ä°Ï¤j¤p¡A¦¹¤j¤p«o¤S¥]§t NULL¡C¦pªG GetLogicalDriveStrings µLªk¥¿±`¹B§@¡A«h EAX ¶Ç¦^ FALSE¡C

¨Ò¦p¤p¤ì°¸ªº¹q¸£¦b°õ¦æ§¹¡uINVOKE GetLogicalDriveStrings,104,12f76ch¡v«á¡A½w½Ä°Ï 12F76Ch ªº¤º®eÅܬ°¡G

0012F760                                      43 3A 5C 00              C:\.
0012F770  44 3A 5C 00 45 3A 5C 00 46 3A 5C 00 47 3A 5C 00  D:\.E:\.F:\.G:\.
0012F780  48 3A 5C 00 49 3A 5C 00 4A 3A 5C 00 4B 3A 5C 00  H:\.I:\.J:\.K:\.
0012F790  4D 3A 5C 00 4E 3A 5C 00 00                       M:\.N:\..

¦Óªð¦^­È EAX Åܬ° 2Ch¡C

GetDriveType API

³o­Ó API ¨M©w¬Y­ÓÅÞ¿èºÏºÐ¬OÄÝ©ó­þ¤@ºØÃþ«¬ªºÀx¦s¸Ë¸m¡Gremovable, fixed, CD-ROM, RAM disk, or network drive¡C¨ä­ì«¬¬°¡G

        INVOKE  GetDriveType,lpRootPathName

lpRootPathName ¬O«ü¦VÅÞ¿èºÏºÐ®Ú¥Ø¿ý¦r¦êªº¦ì§}¡A¥H NULL µ²§À¡A¨Ò¦p¡u"C:\",0¡v¡C¤Ï±×½u¡u\¡v¬O¥²­nªº¡C¦b©I¥s GetDriveType ¤§«e¡A¥²¶·¥ý³]©w¦n lpRootPathName¡A¦pªG lpRootPathName ¬° 0¡A¨º»ò GetDriveType ·|¶Ç¦^¥Ø«eÅÞ¿èºÏºÐªºÀx¦s¸Ë¸mÃþ«¬¡Cªð¦^­È EAX ¥i¯à¬O¤U­±´X¶µ¡A¥kÄæ¥Nªí¨ä·N¸q¡G

ªð¦^­È¼Æ­È·N¡@¡@¸q
DRIVE_UNKNOWN0µLªkÃѧOªº¸Ë¸m
DRIVE_NO_ROOT_DIR1µL¦¹®Ú¥Ø¿ý¡A¨Ò¦p¿é¤JªºÅÞ¿èºÏºÐ¿ù»~¡A¥H­PµL¦¹®Ú¥Ø¿ý
DRIVE_REMOVABLE2¥i¨ø°£ªºÀx¦s¸Ë¸m¡A¦p³nºÐ¡BUSB ÀH¨­ºÐ ( thumb drive )¡BŪ¥d¾÷ ( flash card reader ) µ¥µ¥
DRIVE_FIXED3µwºÐ©Î§Ö°{ºÐ ( flash drive )
DRIVE_REMOTE4ºô¸ôºÏºÐ¾÷
DRIVE_CDROM5¥úºÐ¾÷
DRIVE_RAMDISK6RAM disk

CreateFile API

¦b²Ä¤Q¤@³¹´¿¸g´£¨ì CreateFile¡A¨º®É¬O¥H¥¦¶}±ÒÀɮסA­ì«¬¬°¡G

CreateFile(
 LPCTSTR                lpFileName,
 DWORD                  dwDesiredAccess,
 DWORD                  dwShareMode,
 LPSECURITY_ATTRIBUTES  lpSecurityAttributes,
 DWORD                  dwCreationDisposition,  // how to create
 DWORD                  dwFlagsAndAttributes,   // file attributes
 HANDLE                 hTemplateFile
);

¦ý CreateFile ªº¥\¯à¤£¶È¶È¬O³o¼Ë¡A¥¦ÁÙ¥i¥H¶}±Ò¹êÅéºÏºÐ»PÅÞ¿èºÏºÐ ( ¦b MSDN ùØ¡A¬O»¡¶}±Ò¹êÅéºÏºÐ»PºÏºÐ¼ÐÅÒ¡A­ì¤å¬O¡uphysical disks and volumes¡v¡A¦ý¬OºÏºÐ¼ÐÅÒ«üªºÀ³¸Ó´N¬OÅÞ¿èºÏºÐ )¡C¤@¯ë¦Ó¨¥¡A¦b Win32 ¨t²Î¤¤¡A¤×¨ä¬O Windows NT/XP ¥H¤Wªº§@·~¨t²Î¤¤¡A¶}±Ò¹êÅéºÏºÐ¬O¦³­­¨îªº¡C³o¬O¦]¬°³oºØ°µªk¨ã¦³¦MÀI©Ê¡A°²¦pµ{¦¡¤£·íªº¼g¤J¤À³Î°Ï©Î¬O­«­nªººÏ°Ï¡A·|³y¦¨§@·~¨t²ÎµLªk¿ë»{¡A¦]¦¹¦b¼¶¼g¦¹Ãþµ{¦¡¹êÀ³¯S§O¤p¤ß¡C°£¦¹¤§¥~¡A¨t²Î¹ï¶}±Ò¹êÅéºÏºÐ»PÅÞ¿èºÏºÐ¤]¦³¤@¨Ç­­¨î¡G

  1. ¥²¶·¨ã¦³ºÞ²z­ûÅv­­ªºµ{¦¡¤~¯à°õ¦æ¡C
  2. dwCreationDisposition °Ñ¼Æ¥²¶·¨ã¦³ OPEN_EXISTING ¦ì¤¸¡C
  3. ·í¶}±ÒÅÞ¿èºÏºÐ©Î³nºÐ®É¡AdwShareMode °Ñ¼Æ¥²¶·¨ã¦³ FILE_SHARE_WRITE ¦ì¤¸¡C

·í¶}±Ò¹êÅéºÏºÐ®É¡AlpFileName «ü¦V¥H NULL µ²§À¡B§Î¦p¡u\\.\PhysicalDriveX¡vªº¦r¦ê¡A¨Ò¦p¡G

lpFileName·N¡@¡@¸q
"\\.\PhysicalDrive0",0¶}±Ò²Ä¤@­Ó¹êÅéºÏºÐ
"\\.\PhysicalDrive2",0¶}±Ò²Ä¤T­Ó¹êÅéºÏºÐ

·í¶}±ÒÅÞ¿èºÏºÐ®É¡AlpFileName «ü¦V¥H NULL µ²§À¡B§Î¦p¡u\\.\X:¡vªº¦r¦ê¡A¨Ò¦p¡G

lpFileName·N¡@¡@¸q
"\\.\A:",0¶}±Ò A: ºÏºÐ¡A§Y²Ä¤@­Ó³nºÐ
"\\.\C:",0¶}±Ò C: ºÏºÐ¡A§Y²Ä¤@­ÓÅÞ¿èºÏºÐ

¤@¥¹¦¨¥\¶}±Ò¹êÅéºÏºÐ©ÎÅÞ¿èºÏºÐ«á¡ACreateFile ªº¶Ç¦^­ÈºÙ¬°ª½±µ¦s¨úÀx¦s¸Ë¸m¥N½X ( direct access storage device (DASD) handle )¡A§Ú­Ì¥i¥H§Q¥Î¦¹ DASD ¥N½X©I¥s DeviceIoControl API¡C

DeviceIoControl API

DeviceIoControl ¯à¶Ç¤J³\¦h¤£¦Pªº±±¨î½X¡A¹ï¬Y­Ó¸Ë¸m§@¬Û¹ïÀ³ªº¾Þ§@¡C¨ä­ì«¬¬°¡G

BOOL WINAPI DeviceIoControl(
  __in         HANDLE       hDevice,
  __in         DWORD        dwIoControlCode,
  __in_opt     LPVOID       lpInBuffer,
  __in         DWORD        nInBufferSize,
  __out_opt    LPVOID       lpOutBuffer,
  __in         DWORD        nOutBufferSize,
  __out_opt    LPDWORD      lpBytesReturned,
  __inout_opt  LPOVERLAPPED lpOverlapped
);

²Ä¤@­Ó°Ñ¼Æ¡AhDevice¡A¬O¥Ñ CreateFile ¶Ç¦^ªº¥N½X¡C²Ä¤G­Ó°Ñ¼Æ¡AdwIoControlCode¡A¬O±±¨î½X¡A¤£¦Pªº±±¨î½X¯à¹ï¸Ë¸m§@¤£¦Pªº¾Þ§@¡A¤]¼vÅT lpInBuffer¡BnInBufferSize¡BlpOutBuffer ©M nOutBufferSize ©Ò¥Nªíªº·N¸q¡ClpBytesReturned «ü¦V¤@ÅܼƦì§}¡ADeviceIoControl ·|§â¶Ç¨ì½w½Ä°Ïªº¸ê®Æªø«×¦s¤J¦¹ÅܼƤ¤¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡C¦pªG½w½Ä°Ï¤Ó¤p¥H­P©óµLªk®e¯Ç¶Ç¨Óªº¥ô¦ó¤@µ§¸ê®Æ¡AlpBytesReturned ©Ò«üªºÅܼƷ|Åܬ° 0¡A¥i¥H©I¥s GetLastError¡A¦Ó¶Ç¦^ ERROR_INSUFFICIENT_BUFFER¡F¦pªG½w½Ä°Ï¤Ó¤p¡A¦ý¬O¥i¥H®e¯Ç¼Æµ§¸ê®Æ¡AlpBytesReturned ©Ò«üªºÅܼƷ|Åܬ°¹ê»Ú±o¨ìªº¸ê®Æªø«×¡A¥i¥H©I¥s GetLastError¡A¦Ó¶Ç¦^ ERROR_MORE_DATA¡ClpBytesReturned »P lpOverlapped ¤£¯à¦P®É¬°¹s¡C

¨Æ¹ê¤W¡A¦b MSDN ùØ¡A§â±±¨î½X¤À¦¨¦n´X¶µ¡A¨Ò¦p¦³¡uºÏºÐºÞ²z±±¨î½X¡v( Disk Management Control Codes )¡B¡uÅÞ¿èºÏºÐ±±¨î½X¡v( Volume Management Control Codes )¡B¡u³q°T³]³Æ±±¨î½X¡v( Communications Control Codes ) µ¥µ¥¡C¨C¤@¶µ¤S¥]§t³\¦h±±¨î½X¡A¨Ò¦pºÏºÐºÞ²z±±¨î½X¤S¦³ IOCTL_DISK_CREATE_DISK¡BIOCTL_DISK_GET_DRIVE_GEOMETRY_EX¡BIOCTL_DISK_GET_PARTITION_INFO_EX¡K¡Kµ¥±±¨î½X¡C¦]¦¹±±¨î½X¤Ó¦h¤F¡A©Ò¥H¤p¤ì°¸¶È¶ÈÂǥѩ³¤U¤T­Ó±±¨î½X»¡©ú DeviceIoControl ªº¥Îªk¡C

IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS

IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS ÄÝ©ó¡uÅÞ¿èºÏºÐ±±¨î½X¡vªº¤@ºØ¡A¥¦ªº¥Øªº¬O¨ú¬Y­ÓÅÞ¿èºÏºÐ¦b­þ¤@­Ó¹êÅéºÏºÐ¤W¡C©I¥s¤è¦¡¬O¡G

BOOL DeviceIoControl(
     (HANDLE) hDevice,                        // handle to device
     IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,    // dwIoControlCode
     NULL,                                    // lpInBuffer
     0,                                       // nInBufferSize
     (LPVOID) lpOutBuffer,                    // output buffer
     (DWORD) nOutBufferSize,                  // size of output buffer
     (LPDWORD) lpBytesReturned,               // number of bytes returned
     (LPOVERLAPPED) lpOverlapped              // OVERLAPPED structure
);

hDevice ¬O¸g¥Ñ©I¥s CreateFile ©Ò±oªº¸Ë¸m¥N½X¡A¦]¬°¬O¨ú±oÅÞ¿èºÏºÐªº¸Ë¸m¥N½X¡A©Ò¥H lpFileName «ü¦VÃþ¦ü¡u"\\.\C:",0¡vªº¦r¦ê¡A¦Ó dwShareMode °Ñ¼Æ¥²¶·¨ã¦³ FILE_SHARE_WRITE ¦ì¤¸¡BdwCreationDisposition °Ñ¼Æ¥²¶·¨ã¦³ OPEN_EXISTING ¦ì¤¸¡C

lpOutBuffer «ü¦V¤@­ÓºÙ¬° VOLUME_DISK_EXTENTS ªºµ²ºcÅé¡A¦pªG DeviceIoControl ¦¨¥\°õ¦æ¡A§@·~¨t²Î·|§â¥¿½Tªº¸ê®Æ¶ñ¤J¦¹µ²ºcÅéùØ¡C¦¹µ²ºcÅ骺¦¨­û¬°¡G

VOLUME_DISK_EXTENTS     STRUCT
NumberOfDiskExtents     DWORD           ?
dwUnknown               DWORD           ?       ;MSDNµL¦¹Äæ¦ì
Extents                 DISK_EXTENT     ANYSIZE_ARRAY DUP (<>)
VOLUME_DISK_EXTENTS     ENDS

³oùتº VOLUME_DISK_EXTENTS »P MSDN ©Ò¦Cªº¤£¦P¡AMSDN ¤¤ªº VOLUME_DISK_EXTENTS ¨S¦³ dwUnknown Äæ¦ì¡A³o­ÓÄæ¦ì¬O¤p¤ì°¸¦Û¤v¥[¤W¥hªº¡C¦ý¸g¹L§Ú¹ê»Ú¾Þ§@Æ[¹î¡AVOLUME_DISK_EXTENTS µ²ºcÅéÀ³¸Ó¬O¹³¤W­±³o¼Ë¤~¯à¥¿½T°õ¦æ¡A§_«h¤£¯à¥¿½T°õ¦æ¡C¦ýºô¸ô¤W°£¤F Air ³ì¤¦ªº³¡¸¨®æ¦³»P§ÚÃþ¦üªºª¬ªp¡A¨ä¾l§¡¨S¦³µo²{¡AÅý§Úı±o«Ü¯Ç´e¡CNumberOfDiskExtents ¬O«ü¦¹ÅÞ¿èºÏºÐ¦³¦h¤Ö­Ó¡uextent¡v²Õ¦¨¡A¦pªG¨Ï¥Î°ÊºAºÏºÐ¡ANumberOfDiskExtents ¥i¯à·|¶W¹L 1¡ADeviceIoControl ·|¶Ç¦^ ERROR_MORE_DATA¡A¥Dµ{¦¡À³¸ÓÄ~Äò©I¥s DeviceIoControl¡A±o¨ì¦¹ÅÞ¿èºÏºÐªº©Ò¦³¸ê®Æ¡C²Ä¤T­ÓÄæ¦ì¡AExtents¡A¤]¬O¤@­Óµ²ºcÅé¡ADISK_EXTENT¡A©Ò²Õ¦¨ªº°}¦C¡A¦ý¬O¬d WINDOWS.INC «oµo²{ ANYSIZE_ARRAY ¬° 1¡A¥ç§Y¦¹°}¦C¶È¥Ñ¤@­Ó¤¸¯À²Õ¦¨¡CDISK_EXTENT µ²ºcÅ骺¦¨­û¬O¡G

DISK_EXTENT     STRUCT
DiskNumber      DWORD           ?
dwUnknown       DWORD           ?       ;MSDNµL¦¹Äæ¦ì
StartingOffset  LARGE_INTEGER   <>
ExtentLength    LARGE_INTEGER   <>
DISK_EXTENT     ENDS

²Ä¤@­ÓÄæ¦ì¡ADiskNumber¡A´N¬O¸ÓÅÞ¿èºÏºÐ¦b­þ¤@­Ó¹êÅéºÏºÐ¤¤¡A¦pªG¬O¡u0¡v¡Aªí¥Ü¦b¡u\\.\PhysicalDrive0¡v¹êÅéºÏºÐùØ¡C²Ä¤G­ÓÄæ¦ì¡AdwUnknown¡A¤]©M VOLUME_DISK_EXTENTS ªº dwUnknown ¤@¼Ë¡A¤£ª¾¦ó¬G¡A»P§Ú¹êÅ礣²Å¡C²Ä¤T¡B²Ä¥|­ÓÄæ¦ì¬O 64 ¦ì¤¸ªøªº¾ã¼Æ¡A¤À§Oªí¥Ü¦¹ÅÞ¿èºÏºÐ±q¹êÅéºÏºÐªº²Ä´X­Ó¦ì¤¸²Õ¶}©l¡A¦Óªø«×¬°¦h¤Ö¦ì¤¸²Õ¡C¥¦­Ìªº¸ê®Æ§ÎºA¬° LARGE_INTEGER¡A¥i¥H¦b WINDOWS.INC ùاä¨ì¡G

LARGE_INTEGER       UNION
    STRUCT
        LowPart     DWORD   ?
        HighPart    DWORD   ?
    ENDS
    QuadPart        QWORD   ?
LARGE_INTEGER       ENDS

¦A¦^¨ì DeviceIoControl ªº²Ä¤»­Ó°Ñ¼Æ¡AnOutBufferSize¡Aªí¥Ü½w½Ä°Ï¤j¤p¡A¦¹³B¶ñ¤W¡uSIZEOF VOLUME_DISK_EXTENTS¡v§Y¥i¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡A¥²¶·¦b©I¥s«e±À¤J°ïÅ|¡C²Ä¤C­Ó°Ñ¼Æ¡AlpBytesReturned¡A«ü¦V¬Y­ÓÅܼơA¥HÀò±o¹ê»Ú¤W±q DeviceIoControl ¶Ç¦^ªº¸ê®Æªø«×¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡C³Ì«á¤@­Ó°Ñ¼Æ¡AlpOverlapped¡A¥¦¬O¤@­Ó¦ì§}¡A«ü¦V OVERLAPPED µ²ºcÅé¡C¦pªG lpBytesReturned ¬° NULL¡A´N¥²¶·³]©w lpOverlapped ¬°¬Y­Ó OVERLAPPED µ²ºcÅ骺¦ì§}¡F¦pªG lpBytesReturned ¤w³]¬° ¬Y­ÓÅܼƪº¦ì§}¡AlpOverlapped ´N¥²¶·¬° NULL¡C¦pªG DeviceIoControl °õ¦æ¦¨¥\¡A¶Ç¦^­È¬°«D¹s¡F­Y¥¢±Ñ¡A¶Ç¦^­È¬° 0¡A¥i¥H©I¥s GetLastError¡A¦pªG GetLastError ¶Ç¦^ ERROR_MORE_DATA¡A´Nªí¥Ü½w½Ä°Ï¤Ó¤p¡A¤£¨¬¥H®e¯Ç¶Ç¦^ªº¸ê®Æ¡C

IOCTL_DISK_GET_DRIVE_GEOMETRY_EX

IOCTL_DISK_GET_DRIVE_GEOMETRY_EX ÄÝ©ó¡uºÏºÐºÞ²z±±¨î½X¡vªº¤@ºØ¡A¥Î¨Ó¨ú±o¹êÅéºÏºÐªº¸ê®Æ¡A¦pºÏ¬W ( cylinders ) Á`¼Æ¡B¨CºÏ¬W©Ò§tºÏ­y ( tracks ) ¼Æ¡A¨CºÏ­y©Ò§tºÏ°Ï ( sectors ) ¼Æ¡B¨CºÏ°Ï©Ò§t¦ì¤¸²Õ ( bytes ) ¼Æ¡A¨ä­ì«¬¬°¡G

BOOL DeviceIoControl(
     (HANDLE) hDevice,                 // handle to volume
     IOCTL_DISK_GET_DRIVE_GEOMETRY_EX, // dwIoControlCode
     NULL,                             // lpInBuffer
     0,                                // nInBufferSize
     (LPVOID) lpOutBuffer,             // output buffer
     (DWORD) nOutBufferSize,           // size of output buffer
     (LPDWORD) lpBytesReturned,        // number of bytes returned
     (LPOVERLAPPED) lpOverlapped       // OVERLAPPED structure
);

hDevice ¬O©I¥s CreateFile ¶Ç¦^¨Óªºª½±µ¦s¨úÀx¦s¸Ë¸m¥N½X¡A©I¥s CreateFile ®É¡AlpFileName À³«ü¦VÃþ¦ü¡u\\.\PhysicalDriveX¡v¦r¦êªº¦ì§}¡A½Ð°Ñ¦Ò«e­±ªº»¡©ú¡ClpOutBuffer «ü¦V DISK_GEOMETRY_EX µ²ºcÅ骺¦ì§}¡A¦pªG DeviceIoControl °õ¦æ¥¿½T¡A§@·~¨t²Î·|§â¸ê®Æ¶ñ¤J¦¹µ²ºcÅ餺¡A³o­Óµ²ºcÅ骺Äæ¦ì¬O¡G

DISK_GEOMETRY_EX    STRUCT
Geometry            DISK_GEOMETRY   <>
DiskSize            LARGE_INTEGER   <>
DataOne             BYTE            ?
DISK_GEOMETRY_EX    ENDS

²Ä¤G­ÓÄæ¦ì¡ADiskSize¡A¬O¤@­Ó 64 ¦ì¤¸ªº¥¿¾ã¼Æ¡Aªí¥ÜºÏºÐ¤j¤p¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡CGeometry ¬O¤@­Ó DISK_GEOMETRY µ²ºcÅé¡A¦UÄæ¦ì¬O¡G

DISK_GEOMETRY       STRUCT
Cylinders           LARGE_INTEGER   <>
MediaType           MEDIA_TYPE      ?
TracksPerCylinder   DWORD           ?       ;¨CºÏ¬W©Ò§tºÏ­y¼Æ
SectorsPerTrack     DWORD           ?       ;¨CºÏ­y©Ò§tºÏ°Ï¼Æ
BytesPerSector      DWORD           ?       ;¨CºÏ°Ï©Ò§t¦ì¤¸²Õ¼Æ
DISK_GEOMETRY       ENDS

Cylinders ¬O¤@­Ó 64 ¦ì¤¸ªº¥¿¾ã¼Æ¡Aªí¥ÜºÏ¬WÁ`¼Æ¡CMediaType ¬O¤@­Ó 32 ¦ì¤¸ªº¥¿¾ã¼Æ¡A¥NªíÀx¦s´CÅ骺Ãþ«¬¡A¦p³nºÐ¤ù¡BºÏ¥ú¾÷©ÎµwºÐµ¥µ¥¡A¦ý¤j³¡¤À§¡¤w²^¨O¡A²{¦b±`¥Îªº¤j·§¥u³Ñ¤U RemovableMedia ©M FixedMedia¡A¤À§Oªí¥ÜÀH¨­ºÐ©MµwºÐ¡C¨ä¥LªºÄæ¦ì§Ú·Q´N¤£¥²»¡©ú¤F¡C

¦A¦^¨ì DeviceIoControl ªº²Ä¤»­Ó°Ñ¼Æ¡AnOutBufferSize¡Aªí¥Ü½w½Ä°Ï¤j¤p¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡AÀ³¸Ó¶ñ¤J¡uSIZEOF DISK_GEOMETRY_EX¡v¡ClpBytesReturned «ü¦V¤@­ÓÅܼƪº¦ì§}¡ADeviceIoControl ·|§â¶Ç¦^ªº¸ê®Æªø«×¶ñ¤J¦¹ÅܼÆùØ¡A¶Ç¦^ªº¸ê®Æªø«×³æ¦ì¬O¦ì¤¸²Õ¡C¦pªG nOutBufferSize ©Ò«ü©wªº¤j¤p¤Ó¤p¡A«h©I¥s¥¢±Ñ¡AlpBytesReturned ¬° 0¡A¥i¥H©I¥s GetLastError¡A¦Ó¶Ç¦^ ERROR_INSUFFICIENT_BUFFER¡C¦pªG lpOverlapped ¬° NULL¡A«h lpBytesReturned ¤£¥i¬° NULL¡C¦pªG DeviceIoControl °õ¦æ¦¨¥\¡A¶Ç¦^«D¹s­È¡F¦pªG¥¢±Ñ¡A¶Ç¦^ 0¡A©I¥s GetLastError ±o¨ì§ó¦h¸ê°T¡C

IOCTL_DISK_GET_LENGTH_INFO

IOCTL_DISK_GET_LENGTH_INFO ÄÝ©ó¡uºÏºÐºÞ²z±±¨î½X¡vªº¤@ºØ¡A¥Î©ó¨ú±o¹êÅéºÏºÐ©ÎÅÞ¿èºÏºÐªº¤j¤p¡A¥H¦ì¤¸²Õ¬°³æ¦ì¡C©I¥s DeviceIoControl ®Éªº¦U°Ñ¼Æ¬°¡G

BOOL DeviceIoControl(
  (HANDLE) hDevice,              // handle to device
  IOCTL_DISK_GET_LENGTH_INFO,    // dwIoControlCode
  NULL,                          // lpInBuffer
  0,                             // nInBufferSize
  (LPVOID) lpOutBuffer,          // output buffer
  (DWORD) nOutBufferSize,        // size of output buffer
  (LPDWORD) lpBytesReturned,     // number of bytes returned
  (LPOVERLAPPED) lpOverlapped    // OVERLAPPED structure
);

¨ä¤¤ hDevice ¬OÅÞ¿èºÏºÐ¥N½X©Î¹êÅéºÏºÐ¥N½X¡A©I¥s CreateFile ¥i¶Ç¦^³o¨âºØ¥N½X¡ClpOutBuffer «ü¦V GET_LENGTH_INFORMATION µ²ºcÅé¡A¦¹µ²ºcÅé¥u¦³¤@­ÓÄæ¦ì¡G

GET_LENGTH_INFORMATION  STRUCT
LengthOfDev             LARGE_INTEGER   <>
GET_LENGTH_INFORMATION  ENDS

¦b MSDN ³o­ÓÄæ¦ìºÙ¬° Length¡A¦ý¬O»P MASM ªº«O¯d¦r¬Û¦P¡A¦]¦¹¤p¤ì°¸§ï¬° LengthOfDev¡A¥¦¬O 64 ¦ì¤¸ªº¥¿¾ã¼Æ¡C¦pªG hDevice ¬OÅÞ¿èºÏºÐ¡A«h LengthOfDev ¬OÅÞ¿èºÏºÐ¤j¤p¡F¦pªG hDevice ¬O¹êÅéºÏºÐ¡A«h LengthOfDev ¬O¹êÅéºÏºÐ¤j¤p¡C§¡¥H¦ì¤¸²Õ¬°³æ¦ì¡C¨ä¥L°Ñ¼Æ»P«e­±¬Û¦P¡A½Ð¦Û¦æ°Ñ¦Ò¡C

GetLogicalDriveFromPhysicalDrive ªº¬yµ{

»¡§¹ GetLogicalDriveFromPhysicalDrive ¥Î¨ìªº API «á¡A¤p¤ì°¸¥ýµ¹¤j®a¬Ý¬Ý GetLogicalDriveFromPhysicalDrive ªº­ì©l½X¡AµM«á¦AÁ¿¸Ñ¬yµ{¡C©³¤U´N¬O­ì©l½X¡G

;-----------------------------------------------------------------------------------------------;001
;¦¹°Æµ{¦¡¡AGetLogicalDriveFromPhysicalDrive¡A¯à¨ú¨C­Ó¹êÅéµwºÐ¤¤©Ò§tªºÅÞ¿èºÏºÐ¡A¨Ã¶Ç¦^µ¹¥Dµ{¦¡
;¿é¤J¡GlpData¡Ð­n¶Ç¦^ªº¸ê®Æ¦ì§}¡A¦pªGGetLogicalDriveFromPhysicalDrive¥¿½T°õ¦æ¡A·|¦b¦¹¦ì§}¶ñ¤W
;              ®æ¦¡¬°¡u'\\.\PhysicalDrive0',0,'C:',0,'D:',0,
;                      '\\.\PhysicalDrive1',0,'E:',0,0¡v
;                      ªí¥Ü²Ä¤@­Ó¹êÅéµwºÐ(§Y\\.\PhysicalDrive0)¦³¨â­ÓÅÞ¿èºÏºÐ¡AC¡BD
;                      ²Ä¤G­Ó¹êÅéµwºÐ(§Y\\.\PhysicalDrive1)¶È¤@­ÓÅÞ¿èºÏºÐ¡AE
;                      ¥H¡u0,0¡v¬°µ²§À
;              ¦pªG¥Dµ{¦¡¶Ç¨ÓªºlpData¬°¹s¡A¶Ç¦^­ÈEAX¬°©Ò»Ý®e¯Çªº¸ê®Æ¤j¤p(¥H¦ì¤¸²Õ¬°³æ¦ì)
;¿é¥X¡GEAX¡Ð­Y¦¨¥\¡AEAX¬°¶Ç¦^ªº¸ê®Æ¤j¤p(¥H¦ì¤¸²Õ¬°³æ¦ì)¡A¥BlpData©Ò«ü¦ì§}·|¶ñ¤J¥¿½Tªº¸ê®Æ       ;010
;           ­Y¥¢±Ñ¡AEAX¡×0
GetLogicalDriveFromPhysicalDrive    PROC    USES esi edi ebx, lpData:DWORD
                LOCAL   aryLogicalDrive[104]:BYTE       ;¨C­ÓÅÞ¿èºÏºÐ¾÷¦WºÙ¬°¡u'C:\',0¡v¡A³Ì¦h26­Ó
                LOCAL   szDeviceName[8]:BYTE            ;'\\.\C:',0
                LOCAL   vde:VOLUME_DISK_EXTENTS
                LOCAL   idx:DWORD       ;©I¥sGetLogicalDriveStrings±o¨ìÅÞ¿èÀx¦s¸Ë¸m¦Wªº°}¦C¯Á¤Þ
                LOCAL   nOut:DWORD      ;©I¥sDeviceIoControl¶Ç¦^Ū¨ú¦h¤Ö¦ì¤¸²Õ
                LOCAL   nData:DWORD     ;hMem¤º©Ò°O¸üªº­«­n¸ê®Æ¤j¤p¡A¥ç§Y­n¶Ç¦^ªº¸ê®Æ¤j¤p¡A¥H¦ì¤¸²Õ¬°³æ¦ì
                LOCAL   nPhysicalDriver:DWORD   ;¹êÅéµwºÐ­Ó¼Æ
                LOCAL   hDevice,hMem:HANDLE                                                     ;020
                LOCAL   szPhyDrive[20]:BYTE
                sub     ecx,ecx
                cld
                mov     idx,ecx
                mov     nData,ecx                                                               ;025
                mov     nPhysicalDriver,ecx
        ;ªì©l¤ÆszPhyDrive¦r¦ê¡A¥ç§Y¶ñ¤J"\\.\PhysicalDrive0",0
                lea     edi,szPhyDrive
                mov     DWORD PTR [edi],5c2e5c5ch
                mov     DWORD PTR [edi+4],73796850h                                             ;030
                mov     DWORD PTR [edi+8],6c616369h
                mov     DWORD PTR [edi+12],76697244h
                mov     DWORD PTR [edi+16],3065h
        ;ªì©l¤ÆszDeviceName¦r¦ê¡A¥ç§Y¶ñ¤J"\\.\",?,":",0
                lea     edi,szDeviceName                                                        ;035
                mov     DWORD PTR [edi],5c2e5c5ch
                mov     WORD PTR [edi+5],3ah
        ;¹w¯d2KBªÅ¶¡¦s©ñ­n¶Ç¦^ªº¸ê®Æ(¨C­ÓÅÞ¿èºÏºÐ¦û3­Ó¦ì¤¸²Õ¡a¦p¡uC:¡v,0¡b¡A°²³]¨C­Ó¹êÅéµwºÐ³£¦³ 26
        ;­ÓÅÞ¿èºÏ°Ï¡A¬G¦û26*3¡×78¦ì¤¸²Õ¡A¦A¥[¤W¡u"\\.\PhysicalDrive1",0¡v¦@19­Ó¦ì¤¸²Õ¡A¬G¨C­Ó¹êÅéµw
        ;ºÐ¦û97­Ó¦ì¤¸²Õ¡A°²³]¦³20­Ó¹êÅéµwºÐ¡A¦@»Ý­n1940­Ó¦ì¤¸²Õ¡A¦ý¦A¥[¤W³Ì«á¤@­Ó0¡A¦³971­Ó¦ì¤¸²Õ¡A
        ;¬°¨D«OÀI¡A¦V¨t²Î­n¨D2KB)
                INVOKE  GlobalAlloc,GPTR,2048
                mov     hMem,eax

                INVOKE  GetLogicalDriveStrings,SIZEOF aryLogicalDrive,ADDR aryLogicalDrive      ;045
next_drv:       mov     edx,idx
                lea     esi,aryLogicalDrive
                shl     edx,2
                add     esi,edx
                mov     al,[esi]        ;AL¡×ÅÞ¿èÀx¦s¸Ë¸m¦WºÙ¡A¦p"C"¡B"D"µ¥µ¥                   ;050
                cmp     al,0
                jz      finished
                lea     edi,szDeviceName
                mov     [edi+4],al
                INVOKE  GetDriveType,esi                                                        ;055
        ;­YEAX¡×DRIVE_FIXED¡A«h¦¹ÅÞ¿èºÏºÐ¥i¯à¦bµwºÐ©ÎÀH¨­ºÐ(flash driver)¤W
        .IF eax==DRIVE_FIXED
                INVOKE  CreateFile,ADDR szDeviceName,GENERIC_READ,FILE_SHARE_READ or \
                        FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
                cmp     eax,INVALID_HANDLE_VALUE                                                ;060
                je      error
                mov     hDevice,eax
                INVOKE  DeviceIoControl,hDevice,IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,0,0,ADDR vde,\
                        SIZEOF VOLUME_DISK_EXTENTS,ADDR nOut,0
                INVOKE  CloseHandle,hDevice                                                     ;065
            ;­pºâÀ³¸Ó§â¦¹ÅÞ¿èºÏºÐ¦s¦bhMemªº­þ­Ó¦ì§}¡A¦¹¦ì§}«e´X­Ó¦ì¤¸²Õ¬°"\\.\PhysicalDrive"¡A­Y
            ;¤£¬Oªº¸Ü¡A«hªí¥Ü¦¹ÅÞ¿èºÏºÐ¬°©|¥¼³Q¦¹°Æµ{¦¡³B²z¹Lªº¹êÅéµwºÐ
                mov     eax,vde.Extents.DiskNumber
                sub     edx,edx
                mov     bl,al                                                                   ;070
                mov     ecx,97  ;Windows¤¤¡A¨C­Ó¹êÅéµwºÐ³Ì¦h26­ÓÅÞ¿èºÏºÐ¡A¨C­ÓÅÞ¿èºÏºÐ¦û3¦ì¤¸²Õ
                mov     edi,hMem;(¦p¡uC:\,0¡v)¡A¦A¥[¤W¡u\\.\PhysicalDrive?",0¡v¡A¦@97¦ì¤¸²Õ
                mul     ecx
                lea     esi,szPhyDrive
                add     edi,eax                                                                 ;075
            ;§ä¨ì·sªº¹êÅéµwºÐ¡A§âszPhyDrive¦r¦ê¦s¤JEDI©Ò«ü¦ì§}
            .IF BYTE PTR [edi]==0
                mov     ecx,19
                rep     movsb
                add     [edi-2],bl                                                              ;080
                inc     nPhysicalDriver
                jmp     save_driver
            .ENDIF
            ;·j´MhMem¸Ì¡A¦¹¹êÅéµwºÐ"\\.\PhysicalDrive?",0,'C:',0,'D:',0,0,0¡K¡K«á­þ¤@­Ó¦ì§}¬OªÅµÛªº
                mov     al,0                                                                    ;085
                mov     ecx,97
@@:             repne   scasb
                cmp     BYTE PTR [edi],0
                jne     @b
            ;¨ú±oÅÞ¿èºÏºÐ¦WºÙ¡A¦s©óAL                                                           ;090
save_driver:    lea     edx,szDeviceName
                mov     ah,':'
                mov     al,[edx+4]
                stosw
                xor     al,al                                                                   ;095
                stosb
        .ENDIF
                inc     idx
                jmp     next_drv
                                                                                                ;100
;¤w¦bhMem°O¿ý¤F¹êÅéµwºÐ¥]§t­þ¨ÇÅÞ¿èºÏºÐ¡A²{¦b­n§â¥¦¶Çµ¹¥Dµ{¦¡«ü©wªº°O¾ÐÅé¤F
finished:       mov     edi,lpData
                sub     edx,edx
                mov     idx,edx
   .WHILE idx<=20                                                                               ;105
                mov     eax,97
                sub     edx,edx
                mov     esi,hMem
                mul     idx
                add     esi,eax ;ESI¡×¦bhMemùØ¡A¬Y­Ó¹êÅéµwºÐªº¶}©l¦ì§}                          ;110
       .IF BYTE PTR [esi]=="\"
                mov     ecx,19
                add     nData,ecx
            .IF edi==0
                add     esi,ecx                                                                 ;115
            .ELSE
                rep     movsb
            .ENDIF
                xor     ebx,ebx ;EBX¡×ÅÞ¿èºÏºÐ­Ó¼Æ
next_logic_drv: cmp     BYTE PTR [esi],0                                                        ;120
                jz      finish_the_phy
                mov     ecx,3
                add     nData,ecx
            .IF edi==0
                add     esi,ecx                                                                 ;125
            .ELSE
                lodsw
                stosw
                lodsb
                stosb                                                                           ;130
            .ENDIF
                inc     ebx
                cmp     ebx,26
                jb      next_logic_drv
       .ENDIF                                                                                   ;135
finish_the_phy: inc     idx
   .ENDW
            .IF edi!=0
                mov     BYTE PTR [edi],0
            .ENDIF                                                                              ;140
                INVOKE  GlobalFree,hMem
                mov     eax,nData
                inc     eax
                jmp     exit
error:          INVOKE  GlobalFree,hMem                                                         ;145
                xor     eax,eax
exit:           ret
GetLogicalDriveFromPhysicalDrive    ENDP
;-----------------------------------------------------------------------------------------------;149

¦A¨Ó­n»¡¤@»¡ GetLogicalDriveFromPhysicalDrive ªº¬yµ{¡G

  1. GetLogicalDriveFromPhysicalDrive °Æµ{¦¡¤@¶}©l¡A¬Oªì©l¤Æ¦U­Ó°Ï°ìÅܼơC

  2. ²Ä 38¡ã43 ¦æ¡A¬O«Ø¥ß¤@­Ó°O¾ÐÅé°Ï¶ô¡A¦¹°O¾ÐÅé°Ï¶ô¦ì§}¬O¦b hMem ÅܼÆùØ¡A¦¹°O¾ÐÅé°Ï¶ô±N¨Ì·Ó¨C­Ó¹êÅéºÏºÐ¤ºªºÅÞ¿èºÏºÐ¨Ó¤À°t¡C¦ý¬O¦bµ{¦¡¤@¶}©l®É¡A§Ú­Ì¤£ª¾¹D¨C­Ó¹êÅéºÏºÐ¦³´X­ÓÅÞ¿èºÏºÐ¡A©Ò¥H°²³]³Ì¦h 26 ­ÓÅÞ¿èºÏºÐ ( ¨Æ¹ê¤W¡AWindows §@·~¨t²Î´N¥u¯à¿ë»{ A: ¨ì Z: ¦@ 26 ­ÓÅÞ¿èºÏºÐ¡A³o¨ÇÅÞ¿èºÏºÐ¥]§t¤F³nºÐ¡BµwºÐ¡B¥úºÐ¡BÀH¨­ºÐ¡Bºô¸ôºÏºÐµ¥µ¥ )¡C¨C­ÓÅÞ¿èºÏºÐ¦û¤F 3 ­Ó¦ì¤¸²Õ¡A¨Ò¦p¡u"C:",0¡v¡A¦A¥[¤W¡u"\\.\PhysicalDrive1",0¡v¦@19­Ó¦ì¤¸²Õ¡A¬G¨C­Ó¹êÅéµwºÐ¦û 97 ­Ó¦ì¤¸²Õ¡C°²³]¦³ 20 ­Ó¹êÅéµwºÐ¡A¦@»Ý­n 1940 ­Ó¦ì¤¸²Õ¡A©Ò¥H¦V¨t²Î­n¨D 2KB¡C

    GetLogicalDriveFromPhysicalDrive °õ¦æªñµ²§ô®É¡AhMem ªº¤º®e·|¬O¹³¤U­±³o¼Ë¡G

    00146068  5C 5C 2E 5C 50 68 79 73 69 63 61 6C 44 72 69 76  \\.\PhysicalDriv
    00146078  65 30 00 43 3A 00 45 3A 00 48 3A 00 49 3A 00 00  e0.C:.E:.H:.I:..
    00146088  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00146098  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    001460A8  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    001460B8  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    001460C8  00 5C 5C 2E 5C 50 68 79 73 69 63 61 6C 44 72 69  .\\.\PhysicalDri
    001460D8  76 65 31 00 46 3A 00 47 3A 00 4A 3A 00 00 00 00  ve1.F:.G:.J:....
    001460E8  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    001460F8  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00146108  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00146118  00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    00146128  00 00 5C 5C 2E 5C 50 68 79 73 69 63 61 6C 44 72  ..\\.\PhysicalDr
    00146138  69 76 65 32 00 44 3A 00 00 00 00 00 00 00 00 00  ive2.D:.........
                             hMem ªº¤º®e

    ¥Ñ¤W­± hMem ªº¤º®e¨Ó¬Ý¡A¤p¤ì°¸·Qªk«Ü²³æ¡A¥ý¬°¨C­Ó¹êÅéºÏºÐ¹w¯d 97 ­Ó¦ì¤¸²Õ¡A¦A¨M©w¦U­ÓÅÞ¿èºÏºÐ¦ì©ó­þ­Ó¹êÅéºÏºÐ¤¤¡AµM«á­pºâ¸ÓÅÞ¿èºÏºÐÀ³¸Ó¶ñ¤J­þ¤@­Ó¹êÅéºÏºÐ©ÒÄݪº°Ï¶ô¡A¦Aµy¥[¾ã²z§R¥h¦h¾lªºªÅ¶¡§Y¥i¡C

  3. ²Ä 44¡ã45 ¦æ¬O©I¥s GetLogicalDriveStrings¡A±o¨ìÅÞ¿èºÏºÐªº®Ú¥Ø¿ý¦W¶ñ¤J aryLogicalDrive ùØ­±¡AaryLogicalDrive ¬O¤@­Ó°Ï°ì°}¦C¡A¦¹°}¦C¦@ 104 ¦ì¤¸²Õ ( 26¡Ñ4¡×104 )¡C·í GetLogicalDriveStrings °õ¦æ§¹«á¡AaryLogicalDrive ¤º®eÅܬ°¡G

    0012F760                                      43 3A 5C 00              C:\.
    0012F770  44 3A 5C 00 45 3A 5C 00 46 3A 5C 00 47 3A 5C 00  D:\.E:\.F:\.G:\.
    0012F780  48 3A 5C 00 49 3A 5C 00 4A 3A 5C 00 4B 3A 5C 00  H:\.I:\.J:\.K:\.
    0012F790  4D 3A 5C 00 4E 3A 5C 00 00                       M:\.N:\..
  4. ²Ä 46 ¦æ¶}©l¡A¬O¥H idx ÅܼƬ°¯Á¤Þ­È¡A¥ç§Y idx ¥Ñ 0 ¶}©l¨C¦¸¼W¥[¤@¡AÀËÅç¨C­ÓÅÞ¿èºÏºÐ¦ì©ó­þ¤@­Ó¹êÅéºÏºÐùØ¡C

    1. ¦b²Ä 46¡ã49 ¦æ­pºâÅÞ¿èºÏºÐ®Ú¥Ø¿ý¦Wªº¦ì§}¡A¦s©ó ESI¡A·í idx ¬° 0 ®É¡AESI «ü¦V¡u"C:\",0¡v¡F·í idx ¬° 1 ®É¡AESI «ü¦V¡u"D:\",0¡v¡K¡K¨Ì¦¹Ãþ±À¡C
    2. ²Ä 50¡ã52 ¦æ¡AÀˬd ESI ©Ò«üªºÅÞ¿èºÏºÐ®Ú¥Ø¿ý¦W¬O§_¬°¡u0¡v¡A¦pªG¬O¡Aªí¥Ü¤w¸g³B²z§¹©Ò¦³ªºÅÞ¿èºÏºÐ¦WºÙ¡A¸õ¨ì¤U¤@¶¥¬q¡A¡u§â¸ê®Æ¶Ç¦^µ¹¥Dµ{¦¡«ü©wªº°O¾ÐÅé¡v¡A§Yµ{¦¡²Ä 101 ¦æ¥H«á¡C
    3. ¦pªG ESI ©Ò«üÅÞ¿èºÏºÐ®Ú¥Ø¿ý¤£¬°¡u0¡v¡A±o¨ìÅÞ¿èºÏºÐ¥N¸¹¡A¦s©ó AL ùØ ( ²Ä 50 ¦æ )¡A¨Ã¦s©ó szDeviceName ¦r¦êùØ¡A¥H«Ý d ¨BÆJ¤§¥Î¡C±µµÛ¥H¦s©ó ESI ¤ºªº¦ì§}¬°°Ñ¼Æ¡A©I¥s GetDriveType¡A±o¨ìÅÞ¿èºÏºÐªºÀx¦s¸Ë¸mÃþ«¬¡A¦pªG¬OµwºÐ©ÎÀH¨­ºÐ¤~Ä~Äò d ¨BÆJ¡F¦pªG¤£¬O¡A«h¸õ¨ì²Ä 98 ¦æ¡C
    4. ²Ä 58¡ã65 ¦æ¡A¥H szDeviceName ©I¥s CreateFile ¨ú±o¸Ë¸m¥N½X¡AszDeviceName ¦r¦ê¬OÃþ¦ü¡u"\\.\C:",0¡vªº¦r¦ê¡A¨ä¤¤ªºÅÞ¿èºÏºÐ¦WºÙ¦­¤w¦b²Ä 54 ¦æ³Æ§´¤F¡C±µµÛ¥H IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS ©I¥s DeviceIoControl¡A¦b vde.Extents.DiskNumber ´N¦s¦³¸ÓÅÞ¿èºÏºÐ©Ò¦bªº¹êÅéºÏºÐ¡A±N¨ä¦s©ó EAX ( ²Ä 68 ¦æ )¡C
    5. ²Ä 66¡ã75 ¦æ¡A­pºâ¸Ó¹êÅéºÏºÐÀ³¸Ó¦b­þ¤@­Ó¦ì§}¤W¡A¨Ã¦s©ó EDI ùØ¡AEDI ©Ò«üªº°O¾ÐÅé¦ì§}¦ì©ó hMem ùØ¡C¨Ò¦p¦pªG¬O¹êÅéºÏºÐ 2¡A©Ò±o EDI ¬° 014612A¡A«ü¦V¡uhMem ªº¤º®e¡vªºµµ¦â¦ì§}¡C
    6. ²Ä 76¡ã83 ¦æ¡AÀˬd EDI ©Ò«ü¦ì§}ªº¤º®e¬O§_¬°¡u0¡v¡A¦pªG¬O¹s¡Aªí¥Ü¤§«e³B²z¹LªºÅÞ¿èºÏºÐ¨ÃµL¥ô¦ó¤@­ÓÄݩ󦹹êÅéºÏºÐ¡A§â¡u"\\.\PhysicalDrive?",0¡v¦r¦ê¶ñ¤J EDI ©Ò«ü¦ì§}¡F¦pªG¤£¬°¡u0¡v¡Aª½±µ¸õ¨ì¨BÆJ g¡C
    7. ²Ä 84¡ã89 ¦æªºµ{¦¡½X¬O·j´MªÅªºÄæ¦ì¡A¥H¶ñ¤JÅÞ¿èºÏºÐ¦WºÙ¡C¶ñ¤JÅÞ¿èºÏ°Ïªºµ{¦¡½X¦b²Ä 90¡ã97 ¦æ¡C

  5. ¦¹¤@¶¥¬qµ{¦¡ªº¤u§@¬O§â¦b hMem ©Ò«ü¦ì§}ªº°O¾ÐÅ餺®e¶Ç¦^¨ì¥Dµ{¦¡«ü©wªº¦ì§}¡A¦¹¦ì§}¬O¥H°Ñ¼Æ¡AlpData¡A¶Çµ¹ GetLogicalDriveFromPhysicalDrive ªº¡A¦b²Ä 102 ¦æ¡A«ü©w EDI ¬° lpData¡C¦¹¬qµ{¦¡¤´¥H idx ¬°¯Á¤Þ¡A©Ò¥H¥ý±N¨ä²MªÅ¡A¦]¬°¤§«e°²³]³Ì¦h¨t²Î±µ¤F 20 ­Ó¹êÅéºÏºÐ¡A©Ò¥H idx ¦b¤p©ó 20 ®É¡A¤~°µ±µ¤U¨Óªº¨BÆJ¡A¦]¦¹§â±µ¤U¨Óªº¨BÆJ©ñ¦b¤@­Ó¡u.WHILE¡v©M¡u.ENDW¡v°j°éùØ¡C±µ¤U¨Óªº¨BÆJ¡A¤p¤ì°¸¤]²Ó¤À´X¬q»¡©ú¡G

    1. ²Ä 106¡ã110 ¦æªºµ{¦¡½X¬O­pºâ¬Y­Ó¹êÅéºÏºÐ°Ï¶ôªº°_©l¦ì§}¡A¨Ã±Nµ²ªG¦s©ó ESI ùØ¡C
    2. ²Ä 111¡ã118 ¦æ¬O§â¡u"\\.\PhysicalDrive?",0¡v¦r¦ê¶ñ¤J EDI ©Ò«üªº¦ì§}ùØ¡C¦ý¬O¦pªG EDI ¬°¹s¡Aªí¥Ü GetLogicalDriveFromPhysicalDrive ¥u»Ý­n­pºâ©Ò»Ý®e¯Çªº°O¾ÐÅé°Ï¶ô¤j¤p¡F¦pªG EDI ¤£¬°¹s¡A¨º´N­n¹ê»Ú§â¸Ó¦r¦ê²¾¤J¥Dµ{¦¡¦b lpData «ü©wªº¦ì§}¤º¡C
    3. ²Ä 119¡ã134 ¦æ¬O§â¬Y­Ó¹êÅéºÏºÐ¤º©Ò§tªºÅÞ¿èºÏºÐ¡A¤@­Ó¤@­Óªº¶ñ¨ì¥Dµ{¦¡«ü©wªº¦ì§}¤º¡A¸ò¨BÆJ b ¤@¼Ë¡A¦pªG EDI ¬°¹s¡Aªí¥Ü¥u»Ý­n­pºâ©Ò»Ý®e¯Çªº°O¾ÐÅé°Ï¶ô¤j¤p¡F¦pªG EDI ¤£¬°¹s¡A¨º´N­n¹ê»Ú§âÅÞ¿èºÏºÐªº¦WºÙ²¾¤J¥Dµ{¦¡«ü©wªº¦ì§}¤º¡C¦¹¥~¡AWindows ¨t²Î³Ì¦h¤]¶È¯à¿ë»{ 26 ­ÓÅÞ¿èºÏºÐ¡A©Ò¥H§Q¥Î EBX ¥h­pºâÅÞ¿èºÏºÐ­Ó¼Æ¡A¦pªG¶W¹L´N¥i¥H°±¤î¤F¡C
  6. ²Ä 138¡ã140 ¦æ¡A¦pªG EDI ¤£¬°¹s¡Aªí¥Ü¥²¶·¶Ç¦^¦U­Ó¹êÅéºÏºÐ©Ò§tªºÅÞ¿èºÏºÐ¦WºÙ¡A¨ì¦¹¬°¤î´X¥G§¹¦¨¡A³Ì«á¦A¶ñ¤J¤@­Ó¦ì¤¸²Õªº¹s¡A¨Ï¨äÅܦ¨¨â­Ó¡u0¡v¡A§@¬°¸ê®Æªºµ²§À¡C²Ä 141 ¦æ¡AÄÀ©ñ hMem °O¾ÐÅé°Ï¶ô¡C²Ä 142¡ã143 ¦æ¡A¦]¬°¦h¤F¤@­Ó¦ì¤¸²Õªº¡u0¡v¡A©Ò¥H©Ò»Ý°O¾ÐÅéªø«×¦A¼W¥[¤@¡A¨Ã¦s©ó EAX ùØ¡A§@¬°¶Ç¦^­È¡C


½d¨Ò

©³¤U¤p¤ì°¸´N¥H¤@­Ó¨Ò¤l¡A»¡©ú¦p¦ó©I¥s GetLogicalDriveFromPhysicalDrive¡C§Ú·QÁÙ¬O¨Ï¥Î²Ä 20 ³¹ªº¤èªk«Ø¥ß HD.DLL °ÊºA³sµ²µ{¦¡®w¡CHD.ASM ªº­ì©l½X¦p¤U¡G

        .586
        .MODEL  FLAT,STDCALL
        OPTION  CASEMAP:NONE

INCLUDE         WINDOWS.INC
INCLUDE         KERNEL32.INC
INCLUDELIB      KERNEL32.LIB

IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS    EQU     560000h

DISK_EXTENT     STRUCT
DiskNumber      DWORD           ?
dwUnknown       DWORD           ?               ;MSDNµL¦¹Äæ¦ì
StartingOffset  LARGE_INTEGER   <>
ExtentLength    LARGE_INTEGER   <>
DISK_EXTENT     ENDS

VOLUME_DISK_EXTENTS     STRUCT
NumberOfDiskExtents     DWORD           ?
dwUnknown               DWORD           ?       ;MSDNµL¦¹Äæ¦ì
Extents                 DISK_EXTENT     ANYSIZE_ARRAY DUP (<>)
VOLUME_DISK_EXTENTS     ENDS

;***************************************************************************************************
.CODE
;---------------------------------------------------------------------------------------------------
;¶i¤J¡þÂ÷¶}°Æµ{¦¡
DLLEntry        PROC    hInstDLL,dwReason,dwReserved
                mov     eax,TRUE        ;¤£»Ý¨Æ«e³B²z©Î¨Æ«á²M²z¡A¬G¶Èªð¦^TRUE
                ret
DLLEntry        ENDP
;---------------------------------------------------------------------------------------------------
;¦¹°Æµ{¦¡¡AGetLogicalDriveFromPhysicalDrive¡A¯à¨ú¨C­Ó¹êÅéµwºÐ¤¤©Ò§tªºÅÞ¿èºÏºÐ¡A¨Ã¶Ç¦^µ¹¥Dµ{¦¡
;¿é¤J¡GlpData¡Ð­n¶Ç¦^ªº¸ê®Æ¦ì§}¡A¦pªGGetLogicalDriveFromPhysicalDrive¥¿½T°õ¦æ¡A·|¦b¦¹¦ì§}¶ñ¤W
;              ®æ¦¡¬°¡u'\\.\PhysicalDrive0',0,'C:',0,'D:',0,
;                      '\\.\PhysicalDrive1',0,'E:',0,0¡v
;                      ªí¥Ü²Ä¤@­Ó¹êÅéµwºÐ(§Y\\.\PhysicalDrive0)¦³¨â­ÓÅÞ¿èºÏºÐ¡AC¡BD
;                      ²Ä¤G­Ó¹êÅéµwºÐ(§Y\\.\PhysicalDrive1)¶È¤@­ÓÅÞ¿èºÏºÐ¡AE
;                      ¥H¡u0,0¡v¬°µ²§À
;              ¦pªG¥Dµ{¦¡¶Ç¨ÓªºlpData¬°¹s¡A¶Ç¦^­ÈEAX¬°©Ò»Ý®e¯Çªº¸ê®Æ¤j¤p(¥H¦ì¤¸²Õ¬°³æ¦ì)
;¿é¥X¡GEAX¡Ð­Y¦¨¥\¡AEAX¬°¶Ç¦^ªº¸ê®Æ¤j¤p(¥H¦ì¤¸²Õ¬°³æ¦ì)¡A¥BlpData©Ò«ü¦ì§}·|¶ñ¤J¥¿½Tªº¸ê®Æ
;           ­Y¥¢±Ñ¡AEAX¡×0
GetLogicalDriveFromPhysicalDrive    PROC    USES esi edi ebx, lpData:DWORD
                LOCAL   aryLogicalDrive[104]:BYTE       ;¨C­ÓÅÞ¿èºÏºÐ¾÷¦WºÙ¬°¡u'C:\',0¡v¡A³Ì¦h26­Ó
                LOCAL   szDeviceName[8]:BYTE            ;'\\.\C:',0
                LOCAL   vde:VOLUME_DISK_EXTENTS
                LOCAL   idx:DWORD       ;©I¥sGetLogicalDriveStrings±o¨ìÅÞ¿èÀx¦s¸Ë¸m¦Wªº°}¦C¯Á¤Þ
                LOCAL   nOut:DWORD      ;©I¥sDeviceIoControl¶Ç¦^Ū¨ú¦h¤Ö¦ì¤¸²Õ
                LOCAL   nData:DWORD     ;hMem¤º©Ò°O¸üªº­«­n¸ê®Æ¤j¤p¡A¥ç§Y­n¶Ç¦^ªº¸ê®Æ¤j¤p¡A¥H¦ì¤¸²Õ¬°³æ¦ì
                LOCAL   nPhysicalDriver:DWORD   ;¹êÅéµwºÐ­Ó¼Æ
                LOCAL   hDevice,hMem:HANDLE
                LOCAL   szPhyDrive[20]:BYTE
                sub     ecx,ecx
                cld
                mov     idx,ecx
                mov     nData,ecx
                mov     nPhysicalDriver,ecx
        ;ªì©l¤ÆszPhyDrive¦r¦ê¡A¥ç§Y¶ñ¤J"\\.\PhysicalDrive0",0
                lea     edi,szPhyDrive
                mov     DWORD PTR [edi],5c2e5c5ch
                mov     DWORD PTR [edi+4],73796850h
                mov     DWORD PTR [edi+8],6c616369h
                mov     DWORD PTR [edi+12],76697244h
                mov     DWORD PTR [edi+16],3065h
        ;ªì©l¤ÆszDeviceName¦r¦ê¡A¥ç§Y¶ñ¤J"\\.\",?,":",0
                lea     edi,szDeviceName
                mov     DWORD PTR [edi],5c2e5c5ch
                mov     WORD PTR [edi+5],3ah
        ;¹w¯d2KBªÅ¶¡¦s©ñ­n¶Ç¦^ªº¸ê®Æ(¨C­ÓÅÞ¿èºÏºÐ¦û3­Ó¦ì¤¸²Õ¡a¦p¡uC:¡v,0¡b¡A°²³]¨C­Ó¹êÅéµwºÐ³£¦³ 26
        ;­ÓÅÞ¿èºÏ°Ï¡A¬G¦û26*3¡×78¦ì¤¸²Õ¡A¦A¥[¤W¡u"\\.\PhysicalDrive1",0¡v¦@19­Ó¦ì¤¸²Õ¡A¬G¨C­Ó¹êÅéµw
        ;ºÐ¦û97­Ó¦ì¤¸²Õ¡A°²³]¦³20­Ó¹êÅéµwºÐ¡A¦@»Ý­n1940­Ó¦ì¤¸²Õ¡A¦ý¦A¥[¤W³Ì«á¤@­Ó0¡A¦³971­Ó¦ì¤¸²Õ¡A
        ;¬°¨D«OÀI¡A¦V¨t²Î­n¨D2KB)
                INVOKE  GlobalAlloc,GPTR,2048
                mov     hMem,eax

                INVOKE  GetLogicalDriveStrings,SIZEOF aryLogicalDrive,ADDR aryLogicalDrive
next_drv:       mov     edx,idx
                lea     esi,aryLogicalDrive
                shl     edx,2
                add     esi,edx
                mov     al,[esi]        ;AL¡×ÅÞ¿èÀx¦s¸Ë¸m¦WºÙ¡A¦p"C"¡B"D"µ¥µ¥
                cmp     al,0
                jz      finished
                lea     edi,szDeviceName
                mov     [edi+4],al
                INVOKE  GetDriveType,esi
        ;­YEAX¡×DRIVE_FIXED¡A«h¦¹ÅÞ¿èºÏºÐ¥i¯à¦bµwºÐ©ÎÀH¨­ºÐ(flash driver)¤W
        .IF eax==DRIVE_FIXED
                INVOKE  CreateFile,ADDR szDeviceName,GENERIC_READ,FILE_SHARE_READ or \
                        FILE_SHARE_WRITE,0,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
                cmp     eax,INVALID_HANDLE_VALUE
                je      error
                mov     hDevice,eax
                INVOKE  DeviceIoControl,hDevice,IOCTL_VOLUME_GET_VOLUME_DISK_EXTENTS,0,0,ADDR vde,\
                        SIZEOF VOLUME_DISK_EXTENTS,ADDR nOut,0
                INVOKE  CloseHandle,hDevice
            ;­pºâÀ³¸Ó§â¦¹ÅÞ¿èºÏºÐ¦s¦bhMemªº­þ­Ó¦ì§}¡A¦¹¦ì§}«e´X­Ó¦ì¤¸²Õ¬°"\\.\PhysicalDrive"¡A­Y
            ;¤£¬Oªº¸Ü¡A«hªí¥Ü¦¹ÅÞ¿èºÏºÐ¬°©|¥¼³Q¦¹°Æµ{¦¡³B²z¹Lªº¹êÅéµwºÐ
                mov     eax,vde.Extents.DiskNumber
                sub     edx,edx
                mov     bl,al
                mov     ecx,97  ;Windows¤¤¡A¨C­Ó¹êÅéµwºÐ³Ì¦h26­ÓÅÞ¿èºÏºÐ¡A¨C­ÓÅÞ¿èºÏºÐ¦û3¦ì¤¸²Õ
                mov     edi,hMem;(¦p¡uC:\,0¡v)¡A¦A¥[¤W¡u\\.\PhysicalDrive?",0¡v¡A¦@97¦ì¤¸²Õ
                mul     ecx
                lea     esi,szPhyDrive
                add     edi,eax
            ;§ä¨ì·sªº¹êÅéµwºÐ¡A§âszPhyDrive¦r¦ê¦s¤JEDI©Ò«ü¦ì§}
            .IF BYTE PTR [edi]==0
                mov     ecx,19
                rep     movsb
                add     [edi-2],bl
                inc     nPhysicalDriver
                jmp     save_driver
            .ENDIF
            ;·j´MhMem¸Ì¡A¦¹¹êÅéµwºÐ"\\.\PhysicalDrive?",0,'C:',0,'D:',0,0,0¡K¡K«á­þ¤@­Ó¦ì§}¬OªÅµÛªº
                mov     al,0
                mov     ecx,97
@@:             repne   scasb
                cmp     BYTE PTR [edi],0
                jne     @b
            ;¨ú±oÅÞ¿èºÏºÐ¦WºÙ¡A¦s©óAL
save_driver:    lea     edx,szDeviceName
                mov     ah,':'
                mov     al,[edx+4]
                stosw
                xor     al,al
                stosb
        .ENDIF
                inc     idx
                jmp     next_drv

;¤w¦bhMem°O¿ý¤F¹êÅéµwºÐ¥]§t­þ¨ÇÅÞ¿èºÏºÐ¡A²{¦b­n§â¥¦¶Çµ¹¥Dµ{¦¡«ü©wªº°O¾ÐÅé¤F
finished:       mov     edi,lpData
                sub     edx,edx
                mov     idx,edx
   .WHILE idx<=20
                mov     eax,97
                sub     edx,edx
                mov     esi,hMem
                mul     idx
                add     esi,eax ;ESI¡×¦bhMemùØ¡A¬Y­Ó¹êÅéµwºÐªº¶}©l¦ì§}
       .IF BYTE PTR [esi]=="\"
                mov     ecx,19
                add     nData,ecx
            .IF edi==0
                add     esi,ecx
            .ELSE
                rep     movsb
            .ENDIF
                xor     ebx,ebx ;EBX¡×ÅÞ¿èºÏºÐ­Ó¼Æ
next_logic_drv: cmp     BYTE PTR [esi],0
                jz      finish_the_phy
                mov     ecx,3
                add     nData,ecx
            .IF edi==0
                add     esi,ecx
            .ELSE
                lodsw
                stosw
                lodsb
                stosb
            .ENDIF
                inc     ebx
                cmp     ebx,26
                jb      next_logic_drv
       .ENDIF
finish_the_phy: inc     idx
   .ENDW
            .IF edi!=0
                mov     BYTE PTR [edi],0
            .ENDIF
                INVOKE  GlobalFree,hMem
                mov     eax,nData
                inc     eax
                jmp     exit
error:          INVOKE  GlobalFree,hMem
                xor     eax,eax
exit:           ret
GetLogicalDriveFromPhysicalDrive    ENDP
;***************************************************************************************************
END     DLLEntry

±N¤W­±¦s¦¨ HD.ASM «á¡A¦A«Ø¥ß¤@­Ó HD.DEF ¯Â¤å¦rÀÉ¡A¤º®e¦p¤U¡G

EXPORTS
        GetLogicalDriveFromPhysicalDrive=GetLogicalDriveFromPhysicalDrive

µM«á¶}±Ò¡u©R¥O´£¥Ü¦r¤¸¡v¡A¿é¤J¤U­±«ü¥O¡G

E:\HomePage\SOURCE\Win32\HD_Info>ml /c hd.asm [Enter]
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: hd.asm

***********
ASCII build
***********


E:\HomePage\SOURCE\Win32\HD_Info>link /DLL /SUBSYSTEM:WINDOWS /DEF:hd.def hd.obj [Enter]

Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

   Creating library hd.lib and object hd.exp

E:\HomePage\SOURCE\Win32\HD_Info>

¨ì¦¹¡A¤w«Ø¥ß¦n HD.DLL °ÊºA³sµ²µ{¦¡®w¤F¡C±µµÛ¦A«Ø¥ß LOGICAL.ASM¡A·í°µ´ú¸Õ HD.DLL ªº´ú¸ÕÀÉ¡ALOGICAL.ASM ­ì©l½X¦p¤U¡G

        .586
        .MODEL  FLAT,STDCALL
        OPTION  CASEMAP:NONE

INCLUDE         WINDOWS.INC
INCLUDE         KERNEL32.INC
INCLUDE         USER32.INC
INCLUDELIB      KERNEL32.LIB
INCLUDELIB      USER32.LIB
INCLUDELIB      HD.LIB

GetLogicalDriveFromPhysicalDrive    PROTO   lpData:DWORD
;*********************************************************************
.DATA
hMem    HANDLE  ?
szData  DB      256 DUP (0)
szTitle DB      'ÅÞ¿èºÏºÐ»P¹êÅéºÏºÐ',0
;*********************************************************************
.CODE
;---------------------------------------------------------------------
start:  INVOKE  GetLogicalDriveFromPhysicalDrive,0
        inc     eax
        INVOKE  GlobalAlloc,GPTR,eax
        mov     hMem,eax
        INVOKE  GetLogicalDriveFromPhysicalDrive,hMem
        mov     edi,OFFSET szData
        mov     esi,hMem
nxtphy: cmp     DWORD PTR [esi],5c2e5c5ch       ;"\\.\"
        jne     error
        add     esi,4
        mov     ecx,14
        rep     movsb
@@:     inc     esi
        cmp     BYTE PTR [esi],'\'
        je      cr_lf
        cmp     BYTE PTR [esi],0
        je      endstr
        mov     al,' '
        stosb
        lodsw
        stosw
        jmp     @b
cr_lf:  mov     ax,0a0dh
        stosw
        jmp     nxtphy
error:
endstr: mov     al,0
        stosb
        INVOKE  MessageBox,0,OFFSET szData,OFFSET szTitle,MB_OK or \
                MB_ICONINFORMATION
        INVOKE  GlobalFree,hMem
        INVOKE  ExitProcess,NULL
;*********************************************************************
END     start

¦b¡u©R¥O´£¥Ü¦r¤¸¡v¡A¿é¤J¤U­±«ü¥O¥H²ÕĶ¨Ã³sµ²¡G

E:\HomePage\SOURCE\Win32\HD_Info>ml logical.asm [Enter]
Microsoft (R) Macro Assembler Version 6.14.8444
Copyright (C) Microsoft Corp 1981-1997.  All rights reserved.

 Assembling: logical.asm

***********
ASCII build
***********

Microsoft (R) Incremental Linker Version 5.12.8078
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.

/SUBSYSTEM:WINDOWS
"logical.obj"
"/OUT:logical.exe"

E:\HomePage\SOURCE\Win32\HD_Info>logical [Enter]

°õ¦æµ²ªG¦p¤U¡G