windows CoTaskMemAlloc 的用法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/393509/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Usage of CoTaskMemAlloc?
提问by atVelu
When is it appropriate to use CoTaskMemAlloc? Can someone give an example?
什么时候使用 CoTaskMemAlloc 合适?有人可以举个例子吗?
采纳答案by Jason S
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.
天哪,我不得不为这个考虑一段时间——我已经用 ATL 完成了大量的小规模 COM 编程,但很少使用它。
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, the IMalloc
interface pointer obtained from SHGetMalloc
(deprecated) or CoGetMalloc
-- 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.
不过,我想到了一种情况:Windows Shell 扩展。如果您正在处理一组文件系统对象,您可能必须处理PIDL(指向 ID 列表的指针)。这些是奇怪的小文件系统对象抽象,它们需要使用 COM 感知分配器(如CoTaskMemAlloc
. 还有一种替代方法,IMalloc
从SHGetMalloc
(已弃用)或CoGetMalloc
--它只是一个抽象层获得的接口指针,以便您的代码不依赖于特定的内存分配器,并且可以使用任何适当的分配器。
The point of using CoTaskMemAlloc
or IMalloc
rather than malloc()
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). Neither malloc()
nor new
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.
使用CoTaskMemAlloc
orIMalloc
而不是的重点malloc()
是内存分配/释放需要是“COM-aware”的,以便在运行时一致地执行其分配和释放,即使分配和释放是由完全不相关的代码(例如,Windows 分配内存,将其传输到稍后释放的 C++ 代码,或者您的 C++ 代码分配,将其传输到其他人的 VB 代码,稍后释放)。既malloc()
不能也不能new
与系统的运行时堆互操作,因此您不能使用它们来分配内存以传输到其他 COM 对象,或从其他 COM 对象接收内存并取消分配。
回答by Maurice Flanagan
Use CoTaskMemAlloc when returning a char* from a native C++ library to .NET as a string.
将 char* 从本机 C++ 库作为字符串返回到 .NET 时,请使用 CoTaskMemAlloc。
C#
C#
[DllImport("test.dll", CharSet=CharSet.Ansi)]
extern static string Foo();
C
C
char* Foo()
{
std::string response("response");
int len = response.length() + 1;
char* buff = (char*) CoTaskMemAlloc(len);
strcpy_s(buff, len, response.c_str());
return buff;
}
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.
由于 .NET 使用 CoTaskMemFree,您必须像这样分配字符串,不能使用 malloc / new 在堆栈或堆上分配它。
回答by reuben
This MSDN articlecompares 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.
这篇 MSDN 文章比较了一些 Win32 公开的各种分配器,包括 CoTaskMemAlloc。它主要用于 COM 编程——尤其是当 COM 服务器的实现需要分配内存以返回给客户端时。如果您不编写 COM 服务器,那么您可能不需要使用它。
(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.)
(但是,如果您调用使用 CoTaskMemAlloc 分配内存并将其返回给您的代码,则需要使用CoTaskMemFree释放返回的分配。)
回答by Arnoud Mulder
there is not really much which can go wrong as the following calls all end up with the same allocation:
由于以下调用都以相同的分配结束,因此没有太多可能出错:
CoTaskMemAlloc/SHAlloc -> IMalloc.Alloc -> GlobalAlloc(GMEM_FIXED)
only if you use non-windows (compiler-library) calls like malloc()
things will go wrong.
仅当您使用非 Windows(编译器库)调用时,malloc()
才会出错。
Officially one should use CoTaskMemAlloc
for COM calls (like allocating a FORMATETC.ptd field)
官方应该CoTaskMemAlloc
用于 COM 调用(如分配 FORMATETC.ptd 字段)
That CoTaskMemAlloc
equals GlobalAlloc()
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 prescribe GlobalAlloc()
这CoTaskMemAlloc
等于GlobalAlloc()
将保持这种方式,直到在剪贴板 api 与 com STGMEDIUM 上看到永恒。STGMEDIUM 使用剪贴板结构和方法,而 STGMEDIUM 是 com,因此是 CoTaskMemAlloc,剪贴板 api 规定GlobalAlloc()
回答by Vinay
CoTaskMemAlloc is same as malloc except that former is used to allocate memory which is used across process boundaries.
CoTaskMemAlloc 与 malloc 相同,只是前者用于分配跨进程边界使用的内存。
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.
即,如果我们有两个进程,process1 和 process2,假设 process1 是一个 COM 服务器,而 process2 是一个 COM 客户端,它使用 process1 公开的接口。如果process1要发送一些数据,那么他可以使用CoTaskMemAlloc分配内存来分配内存并复制数据。process2 可以访问该内存位置。
COM library automatically does the marshalling and unmarshalling.
COM 库自动进行编组和解组。