Win32_Volume_Management (Перебор разделов и Перечисление точек монтирования)

Win32_Volume_Management (Перебор разделов и Перечисление точек монтирования)

post id: 6

post length: 4876

post datetime: 12/9/2009 6:11:12 PM

post ip: 77.51.3.219

Win32_Volume_Management (Перебор разделов и Перечисление точек монтирования)

Дата подачи: 06.12.2008

Win32_Volume_Management (продолжение)

[Kernel32.]

• Перебор разделов.

• Перечисление точек монтирования на разделе.

Example [Visual Basic]:

#Region "Find Volume Mount Point API."

<DllImport("kernel32")> _

Public Shared Function FindFirstVolumeMountPoint( _

ByVal lpszRootPathName As String, _

ByVal lpszVolumeMountPoint As IntPtr, _

ByVal cchBufferLength As Integer _

) As Integer

End Function

<DllImport("kernel32")> _

Public Shared Function FindNextVolumeMountPoint( _

ByVal hFindVolumeMountPoint As Integer, _

ByVal lpszVolumeMountPoint As IntPtr, _

ByVal cchBufferLength As Integer _

) As Boolean

End Function

<DllImport("kernel32")> _

Public Shared Function FindVolumeMountPointClose( _

ByVal hFindVolumeMountPoint As Integer _

) As Boolean

End Function

#End Region

#Region "Find Volume API."

<DllImport("kernel32")> _

Public Shared Function FindFirstVolume( _

ByVal lpszVolumeName As [IntPtr], _

ByVal cchBufferLength As Integer _

) As Integer

End Function

<DllImport("kernel32")> _

Public Shared Function FindNextVolume( _

ByVal hFindVolume As Integer, _

ByVal lpszVolumeName As [IntPtr], _

ByVal cchBufferLength As Integer _

) As Boolean

End Function

<DllImport("kernel32")> _

Public Shared Function FindVolumeClose( _

ByVal hFindVolume As Integer _

) As Boolean

End Function

<DllImport("kernel32")> _

Public Shared Function GetVolumePathNamesForVolumeName( _

ByVal lpszVolumeName As String, _

ByVal lpszVolumePathName As IntPtr, _

ByVal cchBufferLength As Integer, _

ByVal lpcchReturnLength As Integer _

) As Boolean

End Function

#End Region

--------------

При перечислении разделов их имена возвращаются в формате [Guid`а]. Для дальнейшего поиска точек монтирования на этом разделе (если такие есть) их нужно преобразовать в путь. Для этого можно использовать GetVolumePathNamesForVolumeName().

Example [Visual Basic]:

<StructLayout(LayoutKind.Sequential)> _

Structure lpszString

<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=254)> _

Public lpszName As String

End Structure

Sub PrintMountPoint(ByVal ptr As IntPtr)

With Runtime.InteropServices.Marshal.PtrToStructure( _

ptr, GetType(lpszString))

Console.WriteLine(String.Concat(" =>> ", .lpszName))

End With

End Sub

Sub EnimeMountPointS(ByVal lpszRootPathName As String)

Dim Buf As New lpszString

Dim BufSize As Integer = Runtime.InteropServices.Marshal.SizeOf(Buf)

Dim ptr As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(BufSize)

Dim hWnd = Emicrox.Win32_VolumeManagement.FindFirstVolumeMountPoint(lpszRootPathName, ptr, BufSize)

If hWnd > 0 Then

Console.WriteLine(String.Concat(" Mount points:"))

PrintMountPoint(ptr)

Do While Emicrox.Win32_VolumeManagement.FindNextVolumeMountPoint(hWnd, ptr, BufSize) = True

PrintMountPoint(ptr)

Loop

Emicrox.Win32_VolumeManagement.FindVolumeClose(hWnd)

End If

Runtime.InteropServices.Marshal.FreeHGlobal(ptr)

End Sub

Sub PrintVolume(ByVal ptr As IntPtr)

Dim _GUID As String = Runtime.InteropServices.Marshal.PtrToStructure( _

ptr, GetType(lpszString)).lpszName

If Emicrox.Win32_VolumeManagement.GetVolumePathNamesForVolumeName( _

_GUID, ptr, Runtime.InteropServices.Marshal.SizeOf(New lpszString) _

, New Integer) = True Then

With Runtime.InteropServices.Marshal.PtrToStructure(ptr _

, GetType(lpszString))

Console.WriteLine(String.Concat(" ", .lpszName _

, " : ", _

_GUID _

))

EnimeMountPointS(.lpszName)

End With

End If

End Sub

Sub EnimeVolumeS()

Dim Buf As New lpszString

Dim BufSize As Integer = Runtime.InteropServices.Marshal.SizeOf(Buf)

Dim ptr As IntPtr = Runtime.InteropServices.Marshal.AllocHGlobal(BufSize)

Dim hWnd = Emicrox.Win32_VolumeManagement.FindFirstVolume(ptr, BufSize)

If hWnd > 0 Then

Console.WriteLine(String.Concat("Volumes:"))

PrintVolume(ptr)

Do While Emicrox.Win32_VolumeManagement.FindNextVolume(hWnd, ptr, BufSize) = True

PrintVolume(ptr)

Loop

Emicrox.Win32_VolumeManagement.FindVolumeClose(hWnd)

End If

Runtime.InteropServices.Marshal.FreeHGlobal(ptr)

End Sub

<STAThread()> Sub Main()

EnimeVolumeS()

Console.ReadLine()

End Sub

THE END.