GetPrivateProfileString

L

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.
 
P

Pieter Philippaerts

L said:
[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
 
G

Guest

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

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 said:
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 said:
[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.
 
G

Guest

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 said:
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 said:
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 said:
[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.
 
G

Guest

I meant "if you're *not* doing a lot of string manipulation..." of course.

L said:
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 said:
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 said:
[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.
 
Top