Results 1 to 22 of 22

Thread: returned ini values have ||||||| at end

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830

    returned ini values have ||||||| at end

    I am pulling info from a .ini file and the value in a variable looks like this:
    VALUE||||||||||||||

    What are those | things and can I just do a replace | with nothing?

    Thanks.

  2. #2
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    Trim it

    They are junk, and Trim should remove them

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Already doing this and not triming it off.

    Code:
    gsLocation = Trim(Temp)

  4. #4
    jim mcnamara
    Guest
    It's Ascii 124 - a pipe sometimes called 'logical or' symbol

    It's on the Keyboard, usually on the \ (backslash) key

    Code:
    mystr = "VALUE||||||"
    mystr=Replace(mystr,chr124),"")

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497
    That's Chr(124)

  6. #6
    jim mcnamara
    Guest
    PS: are you using GetPrivateProfileString or some other api to read the INI file? That works better.

  7. #7

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    The following doesn't work either:
    gsLocation = Replace(Trim(Temp), Chr(124), "")

    HERE IS THE WHOLE SECTION OF CODE:
    Dim ret As Long
    Dim Temp As String * 50
    Dim sLoc As String

    sLoc = "c:\windows\File.ini"

    ret = GetPrivateProfileString("Location", "FullPath", sLoc, Temp, Len(Temp), sLoc)

    If ret = 0 Then
    Beep
    Else
    gsLocation = Replace(Trim(Temp), Chr(124), "")
    End If
    .....

  8. #8
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    real quick do this

    msgbox Right(gsLocation,1)


    tell me the messagebox says
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  9. #9
    jim mcnamara
    Guest
    I give up. Something else is going on. Replace works just fine when I try it on my machine here.

  10. #10
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    after setting gsLocation of course
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  11. #11

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Cander - nothing appears so I am going to say it's a space maybe or could it be null?

  12. #12
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    a null .
    here try this

    Code:
    Do Until Asc(Right(Temp, 1)) <> 0
        Temp = Left(Temp, Len(strName) - 1)
        DoEvents
    Loop
    
    gsLocation = Temp
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  13. #13
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    oops change strName to Temp
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  14. #14

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Cander - That didn't work either.

  15. #15
    Banished Cander's Avatar
    Join Date
    Dec 2000
    Location
    Why do you care?
    Posts
    6,913
    arggh. well im tapped out of ideas
    Stack Overflow
    See the features of Visual Studio 2010 and C# 4.0: The 10-4 show on Channel9

  16. #16
    jim mcnamara
    Guest
    Join the club. He's doing something else somewhere or he's working with hosed data.

    Oh. wait. Hosed data - he isn't init - ing the temp string
    Code:
    Dim Temp as String *50
    'add
    Temp = string(chr(0), 50) ' initilaize it to all chr(0)
    ....
    After you get a RETURN
    
    
    Temp = left(temp,instr(temp,chr(0)-1)
    See if that doesn't help

  17. #17

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Give me a type mismatch error on this line.

    Temp = string(chr(0), 50) ' initilaize it to all chr(0)

  18. #18
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    Do you need to use a fixed length string?

    That is causing all your problems

  19. #19

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Yeah, I hear you on that one. I tried without and it doesn't like that for some reason.

    Here is where I got info on it:
    http://www.vb-world.net/files/tip17.html

  20. #20
    jim mcnamara
    Guest
    I inverted the arguments. If you can code you can fix it, too.



    Jeez Louise.

    Code:
    Temp = string(50,vbNull) '  it to all chr(0)
    This is why you have the trailing crud. GetPRivateProfileString uses null-terminated string because it is written in C

  21. #21
    Frenzied Member ae_jester's Avatar
    Join Date
    Jun 2001
    Location
    Kitchener Ontario Canada Earth
    Posts
    1,545
    This is what I always use and it works without problems.

    VB Code:
    1. 'Add this stuff to a module
    2.  
    3. Public Declare Function WritePrivateProfileString Lib "kernel32" Alias "WritePrivateProfileStringA" _
    4. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    5. ByVal lpString As Any, ByVal lpFileName As String) As Long
    6.  
    7. Public Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" _
    8. (ByVal lpApplicationName As String, ByVal lpKeyName As Any, _
    9. ByVal lpDefault As String, ByVal lpReturnedString As String, _
    10. ByVal nSize As Long, ByVal lpFileName As String) As Long
    11.  
    12. Sub ReadIniFile(SectorName As String, KeyName As String, Inifile As String)
    13. 'Param1
    14. 'The header of the INI file section the value is in.
    15. 'Param2
    16. 'The name of the value to read.
    17. 'Param3
    18. 'The value to return if a valid value cannot be read. Make it something that would definitely not be read, such as "(error)".
    19. 'Param4
    20. 'A fixed-length string that will receive either the string read from the file or lpDefault.
    21. 'Param5
    22. 'The length in characters of lpReturnedString.
    23. 'Param6
    24. 'The filename of the INI file to read from
    25.  
    26. Dim uname As String ' receives the value read from the INI file
    27. Dim slength As Long ' receives length of the returned string
    28. uname = Space(255) ' provide enough room for the function to put the value into the buffer
    29.  
    30. slength = GetPrivateProfileString(SectorName, KeyName, "(error)", uname, 255, Inifile)
    31. RetString = Left(uname, slength) ' extract the returned string from the buffer
    32. End Sub
    33.  
    34. Sub WriteIniFile(ByVal SectorName As String, ByVal KeyName As String, ByVal KeyValue As String, ByVal Inifile As String)
    35. 'Param1
    36. 'The section of the INI file to write to.
    37. 'Param2
    38. 'The name of the value to set.
    39. 'Param3
    40. 'The string to set as the value.
    41. 'Param4
    42. 'The filename of the INI file to write to.
    43.  
    44. Dim Retval As Long ' return value
    45. ' Set the string value.
    46. Retval = WritePrivateProfileString(SectorName, KeyName, KeyValue, Inifile)
    47.  
    48. End Sub
    49.  
    50.  
    51. 'Then to use it in a form add this code
    52. Dim retString as String
    53.  
    54. Call ReadINIFile("MyApp", "DataSource", iniFile)
    55. 'where MyApp is the category, DataSource is the key,
    56. 'and inifile is the full path and file name of your inifile
    57.  
    58. 'Then check to see what was returned
    59. If retString = "(error)" then
    60.    'The key wasnt found in the INI file
    61.    'so you could write a default value here if u wanted
    62.    Call WriteIniFile("MyApp", "DataSource", "C:\Temp", iniFile)
    63. Else
    64.     'The value was found
    65.     msgBox retString
    66. End if

  22. #22

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2000
    Location
    Minnesota
    Posts
    830
    Here is some code I found here at vb-world that works great.

    Function TrimNull(sNullString As String) As String
    '=========================================================
    '=========================================================
    Dim iLen As Integer
    Dim iTemp As Integer
    Dim sTemp As String

    iLen = Len(sNullString)
    iTemp = InStr(sNullString, Chr$(0))

    If iTemp <> 0 Then
    sTemp = Left$(sNullString, iTemp - 1)
    sNullString = sTemp
    End If

    TrimNull = sNullString

    End Function

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width