473,385 Members | 2,029 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,385 software developers and data experts.

GetPrivateProfileString

L
[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
Interoping GetPrivateProfileString is giving me this Exception in
certain cases:

StringBuilder buffer has been overflowed by unmanaged code. The
process may become unstable. Insufficient capacity allocated to the
StringBuilder before marshaling it."

public void ReadINIFile()
{
try
{
GetPrivateProfileString("Directories" , "In" , "(rg)data/xml",
_strInDir,80, _strINIFile);
GetPrivateProfileString("Directories" , "Out", "c:\\xmldata" ,
_strOutDir,80, _strINIFile);
GetPrivateProfileString("System" , "IPAddress", "198.190.229.101" ,
_strIP,80, _strINIFile);
GetPrivateProfileString("System" , "UserID", "jw" , _strUser,80,
_strINIFile);
GetPrivateProfileString("System" , "Password", "jason" ,
_strPwd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeStart", "14:30" ,
_strBusyTimeStart,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeEnd", "15:30" ,
_strBusyTimeEnd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeIntervalInMin", "1" ,
_strBusyTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("Time" , "RegTimeIntervalInMin", "5" ,
_strRegTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("File" , "MainFrame to PC File extension",
"false" , _strFile,80, _strINIFile);
}
catch (Exception ex)
{
throw ex;
}
}

Does anybody have a solution to this?

Thanks,
Lalasa.

Nov 17 '05 #1
5 19610
"L" <la********@investors.com> wrote in message
[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
You may want to use a more explicit declaration of this function:

[DllImport("KERNEL32.DLL", EntryPoint="GetPrivateProfileStringA",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileString (string lpApplicationName,
string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int
nSize, string lpFileName);
StringBuilder buffer has been overflowed by unmanaged code. The
process may become unstable. Insufficient capacity allocated to the
StringBuilder before marshaling it."
How did you initialize the StringBuilder?
Does anybody have a solution to this?


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

Regards,
Pieter Philippaerts
Nov 17 '05 #2
You need to initialize the StringBuilder with the appropriate initial size.
StringBuilder _strInDir = new StringBuilder(80);

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C# to
VB.NET converter

"L" wrote:
[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
Interoping GetPrivateProfileString is giving me this Exception in
certain cases:

StringBuilder buffer has been overflowed by unmanaged code. The
process may become unstable. Insufficient capacity allocated to the
StringBuilder before marshaling it."

public void ReadINIFile()
{
try
{
GetPrivateProfileString("Directories" , "In" , "(rg)data/xml",
_strInDir,80, _strINIFile);
GetPrivateProfileString("Directories" , "Out", "c:\\xmldata" ,
_strOutDir,80, _strINIFile);
GetPrivateProfileString("System" , "IPAddress", "198.190.229.101" ,
_strIP,80, _strINIFile);
GetPrivateProfileString("System" , "UserID", "jw" , _strUser,80,
_strINIFile);
GetPrivateProfileString("System" , "Password", "jason" ,
_strPwd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeStart", "14:30" ,
_strBusyTimeStart,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeEnd", "15:30" ,
_strBusyTimeEnd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeIntervalInMin", "1" ,
_strBusyTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("Time" , "RegTimeIntervalInMin", "5" ,
_strRegTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("File" , "MainFrame to PC File extension",
"false" , _strFile,80, _strINIFile);
}
catch (Exception ex)
{
throw ex;
}
}

Does anybody have a solution to this?

Thanks,
Lalasa.

Nov 17 '05 #3
L
Hi,

I figured that out but I have more questions regarding using
StringBuilder. I am also using WritePrivateProfileString and whenever I
want to assign a value to the string builder before I do a
WritePrivateProfileString, have I no choice but to do a new
StringBuilder(value), every time I want to assign a new value to the
StringBuilder type variable. Will that affect the amount of memory
used?

for ex : I have my class

public class INIFileOp
{

/* private members */
private StringBuilder _strInDir = new StringBuilder(80);
...

/* properties */
public string InputDir
{
get { return _strInDir.toString();
set { _strInDir = new StringBuilder(value); } // Do I have to
do the new every time I set the value?
}

...

/* Methods */
public void WriteINIFile()
{
WritePrivateProfileString("Directories", "In", _strInDir,
_strINIFile);
...
...
}

public void ReadINIFile()
{
GetPrivateProfileString("Directories" , "In" ,
"(rg)data/xml",_strInDir,80, _strINIFile);
...
...
}

}
public class Test
{
INIFileOp objINIFileOp = new INIfileOp();
objINIFileOp.ReadINIFile();
objINIFileOp.InputDir = "xyz/xyz"; // Actually in my code values
will be read from the form fields and set to the properties of
INIFileOp class.
objINIFileOp.WriteINIFile();

}

Any comments on using StringBuilders and the way I am doing it?

Thanks,
Lalasa.

David Anton wrote:
You need to initialize the StringBuilder with the appropriate initial size. StringBuilder _strInDir = new StringBuilder(80);

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C# to VB.NET converter

"L" wrote:
[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
Interoping GetPrivateProfileString is giving me this Exception in
certain cases:

StringBuilder buffer has been overflowed by unmanaged code. The
process may become unstable. Insufficient capacity allocated to the StringBuilder before marshaling it."

public void ReadINIFile()
{
try
{
GetPrivateProfileString("Directories" , "In" , "(rg)data/xml",
_strInDir,80, _strINIFile);
GetPrivateProfileString("Directories" , "Out", "c:\\xmldata" ,
_strOutDir,80, _strINIFile);
GetPrivateProfileString("System" , "IPAddress", "198.190.229.101" , _strIP,80, _strINIFile);
GetPrivateProfileString("System" , "UserID", "jw" , _strUser,80, _strINIFile);
GetPrivateProfileString("System" , "Password", "jason" ,
_strPwd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeStart", "14:30" ,
_strBusyTimeStart,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeEnd", "15:30" ,
_strBusyTimeEnd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeIntervalInMin", "1" ,
_strBusyTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("Time" , "RegTimeIntervalInMin", "5" ,
_strRegTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("File" , "MainFrame to PC File extension", "false" , _strFile,80, _strINIFile);
}
catch (Exception ex)
{
throw ex;
}
}

Does anybody have a solution to this?

Thanks,
Lalasa.


Nov 17 '05 #4
Note that while the GetPrivateProfileString requires a StringBuilder as a
parameter, WritePrivateProfileString does not. So if you're doing a lot of
string manipulation then you can just use a regular string as a parameter and
argument to WritePrivateProfileString.

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C# to
VB.NET converter

"L" wrote:
Hi,

I figured that out but I have more questions regarding using
StringBuilder. I am also using WritePrivateProfileString and whenever I
want to assign a value to the string builder before I do a
WritePrivateProfileString, have I no choice but to do a new
StringBuilder(value), every time I want to assign a new value to the
StringBuilder type variable. Will that affect the amount of memory
used?

for ex : I have my class

public class INIFileOp
{

/* private members */
private StringBuilder _strInDir = new StringBuilder(80);
...

/* properties */
public string InputDir
{
get { return _strInDir.toString();
set { _strInDir = new StringBuilder(value); } // Do I have to
do the new every time I set the value?
}

...

/* Methods */
public void WriteINIFile()
{
WritePrivateProfileString("Directories", "In", _strInDir,
_strINIFile);
...
...
}

public void ReadINIFile()
{
GetPrivateProfileString("Directories" , "In" ,
"(rg)data/xml",_strInDir,80, _strINIFile);
...
...
}

}
public class Test
{
INIFileOp objINIFileOp = new INIfileOp();
objINIFileOp.ReadINIFile();
objINIFileOp.InputDir = "xyz/xyz"; // Actually in my code values
will be read from the form fields and set to the properties of
INIFileOp class.
objINIFileOp.WriteINIFile();

}

Any comments on using StringBuilders and the way I am doing it?

Thanks,
Lalasa.

David Anton wrote:
You need to initialize the StringBuilder with the appropriate initial

size.
StringBuilder _strInDir = new StringBuilder(80);

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C#

to
VB.NET converter

"L" wrote:
[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
Interoping GetPrivateProfileString is giving me this Exception in
certain cases:

StringBuilder buffer has been overflowed by unmanaged code. The
process may become unstable. Insufficient capacity allocated to the StringBuilder before marshaling it."

public void ReadINIFile()
{
try
{
GetPrivateProfileString("Directories" , "In" , "(rg)data/xml",
_strInDir,80, _strINIFile);
GetPrivateProfileString("Directories" , "Out", "c:\\xmldata" ,
_strOutDir,80, _strINIFile);
GetPrivateProfileString("System" , "IPAddress", "198.190.229.101" , _strIP,80, _strINIFile);
GetPrivateProfileString("System" , "UserID", "jw" , _strUser,80, _strINIFile);
GetPrivateProfileString("System" , "Password", "jason" ,
_strPwd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeStart", "14:30" ,
_strBusyTimeStart,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeEnd", "15:30" ,
_strBusyTimeEnd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeIntervalInMin", "1" ,
_strBusyTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("Time" , "RegTimeIntervalInMin", "5" ,
_strRegTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("File" , "MainFrame to PC File extension", "false" , _strFile,80, _strINIFile);
}
catch (Exception ex)
{
throw ex;
}
}

Does anybody have a solution to this?

Thanks,
Lalasa.


Nov 17 '05 #5
I meant "if you're *not* doing a lot of string manipulation..." of course.

"L" wrote:
Hi,

I figured that out but I have more questions regarding using
StringBuilder. I am also using WritePrivateProfileString and whenever I
want to assign a value to the string builder before I do a
WritePrivateProfileString, have I no choice but to do a new
StringBuilder(value), every time I want to assign a new value to the
StringBuilder type variable. Will that affect the amount of memory
used?

for ex : I have my class

public class INIFileOp
{

/* private members */
private StringBuilder _strInDir = new StringBuilder(80);
...

/* properties */
public string InputDir
{
get { return _strInDir.toString();
set { _strInDir = new StringBuilder(value); } // Do I have to
do the new every time I set the value?
}

...

/* Methods */
public void WriteINIFile()
{
WritePrivateProfileString("Directories", "In", _strInDir,
_strINIFile);
...
...
}

public void ReadINIFile()
{
GetPrivateProfileString("Directories" , "In" ,
"(rg)data/xml",_strInDir,80, _strINIFile);
...
...
}

}
public class Test
{
INIFileOp objINIFileOp = new INIfileOp();
objINIFileOp.ReadINIFile();
objINIFileOp.InputDir = "xyz/xyz"; // Actually in my code values
will be read from the form fields and set to the properties of
INIFileOp class.
objINIFileOp.WriteINIFile();

}

Any comments on using StringBuilders and the way I am doing it?

Thanks,
Lalasa.

David Anton wrote:
You need to initialize the StringBuilder with the appropriate initial

size.
StringBuilder _strInDir = new StringBuilder(80);

David Anton
www.tangiblesoftwaresolutions.com
Home of the Instant C# VB.NET to C# converter and the Instant VB C#

to
VB.NET converter

"L" wrote:
[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);
Interoping GetPrivateProfileString is giving me this Exception in
certain cases:

StringBuilder buffer has been overflowed by unmanaged code. The
process may become unstable. Insufficient capacity allocated to the StringBuilder before marshaling it."

public void ReadINIFile()
{
try
{
GetPrivateProfileString("Directories" , "In" , "(rg)data/xml",
_strInDir,80, _strINIFile);
GetPrivateProfileString("Directories" , "Out", "c:\\xmldata" ,
_strOutDir,80, _strINIFile);
GetPrivateProfileString("System" , "IPAddress", "198.190.229.101" , _strIP,80, _strINIFile);
GetPrivateProfileString("System" , "UserID", "jw" , _strUser,80, _strINIFile);
GetPrivateProfileString("System" , "Password", "jason" ,
_strPwd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeStart", "14:30" ,
_strBusyTimeStart,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeEnd", "15:30" ,
_strBusyTimeEnd,80, _strINIFile);
GetPrivateProfileString("Time" , "BusyTimeIntervalInMin", "1" ,
_strBusyTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("Time" , "RegTimeIntervalInMin", "5" ,
_strRegTimeIntervalInMin,80, _strINIFile);
GetPrivateProfileString("File" , "MainFrame to PC File extension", "false" , _strFile,80, _strINIFile);
}
catch (Exception ex)
{
throw ex;
}
}

Does anybody have a solution to this?

Thanks,
Lalasa.


Nov 17 '05 #6

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

Similar topics

4
by: Mark Hayworth | last post by:
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...
1
by: Germic | last post by:
Does C# has an equivalent of the 'C' GetPrivateProfileString? or is the only way to get similar features to read from an INI file is to do a DllImport on Kernel32.dll? Thanks
1
by: Lalasa | last post by:
I got GetPrivateProfileString to work using interop, but there is something that is bugging me. This is the syntax provided on Pinvoke.net static extern uint GetPrivateProfileString( string...
1
by: Domac | last post by:
I need to read some configuration data from .ini file located at Application.StartupPath location . Here is code snippet : Public Declare Unicode Function GetPrivateProfileString Lib...
5
by: 海风 | last post by:
A question about GetPrivateProfileString a section in a .ini file , for example ip = 192.168.1.112 .... i want to get the ip value by using GetPrivateProfileString() function. the code...
2
by: Nhan | last post by:
Hi, I am trying to use the function GetPrivateProfileString as following: public static extern Int32 GetPrivateProfileString(string lpAppName, string lpKeyName, string lpDefault, string...
1
by: thiago777 | last post by:
Is there a better way of accomplishing this ?: http://msdn2.microsoft.com/en-us/library/ms724353.aspx I have some program settings which should be easily editable by the user (really like in a...
6
by: alag20 | last post by:
Hi Guys, I need some urgent help with this as I am becoming clueless now. I have 2 DllImport as below from Kernel32 private static extern int GetPrivateProfileString(string section, int key,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.