Problem with Win API function GetPrivateProfileString.
Author |
Message |
Trupti Saman #1 / 8
|
 Problem with Win API function GetPrivateProfileString.
I am using Win 32 API function. Dim buffer As String section, key, INFILE, DefaultFind all of these are string. GetPrivateProfileString(section, key, DefaultFind, buffer, 80, INIFILE) VB is crashing right after this statement. After some research I found out that "buffer" is the problem. When I pass buffer as VarPtr(buffer) VB did not crash but I did not got the Value. How to solve this Problem? Thanks Trupti
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
 |
Tom E #2 / 8
|
 Problem with Win API function GetPrivateProfileString.
Don't use VarPtr (or StrPtr for that matter). As long as you pass the strings ByVal, VB will pass a compatible pointer. Check your declaration and make sure all the string args are declared ByVal As String. Should be something like: 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 Long, ByVal lpFileName As String) As Long Also you must pre-allocate the buffer so it's large enough to hold the returned string: buffer = String(255,0) ...or... buffer = Space(255) Note there's no way to determine the required buffer size beforehand, but if the function returns nSize-1, then the buffer is too small.
Quote: >I am using Win 32 API function. > Dim buffer As String > section, key, INFILE, DefaultFind all of these are string. >GetPrivateProfileString(section, key, DefaultFind, buffer, 80, INIFILE) >VB is crashing right after this statement. After some research I found out >that "buffer" is the problem. >When I pass buffer as VarPtr(buffer) VB did not crash but I did not got the >Value. > How to solve this Problem? >Thanks >Trupti
-Tom (please post replies to the newsgroup)
|
Fri, 21 Mar 2003 03:00:00 GMT |
|
 |
Hugo de Vreug #3 / 8
|
 Problem with Win API function GetPrivateProfileString.
Quote: > I am using Win 32 API function. > Dim buffer As String > section, key, INFILE, DefaultFind all of these are string. > GetPrivateProfileString(section, key, DefaultFind, buffer, 80, INIFILE) > VB is crashing right after this statement. After some research I found out > that "buffer" is the problem. > When I pass buffer as VarPtr(buffer) VB did not crash but I did not got the > Value. > How to solve this Problem? > Thanks > Trupti
Trupti, Use Dim buffer as String * 80 instead. Regards, Hugo.
|
Sat, 22 Mar 2003 13:39:55 GMT |
|
 |
Robbert te Riel #4 / 8
|
 Problem with Win API function GetPrivateProfileString.
Long ago there was an error in the API Viewer with the declaration of GetPrivateProfileString, Check the declaration with ie. MSDN
Quote: > I am using Win 32 API function. > Dim buffer As String > section, key, INFILE, DefaultFind all of these are string. > GetPrivateProfileString(section, key, DefaultFind, buffer, 80, INIFILE) > VB is crashing right after this statement. After some research I found out > that "buffer" is the problem. > When I pass buffer as VarPtr(buffer) VB did not crash but I did not got the > Value. > How to solve this Problem? > Thanks > Trupti
|
Sun, 23 Mar 2003 03:00:00 GMT |
|
 |
Michel Gingra #5 / 8
|
 Problem with Win API function GetPrivateProfileString.
Hi Trupti This is the function I use to retrieve a string from an INI file. This has been working for some time and never had any problems with it. Private Function GetProfileString(ByVal v_sSectionName As String, _ ByVal v_sKeyName As String, _ ByVal v_sIniFileName As String, _ ByRef r_sReturnedStr As String) As Long Dim lSize As Long lSize = 256 GetProfileString = 0 r_sReturnedStr = "" lSize = GetPrivateProfileString(v_sSectionName, _ v_sKeyName, _ "", _ r_sReturnedStr, _ lSize, _ v_sIniFileName) + 2 If (CBool(lSize - 2)) Then '// Fill the returned string variable with spaces r_sReturnedStr = Space$(lSize) '// Read the actual string... lSize = GetPrivateProfileString(v_sSectionName, _ v_sKeyName, _ "", _ r_sReturnedStr, _ lSize, _ v_sIniFileName) r_sReturnedStr = Left$(r_sReturnedStr, lSize) GetProfileString = lSize End If End Function There you go. --
Quote: > I am using Win 32 API function. > Dim buffer As String > section, key, INFILE, DefaultFind all of these are string. > GetPrivateProfileString(section, key, DefaultFind, buffer, 80, INIFILE) > VB is crashing right after this statement. After some research I found out > that "buffer" is the problem. > When I pass buffer as VarPtr(buffer) VB did not crash but I did not got the > Value. > How to solve this Problem? > Thanks > Trupti
|
Mon, 24 Mar 2003 03:00:00 GMT |
|
 |
