using a String Builder a actual parameter and LPTSTR as formal parameter in unmanaged code

T

Tony Johansson

Hi!

Here I have some code from an example from MSDN.
What I don't fully understand here is how is it possible to pass an object
of type StringBuilder and receive this parameter in the
windows API as an LPTSTR ?

Documentation for Win32 GetShortPathName() API Function
// DWORD GetShortPathName(
// LPCTSTR lpszLongPath, // file for which to get short path
// LPTSTR lpszShortPath, // short path name (output)
// DWORD cchBuffer // size of output buffer
// );

[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(
String path, // input string
StringBuilder shortPath, // output string
Int32 shortPathLength); // StringBuilder.Capacity

//Tony
 
A

Arne Vajhøj

Here I have some code from an example from MSDN.
What I don't fully understand here is how is it possible to pass an object
of type StringBuilder and receive this parameter in the
windows API as an LPTSTR ?

Documentation for Win32 GetShortPathName() API Function
// DWORD GetShortPathName(
// LPCTSTR lpszLongPath, // file for which to get short path
// LPTSTR lpszShortPath, // short path name (output)
// DWORD cchBuffer // size of output buffer
// );

[DllImport("Kernel32", CharSet = CharSet.Auto)]
static extern Int32 GetShortPathName(
String path, // input string
StringBuilder shortPath, // output string
Int32 shortPathLength); // StringBuilder.Capacity

The function returns a "string".

System.String is immutable.

System.Text.StringBuilder seems as a good choice for
returning a "string" in.

Do you have any better suggestions?

Technically the glue that .NET uses knows how to
handle a StringBuilder.

Arne
 

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