473,395 Members | 1,537 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Problem Reading Ini File Using GetPrivateProfileStringKey()

With the following VB.NET code,

----------------------------------------------------------------------------
-----------------------------------------
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String)
As Long

Private Declare Function GetPrivateProfileStringKey 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 GetPrivateProfileStringNullKey Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Integer, ByVal lpDefault As String, ByVal lpReturnedString As
String, ByVal nSize As Long, ByVal lpFileName As String) As Long

Private Sub DoIt()
Dim strData As String = Space(255)

WritePrivateProfileString(Application.ProductName, "Datum1", "NewInfo",
"MyInfo.ini")
GetPrivateProfileStringKey(Application.ProductName , "Datum1",
"DefaultInfo", strData, 255, "MyInfo.ini")
MsgBox (strData)
End Sub
----------------------------------------------------------------------------
-----------------------------------------

The WritePrivateProfileString line works, successfully saving the string
"Datum1=NewInfo" in the MyInfo.ini file under [Application.ProductName].
However, the next line fails to retrieve "NewInfo" without generating an
error, resulting in the MsgBox displaying "DefaultInfo". What am I doing
wrong in my implementation of GetPrivateProfileStringKey? Thanks.

Nov 20 '05 #1
4 8300
"Phillip Galey" <pg****@starcalif.com> schrieb
With the following VB.NET code,

-------------------------------------------------------------------------- -- -----------------------------------------
Private Declare Function WritePrivateProfileString Lib "kernel32"
Alias "WritePrivateProfileStringA" (ByVal lpApplicationName As
String, ByVal lpKeyName As String, ByVal lpString As String, ByVal
lpFileName As String) As Long

[...]

The WritePrivateProfileString line works, successfully saving the
string "Datum1=NewInfo" in the MyInfo.ini file under
[Application.ProductName]. However, the next line fails to retrieve
"NewInfo" without generating an error, resulting in the MsgBox
displaying "DefaultInfo". What am I doing wrong in my implementation
of GetPrivateProfileStringKey? Thanks.


http://msdn.microsoft.com/library/en...rDataTypes.asp
http://msdn.microsoft.com/library/en...ualBasic70.asp
http://msdn.microsoft.com/library/en...indowsAPIs.asp
http://msdn.microsoft.com/library/en...lfunctions.asp

--
Armin

http://www.plig.net/nnq/nquote.html
http://www.netmeister.org/news/learn2quote.html

Nov 20 '05 #2
* "Phillip Galey" <pg****@starcalif.com> scripsit:
With the following VB.NET code,

----------------------------------------------------------------------------
-----------------------------------------
Private Declare Function WritePrivateProfileString Lib "kernel32" Alias
"WritePrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As String, ByVal lpString As String, ByVal lpFileName As String)
As Long

Private Declare Function GetPrivateProfileStringKey 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 GetPrivateProfileStringNullKey Lib "kernel32" Alias
"GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal
lpKeyName As Integer, ByVal lpDefault As String, ByVal lpReturnedString As
String, ByVal nSize As Long, ByVal lpFileName As String) As Long


The declares are wrong -- 'Long' is now a 64 bit datatype. You will
have to use Int32/Integer instead. It's better to declare the functions
as 'Auto' and remove the alias.

Working sample:

<http://www.mentalis.org/soft/class.qpx?id=6>

--
Herfried K. Wagner [MVP]
<http://www.mvps.org/dotnet>
Nov 20 '05 #3
In article <uY*************@TK2MSFTNGP11.phx.gbl>, Phillip Galey wrote:
With the following VB.NET code,
Corrected...
---------------------------------------------------------------------
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 Delcare 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
---------------------------------------------------------------------
The WritePrivateProfileString line works, successfully saving the string
"Datum1=NewInfo" in the MyInfo.ini file under [Application.ProductName].
However, the next line fails to retrieve "NewInfo" without generating an
error, resulting in the MsgBox displaying "DefaultInfo". What am I doing
wrong in my implementation of GetPrivateProfileStringKey? Thanks.


Well, mostly the problem is that Long values in VB.NET are 64-bit,
unlike in VB.CLASSIC where they were 32-bit. In VB.NET - you should use
Integer.

Another problem - though not one that would cause errors - is actually
aliasing to a specific version of GetPrivateProfileString. You
shouldn't do that. You should, in most cases, drop the alias and use
Auto and let the runtime determine which version of the function to
call. This saves you a lot of unnecessary string conversions on NT
based systems.

Also, using strings for writable buffers is not a good idea. It works,
in VB.NET because the VB.NET marshaller does some extra work (which can
result in performance issues as well), but it also violates the
immutability of System.String. It is better, IMHO, to use
System.Text.StringBuilder for writable buffers. It will also make it
easier on you if you have to read/write any C# code, because in C#
passing a string in this case will not work. You won't get an error,
but the value won't change.

--
Tom Shelton
MVP [Visual Basic]
Nov 20 '05 #4
Why not just use an INI File class like:

http://www.jrbtech.com/index.php/cod...ini_file_class

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
3
by: SB | last post by:
Hello. I have an input file which is laid out in the following manner... Name Day 1 am time 1 am time 2 appointment pm time 1 pm time 2 appointment Day 2
11
by: Abhishek | last post by:
I have a problem transfering files using sockets from pocket pc(.net compact c#) to desktop(not using .net just mfc and sockets 2 API). The socket communication is not a issue and I am able to...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
5
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.