GETPROFILESTRING正在崩溃

时间:2019-07-16 17:32:17

标签: vba ms-word

当我运行Read_Click()时,下面的VBA代码崩溃。但是Write_Click正在工作。

Private Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" 
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Declare PtrSafe Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" 
(ByVal lpApplicationName As String, ByVal lpKeyName As Any, ByVal lpString As Any, ByVal lpFileName As String) As Long

Private Sub Write_Click()
 WritePrivateProfileString "Parameters", "Autolaunch", "1", "D:\Test.ini"
End Sub

Private Sub Read_Click()
    Dim buffer As String, IniData As String
     buffer = String(255, Chr(0))
     MsgBox Left(buffer, GetPrivateProfileString("Parameters", "Autolaunch", "1", 
 buffer, Len(buffer), "D:\Test.ini"))
End Sub

Windows 10-X64 / Office 365-X64

1 个答案:

答案 0 :(得分:0)

我遇到了同样的问题,为我解决的问题似乎是在 GetPrivateProfileString 定义中将 lpKeyName 数据类型从 Any 切换为 String:

Private Declare PtrSafe Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
             (ByVal lpApplicationName As String, _
               ByVal lpKeyName As String, _
               ByVal lpDefault As String, _
               ByVal lpReturnedString As String, _
               ByVal nSize As Long, _
               ByVal lpFileName As String) As Long