GetPrivateProfileString

Discussion in 'Microsoft C# .NET' started by L, Mar 29, 2005.

  1. L

    L Guest

    [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.
     
    L, Mar 29, 2005
    #1
    1. Advertisements

  2. "L" <> 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
     
    Pieter Philippaerts, Mar 29, 2005
    #2
    1. Advertisements

  3. L

    Guest 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" 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.
    >
    >
     
    Guest, Mar 29, 2005
    #3
  4. L

    L Guest

    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.
    > >
    > >
     
    L, Mar 29, 2005
    #4
  5. L

    Guest 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" 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.
    > > >
    > > >

    >
    >
     
    Guest, Mar 29, 2005
    #5
  6. L

    Guest Guest

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

    >
    >
     
    Guest, Mar 29, 2005
    #6
    1. Advertisements

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Germic
    Replies:
    1
    Views:
    1,602
    Mattias Sjögren
    Jan 24, 2005
  2. Lalasa

    GetPrivateProfileString

    Lalasa, Apr 5, 2005, in forum: Microsoft C# .NET
    Replies:
    1
    Views:
    789
    Mattias Sjögren
    Apr 5, 2005
  3. Nhan

    use GetPrivateProfileString

    Nhan, Jul 30, 2007, in forum: Microsoft C# .NET
    Replies:
    2
    Views:
    942
    Thanh-Nhan Le
    Jul 31, 2007
Loading...

Share This Page