Patrick Spenc #6 / 8
|
 Problem with Win API function GetPrivateProfileString.
PMFJI, but why are you calling the API function twice??
Quote: > Hi Trupti > This is the function I use to retrieve a string from an INI file. > This has been working for some time and never had any > problems with it. > Private Function GetProfileString(ByVal v_sSectionName As String, _ > ByVal v_sKeyName As String, _ > ByVal v_sIniFileName As String, _ > ByRef r_sReturnedStr As String) As Long > Dim lSize As Long > lSize = 256 > GetProfileString = 0 > r_sReturnedStr = "" > lSize = GetPrivateProfileString(v_sSectionName, _ > v_sKeyName, _ > "", _ > r_sReturnedStr, _ > lSize, _ > v_sIniFileName) + 2 > If (CBool(lSize - 2)) Then > '// Fill the returned string variable with spaces > r_sReturnedStr = Space$(lSize) > '// Read the actual string... > lSize = GetPrivateProfileString(v_sSectionName, _ > v_sKeyName, _ > "", _ > r_sReturnedStr, _ > lSize, _ > v_sIniFileName) > r_sReturnedStr = Left$(r_sReturnedStr, lSize) > GetProfileString = lSize > End If > End Function > There you go. > --
> > I am using Win 32 API function. > > Dim buffer As String > > section, key, INFILE, DefaultFind all of these are string. > > GetPrivateProfileString(section, key, DefaultFind, buffer, 80, INIFILE) > > VB is crashing right after this statement. After some research I found out > > that "buffer" is the problem. > > When I pass buffer as VarPtr(buffer) VB did not crash but I did not got > the > > Value. > > How to solve this Problem? > > Thanks > > Trupti
|
Fri, 04 Apr 2003 03:00:00 GMT |
|
 |
James M. Parke #7 / 8
|
 Problem with Win API function GetPrivateProfileString.
Most likely, the first pass captures the size of the data that will be returned in the second pass. - M2CW
Quote: > PMFJI, but why are you calling the API function twice??
> > Hi Trupti > > This is the function I use to retrieve a string from an INI file. > > This has been working for some time and never had any > > problems with it. > > Private Function GetProfileString(ByVal v_sSectionName As String, _ > > ByVal v_sKeyName As String, _ > > ByVal v_sIniFileName As String, _ > > ByRef r_sReturnedStr As String) As Long > > Dim lSize As Long > > lSize = 256 > > GetProfileString = 0 > > r_sReturnedStr = "" > > lSize = GetPrivateProfileString(v_sSectionName, _ > > v_sKeyName, _ > > "", _ > > r_sReturnedStr, _ > > lSize, _ > > v_sIniFileName) + 2 > > If (CBool(lSize - 2)) Then > > '// Fill the returned string variable with spaces > > r_sReturnedStr = Space$(lSize) > > '// Read the actual string... > > lSize = GetPrivateProfileString(v_sSectionName, _ > > v_sKeyName, _ > > "", _ > > r_sReturnedStr, _ > > lSize, _ > > v_sIniFileName) > > r_sReturnedStr = Left$(r_sReturnedStr, lSize) > > GetProfileString = lSize > > End If > > End Function > > There you go. > > --
> > > I am using Win 32 API function. > > > Dim buffer As String > > > section, key, INFILE, DefaultFind all of these are string. > > > GetPrivateProfileString(section, key, DefaultFind, buffer, 80, INIFILE) > > > VB is crashing right after this statement. After some research I found > out > > > that "buffer" is the problem. > > > When I pass buffer as VarPtr(buffer) VB did not crash but I did not got > > the > > > Value. > > > How to solve this Problem? > > > Thanks > > > Trupti
|
Sun, 06 Apr 2003 03:00:00 GMT |
|
 |
MP #8 / 8
|
 Problem with Win API function GetPrivateProfileString.
On Wed, 18 Oct 2000 19:14:46 -0400, "James M. Parker" Quote:
>Most likely, the first pass captures the size of the data that will be >returned in the second pass.
Not nessecary Public Function GetProf(ByVal section As String, ByVal variable As String, ByVal iniFile As String) As String Dim buf As String buf = Space(64) ' its an ini entry not a paperback novel Dim mA As Integer mA = GetPrivateProfileString(section & Chr$(0), variable & Chr$(0), Chr$(0), buf$, Len(buf$), iniFile & Chr$(0)) GetProf = Left$(buf$, mA) End Function works fine
|
Mon, 07 Apr 2003 11:38:02 GMT |
|
|
|