API following statement as the statement :( MSDN) Declare Function GetPrivateProfileStringKey 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
The call is as follows: ... DIM STRBUFFER AS NEW STRING (Cchar ("), 50) DIM RET AS Long Ret = GetPrivateProfileStringKey (cfg_group_net, cfg_key_server_ip, _ default_server_ip, strbuffer, 50, m_filepath)
GetServerip = Left (strbuffer, instr (strbuffer, chr (0)) - 1) ......
Among them, all currently written for its own defined global string;
As a result, the call failed, the key value in the configuration file is not possible. WritePrivateProfileString is successful. The problem is really strange! I have seen some posts, I think there is two points: 1. Some post points out the return value in the API declaration Should I be int32? Go to the test;
2. INI file path I use as follows: m_filepath = Application.startupPath "/" CFG_FILE_NAME does not use "//" to partition the problem caused by the path? But it is estimated that the problem is not here because it is called in WritePrivateProfileString. This path is also used, it is successful.
I hope that my friends can help, thank you! Haha, you, the problem is finally solved, there should be two reasons for this error: 1. Microsoft's help documentation is not complete;
Please refer to Microsoft's help documentation: http://msdn.microsoft.com/library/chs/default.asp? Url = / library / coup / vbcon / html / vbconupgraderecommendationAdjustDataTypesforwin32apis.asp
Among them:
"Many APIs can be used correctly as in Visual Basic 6.0, but it is necessary to adjust the data type accordingly. Currently, Visual Basic 6.0's long data type is the INTEGER data type of Visual Basic .NET, and Visual Basic 6.0 Integer Data Types for Visual Basic .NET SHORT data type. During the upgrade, these changes will be automatically completed, simple API is exactly the same as in Visual Basic 6.0. "
Ok, this is no problem, saying is very good, and it is clear, but the problem is behind it:
"There are three cases that may need to make some changes. The first case is when transmitting a user-defined type containing a fixed length string or byte array. In Visual Basic .NET, you may need to change the code to the user-defined type. Each set of cells or bytes (from system.Runtime.InterOpServices) is added to the byte arrays. The second case is that this is not supported when using the AS Any variable type in the DECLARE statement. This is not supported. AS Any type variable is usually used to pass a string or NULL variable; you can replace this Visual Basic 6.0 by declaring the two format APIs (one is long, one for string). For example, the GetPrivateProfileString API has a As Any type of parameters lpKeyName: Private Declare 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 can remove "as any" by replacing Declare with two versions, one of which is long, a version accepts a string:
Private Declare Function GetPrivateProfileStringKey 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
Private Declare Function GetPrivateProfileStringNullKey Lib "kernel32" _Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ByVal lpKeyName As Long, ByVal lpDefault As String, ByVal _lpReturnedString As String, ByVal nSize As Long, ByVal lpFileName _As String) As Long "
I don't know if you find that there is no, I have already mentioned the type of long data type for Visual Basic 6.0 as the INTEGER data type of Visual Basic .NET, but in the next getprivateprofilestring example, such a problem is made, VB6's getPrivateProfileString parameter declaration Byval nszie as long and function returns in VB.NET, but also a long type, no correction is Integer !!!! That is to say that Microsoft has led a very mentally wrong mistake, it should be misleaded a lot of development By;
2. My VB skill is still lacking; so it is not possible to discover the clues in time, for the details of VB6 to VB.NET migration;
Finally, the correct statement VB.net given GetPrivateProfileString the Declare Function WritePrivateProfileString Lib "kernel32" Alias _ "WritePrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String) As Integer
Declare Function GetPrivateProfileStringKey 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
Declare Function GetPrivateProfileStringNullKey Lib "kernel32" _ Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, _ ByVal lpKeyName As Long, ByVal lpDefault As String, ByVal _ lpReturnedString As String, ByVal nSize As Integer, ByVal _ lpFileName As String) As Integer
Note that the original VB6long parameter type must be corrected to an Integer type.