J
Just Me
The following almost works.
The problem is Marshal.PtrToStringAuto seems to terminate at the first null
so I don't get the full string.
Any suggestions on how to fix this?
Or how to improve the code?
Thanks
PS I added the +1 because as I understand the GetLogicalDriveStrings doc
there will be an uncounted null at the end for Unicode. Agree?
Dim lDrives As String
Dim lLenOflDrives As Integer
Dim lPointerToMemory As IntPtr
lLenOflDrives = Kernel.GetLogicalDriveStrings(0, Nothing)
Dim lDrivesSB As New StringBuilder(lLenOflDrives + 1)
lDrivesSB.Insert(0, ChrW(33), lLenOflDrives + 1)
lPointerToMemory = Marshal.StringToHGlobalAuto(lDrivesSB.ToString)
'Get the list of dirves. Returns a:\<null>c:\<null><null>
If Kernel.GetLogicalDriveStrings(lLenOflDrives, lPointerToMemory) Then
'Would need to use PtrToStringAnsi if GetLogicalDriveStrings were declared
CharSet.Ansi
lDrives = Marshal.PtrToStringAuto(lPointerToMemory, lLenOflDrives + 1) 'Text
to first null, e.g, a:\
Marshal.FreeHGlobal(lPointerToMemory)
'See if aDrive is in the list
DriveExists = InStr(1, lDrives, drive, CompareMethod.Text)
End If
The problem is Marshal.PtrToStringAuto seems to terminate at the first null
so I don't get the full string.
Any suggestions on how to fix this?
Or how to improve the code?
Thanks
PS I added the +1 because as I understand the GetLogicalDriveStrings doc
there will be an uncounted null at the end for Unicode. Agree?
Dim lDrives As String
Dim lLenOflDrives As Integer
Dim lPointerToMemory As IntPtr
lLenOflDrives = Kernel.GetLogicalDriveStrings(0, Nothing)
Dim lDrivesSB As New StringBuilder(lLenOflDrives + 1)
lDrivesSB.Insert(0, ChrW(33), lLenOflDrives + 1)
lPointerToMemory = Marshal.StringToHGlobalAuto(lDrivesSB.ToString)
'Get the list of dirves. Returns a:\<null>c:\<null><null>
If Kernel.GetLogicalDriveStrings(lLenOflDrives, lPointerToMemory) Then
'Would need to use PtrToStringAnsi if GetLogicalDriveStrings were declared
CharSet.Ansi
lDrives = Marshal.PtrToStringAuto(lPointerToMemory, lLenOflDrives + 1) 'Text
to first null, e.g, a:\
Marshal.FreeHGlobal(lPointerToMemory)
'See if aDrive is in the list
DriveExists = InStr(1, lDrives, drive, CompareMethod.Text)
End If