PC Review


Reply
Thread Tools Rate Thread

GetPrivateProfileString

 
 
L
Guest
Posts: n/a
 
      29th Mar 2005
[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.

 
Reply With Quote
 
 
 
 
Pieter Philippaerts
Guest
Posts: n/a
 
      29th Mar 2005
"L" <(E-Mail Removed)> 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


 
Reply With Quote
 
 
 
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      29th Mar 2005
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.
>
>

 
Reply With Quote
 
L
Guest
Posts: n/a
 
      29th Mar 2005
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.
> >
> >


 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      29th Mar 2005
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.
> > >
> > >

>
>

 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgQW50b24=?=
Guest
Posts: n/a
 
      29th Mar 2005
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.
> > >
> > >

>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
GetPrivateProfileString =?Utf-8?B?a3ZyZGV2ZWxvcGVyMQ==?= Microsoft Dot NET 3 25th Mar 2005 05:53 PM
GetPrivateProfileString =?Utf-8?B?bW9sb25lZGU=?= Microsoft Access Form Coding 2 29th Nov 2004 03:25 PM
GetPrivateProfileString =?Utf-8?B?TmVpbCBH?= Microsoft Dot NET Framework 2 1st Jun 2004 10:27 PM
GetPrivateProfileString any managed method? Robains Microsoft Dot NET Framework 2 29th Dec 2003 02:11 PM
GetPrivateProfileString Susan Landgraf Microsoft Dot NET 1 27th Nov 2003 04:12 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:23 PM.