如果它在本地堆上分配,为什么将其称为Marshal.AllocHGlobal? [英] Why is it called Marshal.AllocHGlobal if it allocates on the local heap?

查看:128
本文介绍了如果它在本地堆上分配,为什么将其称为Marshal.AllocHGlobal?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自 Marshal.AllocHGlobal 的MSDN文档:

From the MSDN documentation of Marshal.AllocHGlobal:

AllocHGlobal是Marshal类中的两种内存分配方法之一.此方法从Kernel32.dll中公开Win32 LocalAlloc函数.

AllocHGlobal is one of two memory allocation methods in the Marshal class. This method exposes the Win32 LocalAlloc function from Kernel32.dll.

考虑到有一个GlobalAlloc API,它在 global 堆而不是 local 堆上分配内存,不是此方法的名称,而是误导?

Considering there's a GlobalAlloc API which allocates memory on the global heap, rather than the local heap, isn't this method's name rather misleading?

是否有理由将其命名为AllocHGlobal,而不是AllocHLocal?

Was there a reason for naming it AllocHGlobal, rather than AllocHLocal?

更新:西蒙在评论中指出,Windows中不再存在诸如全局堆之类的东西,并且GlobalAllocLocalAlloc API仅保留用于遗留目的.如今,GlobalAlloc API只不过是LocalAlloc的包装.

Update: Simon points out in the comments that there's no such thing as a global heap in Windows any more, and the GlobalAlloc and LocalAlloc APIs remained for legacy purposes only. These days, the GlobalAlloc API is nothing morethan a wrapper for LocalAlloc.

这解释了为什么API根本不调用GlobalAlloc的原因,但是没有解释为什么当API不(不能)使用全局堆时也将其命名为AllocHGlobal的原因.甚至调用GlobalAlloc.命名可能不是出于遗留原因,因为它是在.NET 2.0之前才引入的,即在放弃16位支持之后.因此,问题仍然存在:为什么Marshal.AllocHGlobal如此具有误导性?

This explains why the API doesn't call GlobalAlloc at all, but it doesn't explain why the API was named AllocHGlobal when it doesn't (can't) use a global heap, nor does it even call GlobalAlloc. The naming cannot possibly be for legacy reasons, because it wasn't introduced until .NET 2.0, way after 16-bit support was dropped. So, the question remains: why is Marshal.AllocHGlobal so misleadingly named?

推荐答案

假设您正在使用拖放操作或在剪贴板上进行应用程序之间的数据传输.要填充 STGMEDIUM 结构,您需要一个HGLOBAL.因此,您呼叫AllocHGlobal.因此得名.

Suppose you're doing data transfer between apps using drag and drop or over the clipboard. To populate the STGMEDIUM structure you need an HGLOBAL. So you call AllocHGlobal. Hence the name.

此功能的主要用途是与需要HGLOBAL的API互操作.如果将其称为其他名称,将会造成混淆,因为当您想要HGLOBAL时,您必须找到一些文档来告诉您AllocAnythingElse产生了可以用作HGLOBAL的值.

The main use for this function is to interop with APIs that want an HGLOBAL. It would be confusing if it was called anything else because when you wanted an HGLOBAL you'd have to find some documentation to tell you that AllocAnythingElse produced a value you could use as an HGLOBAL.

这篇关于如果它在本地堆上分配,为什么将其称为Marshal.AllocHGlobal?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