Calling Win32 API not working 
Author Message
 Calling Win32 API not working

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



Fri, 06 May 2005 09:00:12 GMT  
 Calling Win32 API not working
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.


Quote:
> 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



Fri, 06 May 2005 11:05:06 GMT  
 Calling Win32 API not working
Mike,
Thanks for the reply
I tried this and it still returned "" - is there anything else I can try?

Thanks
Evan


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



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



Fri, 06 May 2005 11:18:00 GMT  
 Calling Win32 API not working


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

Quote:

> 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

- Show quoted text -

Quote:
> 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

Quote:
>     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


Fri, 06 May 2005 12:35:53 GMT  
 Calling Win32 API not working
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


Quote:



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



Fri, 06 May 2005 13:09:53 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. DestroyWindow API call not working?

2. Win API Call not working in vb.net app distributed to WIn 98

3. API call to GetUserName not working in VB5.0

4. VB5-PRO: SendMessage API Call not Working...Why?

5. GetKeyState API call not working

6. DestroyWindow API call not working?

7. Cant get LogonUser() to work in VB (Win32 API)

8. calling win32 api

9. MCI win32 API call from VB.NET

10. Win32 API Call Problem

11. VB.NET and Win32 API Function Calls

12. Win32 API Call Syntax

 

 
Powered by phpBB® Forum Software