Reputation:
I have a couple of questions regarding the following:
[DllImport("libmp3lame.dll", CharSet = CharSet.Ansi)]
static extern IntPtr get_lame_version();
public static string GetLameVersion()
{
IntPtr pVersion = get_lame_version();
string version = Marshal.PtrToStringAnsi(pVersion);
return version;
}
pVersion
allocated?pVersion
goes out of scope?Upvotes: 1
Views: 159
Reputation: 613013
The string returned by this function is statically allocated and you do not need to free that memory. This means that your current code is already exactly what you need.
This is an open source project and so a websearch leads to the source code for the implementation of this function to confirm this.
As an aside, your p/invoke is incorrect, although it is benign. It should be:
[DllImport("libmp3lame.dll", CallingConvention=CallingConvention.Cdecl)]
static extern IntPtr get_lame_version();
There's no need to specify CharSet
since the function has no text parameters. And in any case Ansi
is the default so you still would not need to specify that. The calling convention is, in general, important and will need to be set for all your LAME imports. It doesn't actually matter for a function with no parameters, but specifying the calling convention is a good habit to get in to.
Upvotes: 3