About
Pricing
Community
Teams
Start Free Trial
Log in
jkavx
asked on
7/22/2008
Using GetProfileString API in C#
I'm migrating some VB code to C# and need an example of how to make the GetProfileString Api call in C#.
C#
6
1
Last Comment
jkavx
8/22/2022 - Mon
Jaime Olivares
7/22/2008
try to declare the interop declaration:
[DllImport("KERNEL32.DLL",
EntryPoint="GetPrivateProf
ileStringA
",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileString (string lpApplicationName,
string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int
nSize, string lpFileName);
you need to create a StringBuilder object before invoking it.
Jaime Olivares
7/22/2008
Also try with the example at this tutorial:
http://jachman.wordpress.com/2006/09/11/how-to-access-ini-files-in-c-net/
jkavx
7/22/2008
ASKER
The problem I'm having is that the StringBuilder only contains a portion of what should be returned; it's being truncated. The return value of the call is 559, but the StringBuilder only has about 50 characters.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck
ASKER CERTIFIED SOLUTION
Jaime Olivares
7/22/2008
THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
jkavx
7/22/2008
ASKER
Passing string rather than StringBuilder the return value is 0, and the string is empty.
jkavx
7/22/2008
ASKER
I found what I needed in one of the comments below the article. You need to pass a char[] instead of a StringBuilder.
[DllImport("KERNEL32.DLL",
CharSet=CharSet.Ansi)]
private static extern int GetPrivateProfileString (string lpApplicationName,
string lpKeyName, string lpDefault, StringBuilder lpReturnedString, int
nSize, string lpFileName);
you need to create a StringBuilder object before invoking it.