10-01-2001, 06:10 AM
Thanx for the help with the last post. My next question is about this API function call. I need to get the size of a string, but before that i need to set it. I have forgotten how to set the size of a string when you dim it, can you refresh my memory please?
Also does anybody know why (under NT4) VB crashes when you set the size of the string in the Function call to 255? or is it just my machine.
10-01-2001, 06:20 AM
String size is automatically determined by its contents so the easy way to do it is with the Space function:
<pre>Dim retStr as String, retLen as Long
retStr = Space(256)
retLen = GetPrivateProfileString("SectionName", "ItemName", "DefaultText", retStr, Len(retStr), "appname.ini")
retStr = Left(retStr, retLen)</pre>retStr now contains the retrieved text.
The reason for the crash is that if you didn't specify the Space function line in the above code then the memory allocated for the string is for a string of length zero. When you specified a length of 255 the GetPrivateProfileString function started overwriting protected memory and therefore crashed VB.<P ID="edit"><FONT class="small"><EM>Edited by Banjo on 10/01/01 07:23 AM.</EM></FONT></P>
you can also set the length with the following syntax
<pre>Dim strString As String * 3</pre> which will limit it to 3 chars
10-01-2001, 06:28 AM
The problem with that is that (in you example) if you had a two character string then you would have to continuously remember to use Trim to get rid of the extra space.