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:
Quote:
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?
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.
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:
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
Re: fail while reading string
Quote:
Originally Posted by
jmcilhinney
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?
Re: fail while reading string
Quote:
Originally Posted by
Niya
The function signature is wrong and I agree with jmc, you should avoid those suffixes like a plague. Use this declaration:-
vbnet Code:
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 :/
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 ?
Re: fail while reading string
Quote:
Originally Posted by
Niya
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.
Re: fail while reading string
Try this:-
vbnet Code:
Function ReadFile(ByVal File As String, ByVal Header As String, ByVal Var As String) As String
Dim BUFFERLEN As Integer = 5000
Dim szReturn As String = Space(BUFFERLEN)
Dim i As Integer
Try
i = GetPrivateProfileString(Header, Var, Nothing, szReturn, BUFFERLEN, File)
ReadFile = szReturn.Substring(0, i)
Catch ex As Exception
MsgBox(String.Format("Error: [{0}] <Yerbato.Text.Files.ReadFile>", ex.Message))
ReadFile = ""
End Try
End Function
Re: fail while reading string
Actually that Try....Catch block is pointless....
vbnet Code:
Function ReadFile(ByVal File As String, ByVal Header As String, ByVal Var As String) As String
Dim BUFFERLEN As Integer = 5000
Dim szReturn As String = Space(BUFFERLEN)
Dim i As Integer
i = GetPrivateProfileString(Header, Var, Nothing, szReturn, BUFFERLEN, File)
If i > 0 Then
Return szReturn.Substring(0, i)
Else
Return ""
End If
End Function
Re: fail while reading string
Quote:
Originally Posted by
Niya
Actually that Try....Catch block is pointless....
vbnet Code:
Function ReadFile(ByVal File As String, ByVal Header As String, ByVal Var As String) As String
Dim BUFFERLEN As Integer = 5000
Dim szReturn As String = Space(BUFFERLEN)
Dim i As Integer
i = GetPrivateProfileString(Header, Var, Nothing, szReturn, BUFFERLEN, File)
If i > 0 Then
Return szReturn.Substring(0, i)
Else
Return ""
End If
End Function
thanks, works fine so far =)
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.
Re: fail while reading string
Quote:
Originally Posted by
DataMiser
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 ;)
Re: fail while reading string
True but part of that piece of code looked like it was actually written in VB6
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.