Go Back   Forum Care Forums > Development Reference Area > Visual Basic

Reply
 
LinkBack Thread Tools Display Modes
Why won't GetPrivateProfileString work in .Net?
Old
  (#1)
Mark Hayworth
Guest
 
Posts: n/a
Default Why won't GetPrivateProfileString work in .Net? - 06-04-2007, 09:48 AM

Randy:
I too am having the problem that this other guy/girl had. I put in
the proper API declarations and arguments for GetPrivateProfileString
yet it doesn't return the correct stuff. It always returns the
default value (padded with huge amount of spaces on the right) and the
return value (number of characters read) is some crazy huge number
like 51298398749084
Your demo doesn't work in .Net - it's for prior versions of VB, and so
the problem still remains. Have you had any luck with
GetPrivateProfileString in VB.Net?
Mark Hayworth




From: Randy Birch (EMAIL REMOVED)
Subject: Re: Why won't GetPrivateProfileString work?


View this article only
Newsgroups: comp.lang.visual.basic
Date: 2003-02-03 19:24:40 PST


Examine the basic private profile demo at
http://www.mvps.org/vbnet/code/file/pprofilebasic.htm

--

Randy Birch
MVP Visual Basic
http://www.mvps.org/vbnet/
Please respond only to the newsgroups so all can benefit.


"nospam" <EMAIL REMOVED> wrote in message
newsEMAIL REMOVED...
# I can't for the life of me to get the GetPrivateProfileString API
function
# to work correctly in VB.NET, although I've gotten
# WritePrivateProfileString to work. Listed below after this message
# are the function declarations I'm using.
#
# These two functions write to the test.ini as they should:
# WritePrivateProfileString("Section1", "FirstKey", "First Value",
"test.ini")
# WritePrivateProfileString("Section2", "SecondKey", "Second Value",
"test.ini")
#
#
# But when I try and read the values back, all that is copied to the
buffer
# is the "Default" value:
#
# GetPrivateProfileString("Section1", "FirstKey", "Default", Buffer,
255,
# "test.ini")
#
# MsgBox(Buffer, MsgBoxStyle.OKOnly, "Ini Test")
#
#
# I also tried hard coding the path to the test.ini file, but that
didn't
# work either. Any ideas why GetPrivateProfileString isn't working?
#
# -Thanks
#
#
# Function Declarations:
#
# Private 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
#
#
# Private Declare Function WritePrivateProfileString Lib "kernel32"
Alias _
# "WritePrivateProfileStringA" (ByVal lpSectionName As String, ByVal
_
# lpKeyName As String, ByVal lpString As String, ByVal lpFileName As
String) As Long
#
   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Re: Why won't GetPrivateProfileString work in .Net?
