Home > Software engineering >  How is CoTaskMemAlloc() a replacement for SHGetMalloc()?
How is CoTaskMemAlloc() a replacement for SHGetMalloc()?

Time:09-18

In this link, it is said that CoTaskMemAlloc() is a replacement for SHGetMalloc().

However, those functions are using different parameter types and return value types.

So, how can the one be a replacement for the other?

Can someone please explain this?

I'm just trying to convert the old code for the new API and make sure it runs properly.

CodePudding user response:

It's not a drop-in replacement, in this case, "replacement for" means that you should now use CoTaskMemAlloc in places where you would have previously relied on SHGetMalloc.

Note that SHGetMalloc returns an allocator (which you would then presumably use), CoTaskMemAlloc obviates the need for that intermediate step, it is a much simpler call pattern.

CodePudding user response:

CoTaskMemAlloc is a replacement for SHGetMalloc in the sense that they are guaranteed to allocate from the same heap. The real replacement for SHGetMalloc is CoGetMalloc and CoTaskMemAlloc just calls CoGetMalloc internally.

SHAlloc/SHGetMalloc/SHFree exist because Windows 95 had a minimum RAM requirement of 4 MiB! To pull this off without paging to disk all the time shell32.dll implemented a mini version of COM to avoid loading ole32.dll into memory in Explorer.exe.

See also:

  • Related