本文介绍了以下元帅代码几乎可行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 以下几乎可以正常工作。 问题是Marshal.PtrToStringAuto似乎终止于第一个null 所以我没有得到完整的字符串。 /> 有关如何解决这个问题的建议吗? 或者如何改进代码? 谢谢 PS我添加了+1,因为据我了解GetLogicalDriveStrings doc ,最后将为Unicode提供一个不计数的null。同意? Dim lDrives As String Dim lLenOflDrives As Integer Dim lPointerToMemory as IntPtr lLenOflDrives = Kernel.GetLogicalDriveStrings(0,Nothing) Dim lDrivesSB作为新的StringBuilder(lLenOflDrives + 1) lDrivesSB.Insert(0,ChrW(33),lLenOflDrives + 1 ) lPointerToMemory = Marshal.StringToHGlobalAuto(lDrivesSB.ToString) ''获取dirves列表。返回一个:\< null> c:\< null>< null> 如果Kernel.GetLogicalDriveStrings(lLenOflDrives,lPointerToMemory)那么 ''会如果宣布了GetLogicalDriveStrings则需要使用PtrToStringAnsi CharSet.Ansi lDrives = Marshal.PtrToStringAuto(lPointerToMemory,lLenOflDrives + 1)''文字 to first null,例如a:\ Marshal.FreeHGlobal(lPointerToMemory) ''查看aDrive是否在列表中 DriveExists = InStr(1,lDrives,drive,CompareMethod.Text) 结束如果 The following almost works.The problem is Marshal.PtrToStringAuto seems to terminate at the first nullso 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 docthere will be an uncounted null at the end for Unicode. Agree? Dim lDrives As StringDim lLenOflDrives As IntegerDim lPointerToMemory As IntPtrlLenOflDrives = 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 declaredCharSet.AnsilDrives = Marshal.PtrToStringAuto(lPointerToMemory, lLenOflDrives + 1) ''Textto first null, e.g, a:\Marshal.FreeHGlobal(lPointerToMemory)''See if aDrive is in the listDriveExists = InStr(1, lDrives, drive, CompareMethod.Text)End If推荐答案 如果你改变了你的GetLogicalDriveStrings声明,以便最后的 参数是一个Char(),你可以这样简单地调用它。 Dim buffer(lLenOflDrives - 1)作为Char 如果Kernel.GetLogicalDriveStrings(lLenOflDrives,buffer)> 0然后 Mattias - Mattias Sj?gren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com 请回复到新闻组。 If you change your GetLogicalDriveStrings declaration so that the lastparameter is a Char(), you can simply call it like this Dim buffer(lLenOflDrives - 1) As CharIf Kernel.GetLogicalDriveStrings(lLenOflDrives, buffer) > 0 Then Mattias --Mattias Sj?gren [MVP] mattias @ mvps.org http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.comPlease reply only to the newsgroup. 这篇关于以下元帅代码几乎可行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-27 02:12