Old
  (#2)
Randy Birch
Guest
 
Posts: n/a
Default Re: Why won't GetPrivateProfileString work in .Net? - 06-04-2007, 09:48 AM

Nope ... my .net disks are used as coasters, so I can feel there is at least
some redeeming value to the product.

: Date: 2003-02-03 19:24:40 PST

golly ... that's why I hate google! <g>

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"Mark Hayworth" <EMAIL REMOVED> wrote in message
news:EMAIL REMOVED m...
: Randy:
: I too am having the problem that this other guy/girl had. I put in
: the proper API declarations and arguments for GetPrivateProfileString
: yet it doesn't return the correct stuff. It always returns the
: default value (padded with huge amount of spaces on the right) and the
: return value (number of characters read) is some crazy huge number
: like 51298398749084
: Your demo doesn't work in .Net - it's for prior versions of VB, and so
: the problem still remains. Have you had any luck with
: GetPrivateProfileString in VB.Net?
: Mark Hayworth
:
:
:
:
: From: Randy Birch (EMAIL REMOVED)
: Subject: Re: Why won't GetPrivateProfileString work?
:
:
: View this article only
: Newsgroups: comp.lang.visual.basic
: Date: 2003-02-03 19:24:40 PST
:
:
: Examine the basic private profile demo at
: http://www.mvps.org/vbnet/code/file/pprofilebasic.htm
:
: --
:
: Randy Birch
: MVP Visual Basic
: http://www.mvps.org/vbnet/
: Please respond only to the newsgroups so all can benefit.
:
:
: "nospam" <EMAIL REMOVED> wrote in message
: newsEMAIL REMOVED...
: # I can't for the life of me to get the GetPrivateProfileString API
: function
: # to work correctly in VB.NET, although I've gotten
: # WritePrivateProfileString to work. Listed below after this message
: # are the function declarations I'm using.
: #
: # These two functions write to the test.ini as they should:
: # WritePrivateProfileString("Section1", "FirstKey", "First Value",
: "test.ini")
: # WritePrivateProfileString("Section2", "SecondKey", "Second Value",
: "test.ini")
: #
: #
: # But when I try and read the values back, all that is copied to the
: buffer
: # is the "Default" value:
: #
: # GetPrivateProfileString("Section1", "FirstKey", "Default", Buffer,
: 255,
: # "test.ini")
: #
: # MsgBox(Buffer, MsgBoxStyle.OKOnly, "Ini Test")
: #
: #
: # I also tried hard coding the path to the test.ini file, but that
: didn't
: # work either. Any ideas why GetPrivateProfileString isn't working?
: #
: # -Thanks
: #
: #
: # Function Declarations:
: #
: # Private 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
: #
: #
: # Private Declare Function WritePrivateProfileString Lib "kernel32"
: Alias _
: # "WritePrivateProfileStringA" (ByVal lpSectionName As String, ByVal
: _
: # lpKeyName As String, ByVal lpString As String, ByVal lpFileName As
: String) As Long
: #

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Why won't GetPrivateProfileString work in .Net?
Old
  (#3)
Randy Birch
Guest
 
Posts: n/a
Default Re: Why won't GetPrivateProfileString work in .Net? - 06-04-2007, 09:49 AM

Sorry ... I meant to include links to net groups where you might find a
comp***ionate ear ..

news://msnews.microsoft.com/microsof...dotnet.general
news://msnews.microsoft.com/microsof...t.languages.vb
news://msnews.microsoft.com/microsof....vsnet.general

--

Randy Birch
MVP Visual Basic
http://vbnet.mvps.org/
Please respond only to the newsgroups so all can benefit.


"Randy Birch" <EMAIL REMOVED> wrote in message
news:Sn2Yc.77848$EMAIL REMOVED. cable.rogers.com...
: Nope ... my .net disks are used as coasters, so I can feel there is at
least
: some redeeming value to the product.
:
:: Date: 2003-02-03 19:24:40 PST
:
: golly ... that's why I hate google! <g>
:
: --
:
: Randy Birch
: MVP Visual Basic
: http://vbnet.mvps.org/
: Please respond only to the newsgroups so all can benefit.
:
:
: "Mark Hayworth" <EMAIL REMOVED> wrote in message
: news:EMAIL REMOVED m...
:: Randy:
:: I too am having the problem that this other guy/girl had. I put in
:: the proper API declarations and arguments for GetPrivateProfileString
:: yet it doesn't return the correct stuff. It always returns the
:: default value (padded with huge amount of spaces on the right) and the
:: return value (number of characters read) is some crazy huge number
:: like 51298398749084
:: Your demo doesn't work in .Net - it's for prior versions of VB, and so
:: the problem still remains. Have you had any luck with
:: GetPrivateProfileString in VB.Net?
:: Mark Hayworth
::
::
::
::
:: From: Randy Birch (EMAIL REMOVED)
:: Subject: Re: Why won't GetPrivateProfileString work?
::
::
:: View this article only
:: Newsgroups: comp.lang.visual.basic
:: Date: 2003-02-03 19:24:40 PST
::
::
:: Examine the basic private profile demo at
:: http://www.mvps.org/vbnet/code/file/pprofilebasic.htm
::
:: --
::
:: Randy Birch
:: MVP Visual Basic
:: http://www.mvps.org/vbnet/
:: Please respond only to the newsgroups so all can benefit.
::
::
:: "nospam" <EMAIL REMOVED> wrote in message
:: newsEMAIL REMOVED...
:: # I can't for the life of me to get the GetPrivateProfileString API
:: function
:: # to work correctly in VB.NET, although I've gotten
:: # WritePrivateProfileString to work. Listed below after this message
:: # are the function declarations I'm using.
:: #
:: # These two functions write to the test.ini as they should:
:: # WritePrivateProfileString("Section1", "FirstKey", "First Value",
:: "test.ini")
:: # WritePrivateProfileString("Section2", "SecondKey", "Second Value",
:: "test.ini")
:: #
:: #
:: # But when I try and read the values back, all that is copied to the
:: buffer
:: # is the "Default" value:
:: #
:: # GetPrivateProfileString("Section1", "FirstKey", "Default", Buffer,
:: 255,
:: # "test.ini")
:: #
:: # MsgBox(Buffer, MsgBoxStyle.OKOnly, "Ini Test")
:: #
:: #
:: # I also tried hard coding the path to the test.ini file, but that
:: didn't
:: # work either. Any ideas why GetPrivateProfileString isn't working?
:: #
:: # -Thanks
:: #
:: #
:: # Function Declarations:
:: #
:: # Private 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
:: #
:: #
:: # Private Declare Function WritePrivateProfileString Lib "kernel32"
:: Alias _
:: # "WritePrivateProfileStringA" (ByVal lpSectionName As String, ByVal
:: _
:: # lpKeyName As String, ByVal lpString As String, ByVal lpFileName As
:: String) As Long
:: #
:

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Why won't GetPrivateProfileString work in .Net?
Old
  (#4)
Mark Hayworth
Guest
 
Posts: n/a
Default Re: Why won't GetPrivateProfileString work in .Net? - 06-04-2007, 09:49 AM

Randy:
Thanks for pointing me to the other newsgroups - I found the answer
there pretty quickly. Apparently it has to do with vb6 longs are now
..Net integers because a long in .net is 64 bits not 32 bits as before
in vb6. Corrected code from Tom Shelton follows.
---------------------------------------------------------------------
Private Declare Auto Function WritePrivateProfileString Lib "kernel32"
_
(ByVal lpApplication 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

Private Sub DoIt()
Dim strData As New System.Text.StringBuilder(255)

WritePrivateProfileString(Application.ProductName, "Datum1", _
"NewInfo", "MyInfo.ini")

GetPrivateProfileString (Application.ProductName, "Datum1", _
"DefaultInfo", strData, strData.Capacity, "MyInfo.ini")

MsgBox (strData.ToString())
End Sub
---------------------------------------------------------------------
Note that Application.ProductName is really the section name in your
ini file and "Datum1" is the key name. The value comes out in strData
automatically - you don't need to ***ign anything to the return value
of GetPrivateProfileString
Thanks,
Mark Hayworth


"Randy Birch" <EMAIL REMOVED> wrote in message news:<9p2Yc.77863$EMAIL REMOVED .cable.rogers.com>...
> Sorry ... I meant to include links to net groups where you might find a
> comp***ionate ear ..
>
> news://msnews.microsoft.com/microsof...dotnet.general
> news://msnews.microsoft.com/microsof...t.languages.vb
> news://msnews.microsoft.com/microsof....vsnet.general
>
> --
>
> Randy Birch
> MVP Visual Basic
> http://vbnet.mvps.org/
> Please respond only to the newsgroups so all can benefit.
>
>
> "Randy Birch" <EMAIL REMOVED> wrote in message
> news:Sn2Yc.77848$EMAIL REMOVED. cable.rogers.com...
> : Nope ... my .net disks are used as coasters, so I can feel there is at
> least
> : some redeeming value to the product.
> :
> :: Date: 2003-02-03 19:24:40 PST
> :
> : golly ... that's why I hate google! <g>
> :
> : --
> :
> : Randy Birch
> : MVP Visual Basic
> : http://vbnet.mvps.org/
> : Please respond only to the newsgroups so all can benefit.
> :
> :
> : "Mark Hayworth" <EMAIL REMOVED> wrote in message
> : news:EMAIL REMOVED m...
> :: Randy:
> :: I too am having the problem that this other guy/girl had. I put in
> :: the proper API declarations and arguments for GetPrivateProfileString
> :: yet it doesn't return the correct stuff. It always returns the
> :: default value (padded with huge amount of spaces on the right) and the
> :: return value (number of characters read) is some crazy huge number
> :: like 51298398749084
> :: Your demo doesn't work in .Net - it's for prior versions of VB, and so
> :: the problem still remains. Have you had any luck with
> :: GetPrivateProfileString in VB.Net?
> :: Mark Hayworth
> ::
> ::

   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Re: Why won't GetPrivateProfileString work in .Net?
Old
  (#5)
Guest
 
Posts: n/a
Default Re: Why won't GetPrivateProfileString work in .Net? - 06-04-2007, 10:02 AM

II found this using google:

I put the following at the top of the module, above the forms stuff:

==========================================

Public Cl*** IniFile

' API functions

Private Declare Ansi Function GetPrivateProfileString _

Lib "kernel32.dll" Alias "GetPrivateProfileStringA" _

(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

Private Declare Ansi Function WritePrivateProfileString _

Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As String, ByVal lpString As String, _

ByVal lpFileName As String) As Integer

Private Declare Ansi Function GetPrivateProfileInt _

Lib "kernel32.dll" Alias "GetPrivateProfileIntA" _

(ByVal lpApplicationName As String, _

ByVal lpKeyName As String, ByVal nDefault As Integer, _

ByVal lpFileName As String) As Integer

Private Declare Ansi Function FlushPrivateProfileString _

Lib "kernel32.dll" Alias "WritePrivateProfileStringA" _

(ByVal lpApplicationName As Integer, _

ByVal lpKeyName As Integer, ByVal lpString As Integer, _

ByVal lpFileName As String) As Integer

Dim strFilename As String

' Constructor, accepting a filename

Public Sub New(ByVal Filename As String)

strFilename = Filename

End Sub

' Read-only filename property

ReadOnly Property FileName() As String

Get

Return strFilename

End Get

End Property

Public Function GetString(ByVal Section As String, _

ByVal Key As String, ByVal [Default] As String) As String

' Returns a string from your INI file

Dim intCharCount As Integer

Dim objResult As New System.Text.StringBuilder(256)

intCharCount = GetPrivateProfileString(Section, Key, _

[Default], objResult, objResult.Capacity, strFilename)

If intCharCount > 0 Then GetString = Strings.Left(objResult.ToString,
intCharCount)

End Function

Public Function GetInteger(ByVal Section As String, _

ByVal Key As String, ByVal [Default] As Integer) As Integer

' Returns an integer from your INI file

Return GetPrivateProfileInt(Section, Key, _

[Default], strFilename)

End Function

Public Function GetBoolean(ByVal Section As String, _

ByVal Key As String, ByVal [Default] As Boolean) As Boolean

' Returns a boolean from your INI file

Return (GetPrivateProfileInt(Section, Key, _

CInt([Default]), strFilename) = 1)

End Function

Public Sub WriteString(ByVal Section As String, _

ByVal Key As String, ByVal Value As String)

' Writes a string to your INI file

WritePrivateProfileString(Section, Key, Value, strFilename)

Flush()

End Sub

Public Sub WriteInteger(ByVal Section As String, _

ByVal Key As String, ByVal Value As Integer)

' Writes an integer to your INI file

WriteString(Section, Key, CStr(Value))

Flush()

End Sub

Public Sub WriteBoolean(ByVal Section As String, _

ByVal Key As String, ByVal Value As Boolean)

' Writes a boolean to your INI file

WriteString(Section, Key, CStr(CInt(Value)))

Flush()

End Sub

Private Sub Flush()

' Stores all the cached changes to your INI file

FlushPrivateProfileString(0, 0, 0, strFilename)

End Sub

End Cl***

=============================

within my code I use the following to write a to a [setion] key=value

Dim objIniFile As New IniFile("your file path\yourfile.ini")

objIniFile.WriteString("Section name", "Keyname", "your value as string")

'====================

to read from a [section] key=value

Dim objIniFile As New IniFile("your file path\yourfile.ini")

Dim boolData As Boolean = objIniFile.GetBoolean("your Section name", "your
key", True)



good luck.......joisey


   
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Contact Us - Forum Care Forums - Archive - Top