API函数 GetPrivateProfileString 从INI文件中取汉字的问题?

wang_cel 2002-12-02 12:25:39
API函数 GetPrivateProfileString 从INI文件中取汉字的问题?
取英文没问题,但汉字(我取得是日文,汉字估计差不多)
GetPrivateProfileString(AppName, Key, Default_Renamed, strbuf, strbuf.Length, Filename)

strbuf 显示 "字段 我想取得结果是 "字段" (前面少一个双引号)
strbuf 显示 "FIELDS" (如果是取英文,就没问题)
...全文
287 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
hiyejpgd 2002-12-03
  • 打赏
  • 举报
回复
我試過了,是你寫入時沒有配對寫入,比如你的值為"'我"時,則在ini文件中保存的是'我,但你的值為"'我'"時,則保存的是'我',在這個時候如果你取出ini文件的值,則第一種情況下會是'我,第二種情況會是我,也就是說系統會去除其首尾的引號(配對時)
根據以上結論,則你是在寫入ini文件時的值出了問題,可能寫成了'"我',或是""我",你再檢查一下!
wang_cel 2002-12-02
  • 打赏
  • 举报
回复
运行到
lngRet = GetPrivateProfileString(AppName, Key, Default_Renamed, strbuf, strbuf.Length, Filename)

这一步时, strbuf 显示 "字段 应该是"字段",因为后面我要用它和一个字符串比较,比如 "字段" 比较时,就显示不相等了,而实际是应该相等的.
wang_cel 2002-12-02
  • 打赏
  • 举报
回复
声明如下:
Public 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 Int32, ByVal lpFileName As String) As Int32

调用如下:
Public Function Get_PrivateProfileString(ByVal AppName As String, ByVal Key As String, ByVal Default_Renamed As String, ByVal Filename As String) As String

Const cintBufSZ As Short = 256

Dim lngRet As Integer
Dim strbuf As String

'strbuf = Space(cintBufSZ)
strbuf = New String(CChar(" "), 256)

lngRet = GetPrivateProfileString(AppName, Key, Default_Renamed, strbuf, strbuf.Length, Filename)
Get_PrivateProfileString = AnsiLeftB(strbuf, lngRet)


End Function
wang_cel 2002-12-02
  • 打赏
  • 举报
回复
汉字是能取得,但还是前面有双引号,后面没有(是在跟踪调试的情况下,只显示
"我 ,而不是显示 "我")
hiyejpgd 2002-12-02
  • 打赏
  • 举报
回复
如果數據是字串"我",則寫入時其雙引號不寫入文件,如果是"'我'",則寫入時會有單引號,但讀出時都會只是一個2個字節長度的結果,也就是說系統會舍棄其配對引號
hiyejpgd 2002-12-02
  • 打赏
  • 举报
回复
第二個32767可以改為tSize變量,聲明如下:
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 Short, ByVal lpFileName As String) As Short
hiyejpgd 2002-12-02
  • 打赏
  • 举报
回复
我成功了,能取到漢字:
函數如下:
Public Function ReadIniValue(ByVal FileName As String, ByVal ApplicationName As String, ByVal KeyName As String) As String
Try
Dim tString As String
Dim tSize As Short
tString = New String(CChar(""), 2)
tSize = GetPrivateProfileString(ApplicationName, KeyName, "", tString, 32767, FileName)
tString = New String(CChar(""), tSize)
GetPrivateProfileString(ApplicationName, KeyName, "", tString, 32767, FileName)
Return tString
Catch ex As Exception
ReadIniValue = Nothing
'RaiseEvent Erroring(ex)
End Try
End Function
wang_cel 2002-12-02
  • 打赏
  • 举报
回复
to Montaque(Rainman):
还是不行,运行到
lngRet = GetPrivateProfileString(AppName, Key, Default_Renamed, strbuf, strbuf.Length, Filename)

这一步时, strbuf 显示 "字段 应该是"字段",因为后面我要用它和一个字符串比较,比如 "字段" 比较时,就显示不相等了,而实际是应该相等的.

Get_PrivateProfileString = AnsiLeftB(strbuf, lngRet)这一句有没有都一样.
Montaque 2002-12-02
  • 打赏
  • 举报
回复
或者
Get_PrivateProfileString = AnsiLeftB(strbuf, lngRet+1)
Montaque 2002-12-02
  • 打赏
  • 举报
回复
Get_PrivateProfileString = AnsiLeftB(strbuf, lngRet)
改为
Get_PrivateProfileString=strbuf
wang_cel 2002-12-02
  • 打赏
  • 举报
回复
???????????????????????????

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