Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calling Win32 API not working

24 views
Skip to first unread message

Evan Bourlotos

unread,
Nov 17, 2002, 8:00:12 PM11/17/02
to
Hi All,.
I am trying to (re-)use my vb6 code in vb.net with the following api call
to getprivateprofilestring
It seems to go through without error but returns a null string. The routines
worked fine in vb6 and I made sure the as any stuff was fixed. This is
pretty basic but I just can't get it to return a string I know exists in a
file I know exists.

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

Mike Huguet

unread,
Nov 17, 2002, 10:05:06 PM11/17/02
to
Try
Public Declare Unicode Function GetPrivateProfileString Lib "kernel32.dll"
Alias _
"GetPrivateProfileStringW" ByVal lpApplicationName As String,

ByVal
lpKeyName As String, _
ByVal lpDefault As String, ByVal lpReturnedString As String,
ByVal nSize As Integer, _
ByVal lpFileName As String) As Integer

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...

Evan Bourlotos

unread,
Nov 17, 2002, 10:18:00 PM11/17/02
to
Mike,
Thanks for the reply
I tried this and it still returned "" - is there anything else I can try?

Thanks
Evan

"Mike Huguet" <mhu...@sark.com> wrote in message
news:e2XCB9qjCHA.2808@tkmsftngp11...

Tom Shelton

unread,
Nov 17, 2002, 11:35:53 PM11/17/02
to

"Evan Bourlotos" <evan_at_cheapaschips_dot_com_dot_au> wrote in message
news:e6Ot81pjCHA.2580@tkmsftngp12...
> Hi All,.
> I am trying to (re-)use my vb6 code in vb.net with the following api call
> to getprivateprofilestring
> It seems to go through without error but returns a null string. The
routines
> worked fine in vb6 and I made sure the as any stuff was fixed. This is
> pretty basic but I just can't get it to return a string I know exists in a
> file I know exists.
>
> 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

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 Bourlotos

unread,
Nov 18, 2002, 12:09:53 AM11/18/02
to
thanks worked like a charm , I thought I had changed to int but with all the
cust and paste shit I may have undone something and not gone back.

Evan

"Tom Shelton" <t...@mtogden.com> wrote in message
news:O62w9vrjCHA.2736@tkmsftngp10...

0 new messages