Avatar of jkavx
jkavx
 asked on

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#

Avatar of undefined
Last Comment
jkavx

8/22/2022 - Mon
Jaime Olivares

try to declare the interop declaration:

[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);

you need to create a StringBuilder object before invoking it.
Jaime Olivares

jkavx

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

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

ASKER
Passing string rather than StringBuilder the return value is 0, and the string is empty.
jkavx

ASKER
I found what I needed in one of the comments below the article.  You need to pass a char[] instead of a StringBuilder.