【问题标题】:VBA GetPrivateProfileStringVBA GetPrivateProfileString
【发布时间】:2017-11-02 08:47:19
【问题描述】:

我想读取带有GetPrivateProfileString 的INI 文件。
问题是我的字符串包含一些不应该存在的字符

我认为这个问题来自path = String(255,0),但如果我不写这个,我的 Excel 程序就会停止工作。

有没有办法绕过这个问题或者我的代码有问题?

Dim path as String,m2_path as string
path = String(255, 0)
m2_path = String(255, 0)

nc = GetPrivateProfileString("PATH", "path1", "", path, 255, inipath)
nc = GetPrivateProfileString("PATH", "path2", "", m2_path, 255, inipath)

功能

Private Declare 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

INI 文件

[PATH]
path1="G:\Arbeit\gen molding"
...

【问题讨论】:

    标签: excel vba ini


    【解决方案1】:

    值 nc 告诉你字符串中有多少个字符。问题是实际的接口是一个字符数组:不是字符串。所以你要做的就是给它你可能得到多少个字符的最佳猜测(nsize),它会告诉你有多少个字符。

    dim path1 as string, path2 as string, path as string
    
    path = string(255, 0)
    nc = GetPrivateProfileString("PATH", "path1", "", path, 255, inipath)
    path1 = left(path, nc)
    nc = GetPrivateProfileString("PATH", "path2", "", m2_path, 255, inipath)
    path2 = left(path, nc)
    

    【讨论】:

    • 我建议使用Len(path) 作为第五个参数而不是文字255 以避免缓冲区溢出和OP 的“我不写这个,我的Excel 程序只是停止工作。”
    猜你喜欢
    • 2014-03-03
    • 2010-09-12
    • 2013-07-02
    • 2012-11-14
    • 2012-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多