When is it appropriate to use CoTaskMemAlloc? Can someone give an example?
相关问题
- the application was unable to start correctly 0xc0
- Inheritance impossible in Windows Runtime Componen
- how to get running process information in java?
- Is TWebBrowser dependant on IE version?
- Handle button click in another application
相关文章
- 如何让cmd.exe 执行 UNICODE 文本格式的批处理?
- 怎么把Windows开机按钮通过修改注册表指向我自己的程序
- Warning : HTML 1300 Navigation occured?
- Bundling the Windows Mono runtime with an applicat
- Windows 8.1 How to fix this obsolete code?
- Signing an F# Assembly (Strong name component)
- Why windows 64 still makes use of user32.dll etc?
- CosmosDB emulator can't start since port is al
This MSDN article compares a few of the various allocators exposed by Win32, including CoTaskMemAlloc. It's mainly used in COM programming--most specifically when the implementation of a COM server needs to allocate memory to return back to a client. If you aren't writing a COM server, then you probably don't need to use it.
(However, if you call code that allocates memory using CoTaskMemAlloc and returns it back to you, you'll need to free the returned allocation(s) using CoTaskMemFree.)
there is not really much which can go wrong as the following calls all end up with the same allocation:
only if you use non-windows (compiler-library) calls like
malloc()
things will go wrong.Officially one should use
CoTaskMemAlloc
for COM calls (like allocating a FORMATETC.ptd field)That
CoTaskMemAlloc
equalsGlobalAlloc()
will stay this way 'till eternity is seen at the clipboard api versus com STGMEDIUM. The STGMEDIUM uses the clipboard structures and method and while STGMEDIUM is com and thus CoTaskMemAlloc, the clipboard apis prescribeGlobalAlloc()
Use CoTaskMemAlloc when returning a char* from a native C++ library to .NET as a string.
C#
C
Since .NET uses CoTaskMemFree, you have to allocate the string like this, you can't allocate it on the stack or the heap using malloc / new.
CoTaskMemAlloc is same as malloc except that former is used to allocate memory which is used across process boundaries.
i.e., if we have two processes, process1 and process2, assume that process1 is a COM server, and process2 is a COM Client which uses the interfaces exposed by process1. If process1 has to send some data, then he can allocate memory using CoTaskMemAlloc to allocate the memory and copies the data. That memory location can be accessed by process2.
COM library automatically does the marshalling and unmarshalling.
Gosh, I had to think for a while for this one -- I've done a fair amount of small-scale COM programming with ATL and rarely have had to use it.
There is one situation though that comes to mind: Windows Shell extensions. If you are dealing with a set of filesystem objects you might have to deal with PIDLs (pointer to an ID list). These are bizarre little filesystem object abstractions and they need to be explicitly allocated/deallocated using a COM-aware allocator such as
CoTaskMemAlloc
. There is also an alternative, theIMalloc
interface pointer obtained fromSHGetMalloc
(deprecated) orCoGetMalloc
-- it's just an abstraction layer to use, so that your code isn't tied to a specific memory allocator and can use any appropriate one.The point of using
CoTaskMemAlloc
orIMalloc
rather thanmalloc()
is that the memory allocation/deallocation needs to be something that is "COM-aware" so that its allocation and deallocation are performed consistently at run-time, even if the allocation and deallocation are done by completely unrelated code (e.g. Windows allocates memory, transfers it to your C++ code which later deallocates, or your C++ code allocates, transfers it to someone else's VB code which later deallocates). Neithermalloc()
nornew
are capable of interoperating with the system's run-time heap so you can't use them to allocate memory to transfer to other COM objects, or to receive memory from other COM objects and deallocate.