简体   繁体   English

使用Visual Basic读取Unicode INI文件

[英]Using Visual Basic to read a Unicode INI file

Reading INI values from a Unicode INI document with Visual Basic. 使用Visual Basic从Unicode INI文档中读取INI值。

I thought that this was correct: 我认为这是正确的:

Private Class NativeMethods
    ' AJT 2014.11.28 Use the unicode function !!!
    <DllImport("kernel32", CharSet:=CharSet.Unicode)>
    Public Shared Function WritePrivateProfileString(section As String, key As String, val As String, filePath As String) As Boolean
    End Function

    ' AJT 2014.11.28 Use the unicode function !!!
    <DllImport("kernel32", CharSet:=CharSet.Unicode)>
    Public Shared Function GetPrivateProfileString(section As String, key As String, def As String, retVal As StringBuilder, size As Integer, filePath As String) As Integer
    End Function
End Class

But I am getting question marks when reading Russian data from the INI file. 但是从INI文件读取俄语数据时出现问号。

Then I stumbled over this which defines: 然后我偶然发现了这个定义:

 Private Declare Function GetPrivateProfileString _ Lib "kernel32" Alias "GetPrivateProfileStringA" _ (ByVal lpSectionName As String, _ ByVal lpKeyName As Any, _ ByVal lpDefault As String, _ ByVal lpbuffurnedString As String, _ ByVal nBuffSize As Long, _ ByVal lpFileName As String) As Long 

I noticed they used GetPrivateProfileStringA which makes me think I might need to use GetPrivateProfileStringW but now I am confused, as I am using: 我注意到他们使用了GetPrivateProfileStringA ,这使我认为我可能需要使用GetPrivateProfileStringW但是现在我很困惑,因为我正在使用:

<DllImport("kernel32", CharSet:=CharSet.Unicode)>

Please help. 请帮忙。

The link you've provided predates .NET. 您提供的链接早于.NET。 What you're looking at (the Lib "kernel32" Alias "GetPrivateProfileStringA" syntax) is in fact the old way of importing unmanaged types and so on in VB6 , etc. This syntax will still work in VB.NET, but it is superseded by the DllImport functionality. 您正在查看的内容( Lib "kernel32" Alias "GetPrivateProfileStringA"语法)实际上是在VB6等中导入非托管类型等的旧方法。此语法在VB.NET中仍然适用,但是已被取代通过DllImport功能实现。 If using this older way of doing things, you are correct in that you would need to specify "GetPrivateProfileStringW" rather than "GetPrivateProfileStringA" . 如果使用这种较旧的处理方式,则正确的做法是,您需要指定"GetPrivateProfileStringW"而不是"GetPrivateProfileStringA"

With regards to the problem you're experiencing, Russian characters may not be the actual cause of your problem; 对于您遇到的问题,俄语字符可能不是您出现问题的真正原因; it may be that you are simply getting junk data back. 可能是您只是在获取垃圾数据。 This can happen if the parameters for your DllImport method are not defined using the correct types for one or more of the parameters. 如果未使用一个或多个参数的正确类型定义DllImport方法的参数,则会发生这种情况。

Try taking a look at the following links. 尝试看看以下链接。 The PInvoke website usually contains useful information on how to correctly define the method signatures: https://www.pinvoke.net/default.aspx/kernel32.getprivateprofilestring https://www.pinvoke.net/default.aspx/kernel32.writeprivateprofilestring PInvoke网站通常包含有关如何正确定义方法签名的有用信息: https : //www.pinvoke.net/default.aspx/kernel32.getprivateprofilestring https://www.pinvoke.net/default.aspx/kernel32.writeprivateprofilestring

For the sake of completeness, I've added these definitions below (copied directly from the links above, although I confess I have not tested them myself). 为了完整起见,我在下面添加了这些定义(直接从上面的链接复制,尽管我承认我自己尚未对其进行测试)。 If you are still having difficulty with this, it may be worth updating your answer with an example of the INI file content you are trying to read. 如果您仍然对此感到困难,则可能需要使用您尝试读取的INI文件内容示例来更新答案。

GetPrivateProfileString(): GetPrivateProfileString():

DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                        ByVal lpKeyName As String, _
                        ByVal lpDefault As String, _
                        ByVal lpReturnedString As StringBuilder, _
                        ByVal nSize As Integer, _
                        ByVal lpFileName As String) As Integer
End Function

WritePrivateProfileString(): WritePrivateProfileString():

<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Unicode)> _
Private Shared Function GetPrivateProfileString(ByVal lpAppName As String, _
                        ByVal lpKeyName As String, _
                        ByVal lpDefault As String, _
                        ByVal lpReturnedString As StringBuilder, _
                        ByVal nSize As Integer, _
                        ByVal lpFileName As String) As Integer
End Function

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM