Thanks
evan
Declaration
Public Declare Auto Function GetPrivateProfileString Lib "kernel32.dll"
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
Called from
Public Function ReadIni(ByVal FileName As String, ByVal Appname As String,
ByVal KeyName As String) As String
Dim ret As Long
Dim temp As String = Space(512)
Dim Slen As Long
Dim lpAppName As String
Dim lpKeyName As String
Dim lpDefault As String
Dim lpFileName As String
lpAppName = Appname
lpKeyName = KeyName
lpDefault = ""
lpFileName = FileName
'temp = ""
Slen = Len(temp)
ret = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault, temp,
Slen, lpFileName)
If ret = 0 Then
ReadIni = ""
Else
ReadIni = StrField(Trim(temp), Chr(0), 1)
End If
End Function
Function WriteIni(ByVal FileName As String, ByVal Appname As String,
ByVal KeyName As String, ByVal ItemValue As String) As String
Dim lpAppName As String, lpFileName As String, lpKeyName As String,
lpString As String
Dim ret As Long
lpAppName = Appname
lpKeyName = KeyName
lpString = ItemValue
lpFileName = FileName
ret = WritePrivateProfileString(lpAppName, lpKeyName, lpString,
lpFileName)
WriteIni = ret
End Function
VB.Net handles strings differently than prior versions of VB. The "W"
versions uses Unicode which is how strings are encoded in .Net.
-Mike Huguet
Software Architects, Inc.
"Evan Bourlotos" <evan_at_cheapaschips_dot_com_dot_au> wrote in message
news:e6Ot81pjCHA.2580@tkmsftngp12...
Thanks
Evan
"Mike Huguet" <mhu...@sark.com> wrote in message
news:e2XCB9qjCHA.2808@tkmsftngp11...
Private Declare Auto Function WritePrivateProfileString Lib "kernel32" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpString As String, _
ByVal lpFileName As String) As Integer
Private Declare Auto Function GetPrivateProfileString Lib "kernel32" _
(ByVal lpApplicationName As String, _
ByVal lpKeyName As String, _
ByVal lpDefault As String, _
ByVal lpReturnedString As System.Text.StringBuilder, _
ByVal nSize As Integer,
ByVal lpFileName As String) As Integer
You should notice about the above declares that there is 1. no alias
statement, 2. the return string is declared as System.Text.StringBuilder,
and 3. that all of the longs have been converted to integers.
1. There is no allias because when you use Auto in the declare the runtime
will automatically append an A or W on to the name of the function before
making the call as well as making the necessary conversions.
2. All strings that are going to be populated by the API should be declared
as StringBuilder. This is really for performance reasons since Strings are
immutable objects in .NET, so you can avoid extraneous string copies by
using StringBuilder which is mutable.
3. Longs in VB.NET are 64-bit integers. This API call is expecting 32-bit
integers, which is just Integer in VB.NET.
>
> Called from
Public Function ReadIni (ByVal FileName As String, ByVal AppName As String,
ByVal KeyName As String) As String
Dim buffer As New StringBuilder(512)
If GetPrivateProfileString(AppName, KeyName, "", buffer,
buffer.Capacity, FileName) > 0 Then
Return buffer.ToString()
End If
End Function
> Public Function ReadIni(ByVal FileName As String, ByVal Appname As String,
> ByVal KeyName As String) As String
> Dim ret As Long
> Dim temp As String = Space(512)
> Dim Slen As Long
> Dim lpAppName As String
> Dim lpKeyName As String
> Dim lpDefault As String
> Dim lpFileName As String
>
> lpAppName = Appname
> lpKeyName = KeyName
> lpDefault = ""
> lpFileName = FileName
> 'temp = ""
> Slen = Len(temp)
>
> ret = GetPrivateProfileString(lpAppName, lpKeyName, lpDefault,
temp,
> Slen, lpFileName)
>
> If ret = 0 Then
> ReadIni = ""
> Else
> ReadIni = StrField(Trim(temp), Chr(0), 1)
> End If
>
> End Function
Public Function WriteIni (ByVal FileName As String, ByVal Appname As String,
ByVal KeyName As String, ByVal ItemValue As String)
Return WritePrivateProfileString(Appname, KeyName, ItemValue, FileName)
End Function
> Function WriteIni(ByVal FileName As String, ByVal Appname As String,
> ByVal KeyName As String, ByVal ItemValue As String) As String
> Dim lpAppName As String, lpFileName As String, lpKeyName As
String,
> lpString As String
> Dim ret As Long
> lpAppName = Appname
> lpKeyName = KeyName
> lpString = ItemValue
> lpFileName = FileName
> ret = WritePrivateProfileString(lpAppName, lpKeyName, lpString,
> lpFileName)
> WriteIni = ret
> End Function
HTH,
Tom Shelton
Evan
"Tom Shelton" <t...@mtogden.com> wrote in message
news:O62w9vrjCHA.2736@tkmsftngp10...