GetPrivateProfileString

L

Lalasa

I got GetPrivateProfileString to work using interop, but there is
something that is bugging me.

This is the syntax provided on Pinvoke.net
[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
StringBuilder lpReturnedString,
uint nSize,
string lpFileName);


Why should lpReturnedString be a StringBuilder type?
Why doesn't the syntax below work?

[DllImport("kernel32.dll")]
static extern uint GetPrivateProfileString(
string lpAppName,
string lpKeyName,
string lpDefault,
[MarshalAs(UnmanagedType.LPTStr)] string lpReturnedString,
uint nSize,
string lpFileName);

MSDN documentation is as below:

DWORD GetPrivateProfileString(
LPCTSTR lpAppName,
LPCTSTR lpKeyName,
LPCTSTR lpDefault,
LPTSTR lpReturnedString,
DWORD nSize,
LPCTSTR lpFileName
);


Thanks,
Lalasa.
 
M

Mattias Sjögren

Why should lpReturnedString be a StringBuilder type?
Why doesn't the syntax below work?

Because it's an output parameter which is changed by the called
function. Strings are immutable and can't be changed.



Mattias
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top