Results 1 to 14 of 14

Thread: fail while reading string

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    fail while reading string

    Hi,

    I'm trying to read a file with my following sub:
    Code:
    Public Declare Function GetPrivateProfileString& Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal AppName$, ByVal KeyName$, ByVal keydefault$, ByVal ReturnedString$, ByVal RSSize&, ByVal FileName$)
    Code:
        Function ReadFile(ByVal File As String, Header As String, Var As String) As String
            Dim sSpaces As String   ' Max string length
            Dim szReturn As String  ' Return default value if not found
    
            szReturn = vbNullString
            sSpaces = Space$(5000)
    
            Try
    
                Call GetPrivateProfileString(Header, Var, szReturn, sSpaces, Len(sSpaces), File)
    
                ReadFile = RTrim$(sSpaces)
                ReadFile = Left$(ReadFile, Len(ReadFile) - 1)
    
            Catch ex As Exception
    
                MsgBox(String.Format("Error: [{0}] <Yerbato.Text.Files.ReadFile>", ex.Message))
                ReadFile = ""
    
            End Try
    
        End Function
    but after calling the sub
    Code:
                MsgBox(File.ReadFile(Application.StartupPath & "/Data/Accounts/" & Username & ".bin", "ACCOUNT", "Username"))
    I'll get this error:

    PInvokeStackImbalance was detected
    Message: A call to PInvoke function 'Yerbato.Text.Files!Yerbato.Text.Files.clsFiles::GetPrivateProfileString' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.
    How can I fix this?

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,347

    Re: fail while reading string

    I suggest that you get out of the habit of using those suffixes to specify the type of something. It's a great way to make your code difficult to read. I know that $ makes something a String but I have no recollection what & does because I never use it, never want to use it and never want to see anyone else use it. I'm going to guess that it makes something a Long, because that error message usually results from using a declaration written for VB6 in VB.NET. Most API functions work with 32-bit numbers, which means Long in VB6. A Long in VB.NET is 64-bit though, which is what causes the issue. Any declaration that uses Long in VB6 must use Integer in VB.NET.

  3. #3
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: fail while reading string

    The function signature is wrong and I agree with jmc, you should avoid those suffixes like a plague. Use this declaration:-
    vbnet Code:
    1. Public Declare Function GetPrivateProfileString Lib "Kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturn As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: fail while reading string

    Quote Originally Posted by jmcilhinney View Post
    I suggest that you get out of the habit of using those suffixes to specify the type of something. It's a great way to make your code difficult to read. I know that $ makes something a String but I have no recollection what & does because I never use it, never want to use it and never want to see anyone else use it. I'm going to guess that it makes something a Long, because that error message usually results from using a declaration written for VB6 in VB.NET. Most API functions work with 32-bit numbers, which means Long in VB6. A Long in VB.NET is 64-bit though, which is what causes the issue. Any declaration that uses Long in VB6 must use Integer in VB.NET.
    Hi,

    How would I do this in vb.net langlauge then? (instead of vb6)

    the code must easy write and read files, like possible with my code above (in vb6)

    I need it to save and load accounts from my server.

    in vb6 my file looked like this:

    Code:
    [ACCOUNT]
    login=test
    password=12345
    name=herodimus
    level=1
    it was pretty easy to get the values of username, password, name and the level.

    How can I make a code what can easily write and read a specific thing of the file?

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: fail while reading string

    Quote Originally Posted by Niya View Post
    The function signature is wrong and I agree with jmc, you should avoid those suffixes like a plague. Use this declaration:-
    vbnet Code:
    1. Public Declare Function GetPrivateProfileString Lib "Kernel32.dll" Alias "GetPrivateProfileStringA" (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturn As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
    Actually it does not fix the error :/

  6. #6
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: fail while reading string

    You posted same time as me so you missed my post.....I gave you the proper signature.

    EDIT:-
    Are you still getting a stack imbalanced error ?

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: fail while reading string

    Quote Originally Posted by Niya View Post
    You posted same time as me so you missed my post.....I gave you the proper signature.

    EDIT:-
    Are you still getting a stack imbalanced error ?
    Yes,

    Code:
    PInvokeStackImbalance was detected
    Message: A call to PInvoke function 'Yerbato.Text.Files!Yerbato.Text.Files.clsFiles::GetPrivateProfileString' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: fail while reading string

    Try this:-
    vbnet Code:
    1. Function ReadFile(ByVal File As String, ByVal Header As String, ByVal Var As String) As String
    2.         Dim BUFFERLEN As Integer = 5000
    3.         Dim szReturn As String = Space(BUFFERLEN)
    4.         Dim i As Integer
    5.  
    6.         Try
    7.  
    8.             i = GetPrivateProfileString(Header, Var, Nothing, szReturn, BUFFERLEN, File)
    9.  
    10.             ReadFile = szReturn.Substring(0, i)
    11.  
    12.  
    13.         Catch ex As Exception
    14.  
    15.             MsgBox(String.Format("Error: [{0}] <Yerbato.Text.Files.ReadFile>", ex.Message))
    16.             ReadFile = ""
    17.  
    18.         End Try
    19.  
    20.     End Function

  9. #9
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: fail while reading string

    Actually that Try....Catch block is pointless....
    vbnet Code:
    1. Function ReadFile(ByVal File As String, ByVal Header As String, ByVal Var As String) As String
    2.         Dim BUFFERLEN As Integer = 5000
    3.         Dim szReturn As String = Space(BUFFERLEN)
    4.         Dim i As Integer
    5.  
    6.         i = GetPrivateProfileString(Header, Var, Nothing, szReturn, BUFFERLEN, File)
    7.  
    8.         If i > 0 Then
    9.             Return szReturn.Substring(0, i)
    10.         Else
    11.             Return ""
    12.         End If
    13.     End Function

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: fail while reading string

    Quote Originally Posted by Niya View Post
    Actually that Try....Catch block is pointless....
    vbnet Code:
    1. Function ReadFile(ByVal File As String, ByVal Header As String, ByVal Var As String) As String
    2.         Dim BUFFERLEN As Integer = 5000
    3.         Dim szReturn As String = Space(BUFFERLEN)
    4.         Dim i As Integer
    5.  
    6.         i = GetPrivateProfileString(Header, Var, Nothing, szReturn, BUFFERLEN, File)
    7.  
    8.         If i > 0 Then
    9.             Return szReturn.Substring(0, i)
    10.         Else
    11.             Return ""
    12.         End If
    13.     End Function
    thanks, works fine so far =)

  11. #11
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: fail while reading string

    Just so you guys know the functions with $ on the end are a bit faster than those without.

    I have did some testing in VB6 and in every case I have tested There is a very small increase in speed when using functions like CHR$() Mid$() Right$() vs CHR() Mid() Right() as well as others I have tried.

    Personally I usually do not use the $ just for readability but there is a small hit to performance for not using it. In VB6 anyway.

  12. #12
    PowerPoster SJWhiteley's Avatar
    Join Date
    Feb 2009
    Location
    South of the Mason-Dixon Line
    Posts
    2,256

    Re: fail while reading string

    Quote Originally Posted by DataMiser View Post
    Just so you guys know the functions with $ on the end are a bit faster than those without.

    I have did some testing in VB6 and in every case I have tested There is a very small increase in speed when using functions like CHR$() Mid$() Right$() vs CHR() Mid() Right() as well as others I have tried.

    Personally I usually do not use the $ just for readability but there is a small hit to performance for not using it. In VB6 anyway.
    Since those functions no longer exist, except as perhaps wrappers to .NET functions, it isn't really relevant. We aren't shoeing horses any more
    "Ok, my response to that is pending a Google search" - Bucky Katt.
    "There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
    "Before you can 'think outside the box' you need to understand where the box is."

  13. #13
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: fail while reading string

    True but part of that piece of code looked like it was actually written in VB6

  14. #14
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    8,600

    Re: fail while reading string

    The reason Chr$ would be faster than Chr in VB6 is because Chr operates on Variants so every time you pass a string to Chr it would have to internally convert it into a Variant hence the performance hit. Such things are not a concern in VB.Net though.

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