C
C#16mo ago
heftymouse

✅ CoTaskMemFree on a buffer returned by CsWin32

How do I do this?
9 Replies
Martindb
Martindb16mo ago
$ask
MODiX
MODiX16mo ago
How to get the best help catpog Make a post in #help or one of the topic channels under Development. Avoid askingcatthinking Can anybody help catthinking Has anyone used catthinking Why doesn't my code work? C# is a big area! No one knows they can help unless you tell them about the small area you're trying to work in. Explain what you are doing, and potentially why for as much context as possible. Avoid screenshots where possible, share code directly in Discord. Type $code into chat to learn how to post code. See https://www.nohello.net and https://dontasktoask.com if you want common help chat room etiquette.
Jester
Jester16mo ago
just call CoTaskMemFree on that buffer? which api are you using that you need to do this?
reflectronic
reflectronic16mo ago
Marshal.FreeCoTaskMem(IntPtr) Method (System.Runtime.InteropServices)
Frees a block of memory allocated by the unmanaged COM task memory allocator.
heftymouse
heftymouseOP15mo ago
my code right now looks like this:
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Shell;

PInvoke.SHCreateItemFromParsingName("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}", null, typeof(IShellItem).GUID, out var e);
IShellItem shellItem = e as IShellItem;
unsafe
{
PWSTR pwstr;
shellItem.GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY, &pwstr);
string thing = new string((char*)pwstr);
Console.WriteLine(thing);
// free pwstr here somehow?
}
using System.Runtime.InteropServices;
using Windows.Win32;
using Windows.Win32.Foundation;
using Windows.Win32.UI.Shell;

PInvoke.SHCreateItemFromParsingName("shell:::{679f85cb-0220-4080-b29b-5540cc05aab6}", null, typeof(IShellItem).GUID, out var e);
IShellItem shellItem = e as IShellItem;
unsafe
{
PWSTR pwstr;
shellItem.GetDisplayName(SIGDN.SIGDN_NORMALDISPLAY, &pwstr);
string thing = new string((char*)pwstr);
Console.WriteLine(thing);
// free pwstr here somehow?
}
Marshal.FreeCoTaskMem dosn't accept the pointer to pwstr directly it seems, and trying to use CoTaskMemFree itself results in an access violation sorry for posting such a half-baked question
reflectronic
reflectronic15mo ago
you don't pass the pointer to PWSTR to CoTaskMemFree you pass PWSTR itself Marshal.FreeCoTaskMem((nint)pwstr);
heftymouse
heftymouseOP15mo ago
this complains about not being able to cast
reflectronic
reflectronic15mo ago
(nint)pwstr.Value?
heftymouse
heftymouseOP15mo ago
that worked, thanks a ton

Did you find this page helpful?