引自:http://blog.csdn.net/yizia/archive/2008/06/24/2580744.aspx
在vb.net2005+winxp中,我使用GetPrivateProfileString读取一个ini文件,如果文件路径中含有中文,就会遇到一个奇怪的问题:第一次读取正常,第二次读取就取不到值。代码如下:
- @H_301_20@Private@H_301_20@Declare@H_301_20@FunctionGetPrivateProfileString@H_301_20@Lib"kernel32"@H_301_20@Alias"GetPrivateProfileStringA"(@H_301_20@ByVallpApplicationName@H_301_20@As@H_301_20@String,@H_301_20@ByVallpKeyName@H_301_20@As@H_301_20@String,@H_301_20@ByVallpDefault@H_301_20@As@H_301_20@String,@H_301_20@ByVallpReturnedString@H_301_20@As@H_301_20@String,@H_301_20@ByValnSize@H_301_20@As@H_301_20@Integer,@H_301_20@ByVallpFileName@H_301_20@As@H_301_20@String)@H_301_20@As@H_301_20@Integer
-
- @H_301_20@Privatem_filename@H_301_20@As@H_301_20@String="c:/椅子/slcuserinfo.ini"
-
- @H_301_20@Public@H_301_20@FunctionGetiniValue(@H_301_20@ByVallpKeyName@H_301_20@As@H_301_20@String,@H_301_20@ByValstrName@H_301_20@As@H_301_20@String)@H_301_20@As@H_301_20@String
-
- @H_301_20@DimstrTmp@H_301_20@As@H_301_20@String
-
-
-
-
-
- strTmp=@H_301_20@New@H_301_20@String("",100)
-
-
-
- @H_301_20@CallGetPrivateProfileString(lpKeyName,"",m_filename)
-
-
-
- GetiniValue=Left$(strTmp,InStr(strTmp,vbNullChar)-1)
-
- @H_301_20@End@H_301_20@Function
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 Integer,ByVal lpFileName As String) As Integer
Private m_filename As String = "c:/椅子/slcuserinfo.ini"
Public Function GetiniValue(ByVal lpKeyName As String,ByVal strName As String) As String
Dim strTmp As String
'Dim fileTmp As String
'fileTmp = m_filename
strTmp = New String("",100)
'Call GetPrivateProfileString(lpKeyName,fileTmp)
Call GetPrivateProfileString(lpKeyName,m_filename)
GetiniValue = Left$(strTmp,vbNullChar) - 1)
End Function
反复试验过以后,发现取消注释掉的代码就可以正常运行。百思不得其解,看api的声明没有问题啊,为什么lpFileName会出现异常呢?
不管怎样,这个问题耽误了我2个小时,先贴上故障和解决办法,以后再研究