Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Go Back  Xtreme Visual Basic Talk > > > Help with GetPrivateProfileString problem


Reply
 
Thread Tools Display Modes
  #1  
Old 01-30-2008, 04:08 PM
TavMaster-T TavMaster-T is offline
Regular
 
Join Date: Aug 2004
Location: St Louis
Posts: 72
Default Help with GetPrivateProfileString problem


Hi,

I'm trying to port some code from VB6 to vb.net for reading from .ini files. I've made some minor adjustments for vb.net but I'm still having some problems.

This code is in a module:
Code:
Module Module1
    Declare Function GetPrivateProfileString Lib "kernel32" 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

    'Read a string from an INI file
    Public Function GetINI(ByVal sSection As String, ByVal sKey As String, Optional ByVal sDefault As String = "") As String
        Dim stemp As String
        Dim nLength As Long

        stemp = Space$(256)
        nLength = GetPrivateProfileString(sSection, sKey, sDefault, stemp, 255, "C:\x.ini")
        stemp = Trim(stemp)
        MsgBox(nLength)
        GetINI = Left$(stemp, nLength)
    End Function
End Module
This is the test function under a button on a form:
Code:
        MsgBox(GetINI("Section", "Item1", ""))
and this is the contents of the .ini file:
Code:
[SSS]
Item1=xxx
Item2=yyy
This code works fine in VB6 but in vb.net the value returned from GetPrivateProfileString is 5325540403707904 which causes Left$(stemp, nLength) to error out. I'd just work around this by replacing "nLenght =" with "Call" but the function is returning an empty string anyway.

Does anyone know why I'm having this issue? Do I need to declare the api functions differently? Thanks in advance for any help. BTW, I'm using vb.net 2002.
Reply With Quote
  #2  
Old 01-30-2008, 04:48 PM
AtmaWeapon's Avatar
AtmaWeaponHelp with GetPrivateProfileString problem AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Try replacing the Long declarations with Integer. In VB6, Integer was a 16-bit variable so Long was required to be compatible with C. In .NET, Long is a 64-bit integer, so using it when you need a 32-bit integer can provide wonky results.

A useful site for API declarations in VB .NET is http://www.pinvoke.net/. I found the page for GetPrivateProfileString and they are using Integer instead of Long.

It might be better to look in to configuration files rather than INI files; .NET provides no native support for INI files because for whatever reason MS is trying to move away from them towards XML files. I have a feeling this is not an option, since you're using VB .NET 2002, which is now 3 versions old and not really supported. Why not use VB .NET 2005 or 2008, both of which have Express Editions which are free? At least that way you won't be porting to 2005 in a few years (.NET 1.0 had a ton of issues that were corrected in 2.0).
Reply With Quote
  #3  
Old 01-31-2008, 08:54 AM
TavMaster-T TavMaster-T is offline
Regular
 
Join Date: Aug 2004
Location: St Louis
Posts: 72
Default

Thanks for the reply. I changed the the code to use Integer instead of Long and it got rid of the error
but I still get an empty string instead of the value from the .ini file. Any idea why that is? Do you
think the fact that I'm using 2002 is part of the problem?

I like your suggestion about using an XML config file and I definitely plan on looking in to that. For now
I'd really like to get this code working as it's actually part of an application I created a while back in VB6.
I hoping I can get it converted and see it run as vb.net app before I change it.

The reason I'm using 2002 is simply becuase that's what I bought a while back and it's currently installed
on my computer. I've just been lazy about upgrading it, especially since I'm just trying to get up to speed
on vb.net. I do have access to a copy of 2005 express on CD at work so maybe I should just go ahead
and upgrade.


EDIT: I tried the sample from pinvoke.net and that works so I should be able to figure out what's still wrong with my code based on that. Thanks again for your help!

Last edited by TavMaster-T; 01-31-2008 at 09:19 AM.
Reply With Quote
  #4  
Old 01-31-2008, 09:34 AM
AtmaWeapon's Avatar
AtmaWeaponHelp with GetPrivateProfileString problem AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

I believe the reason you weren't getting anything is because lpReturnedString was a String, and the pinvoke.net code uses StringBuilder. StringBuilder is a buffered string and tends to work better when an API function returns through a parameter, but sometimes just using String works.
Reply With Quote
  #5  
Old 01-31-2008, 10:03 AM
TavMaster-T TavMaster-T is offline
Regular
 
Join Date: Aug 2004
Location: St Louis
Posts: 72
Default

Actually, I had already changed the code to use StringBuilder but I had missed the Long type for the size parameter. I changed size to be an integer. I also took out the "Alias" part of the declaration and added the "Auto" keyword. Now it's working fine.

Thanks for your help and the link!
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
 
Help with GetPrivateProfileString problem
Help with GetPrivateProfileString problem
 
-->