关于 IntPtr 的问题

发布于 2024-09-27 09:50:37 字数 433 浏览 0 评论 0原文

当我进行一些 P/Invoke 或 COM InterOP 时,我经常会遇到 IntPtr。所以这里有几个问题:

  • 每个人都说 IntPtr 是一个struct,那么它里面有什么?我只能想到一个32位/64位整数,这是它指向的地址。还有什么吗?

  • IntPtr 位于哪里?如果它是一个结构,我相信它应该位于 CLR托管堆栈中。但是,如果我在执行 p/invoke 时将 IntPtr 传递给非托管方法怎么办,它不是传递给非托管堆栈吗?它会被复制到非托管堆栈吗?进程地址空间中有托管堆和非托管堆,这种分离存在于进程地址空间中吗?进程的堆栈?

When I make some P/Invoke or COM InterOP, I often bump into the IntPtr. So here is a couple of questions:

  • Everyone said that IntPtr is a struct, so what's in it? I can only think of an 32bit/64bit integer which is the address it points to. Anything else?

  • Where is the IntPtr located? If it is a struct, I believe it should be in the CLR managed stack. But what if I pass an IntPtr to an unmanaged method when doing p/invoke, isn't it passed to the unmanaged stack? Will it be copied to the unmanaged stack? There're managed and unmanaged heaps in a process address space, does this seperation also exist for a process' stack?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

无风消散 2024-10-04 09:50:37

IntPtr 只是一个保证为指针大小的整数。因此,它通常用于在使用指针被认为“不安全”的上下文中包含指针。

但是,就像指针一样,它只是一个数字。如果它包含地址,则该地址在此过程中可能有效或无效。它可以是对齐的或未对齐的,它可以是当前线程的堆栈或另一个线程的堆栈或托管堆或某些非托管堆上的位置。这只是一个数字;将该数字解释为指针是否有意义或安全由您决定。

An IntPtr is simply an integer that is guaranteed to be the size of a pointer. Because of that, it is often used to contain pointers in contexts where it would be considered "unsafe" to have a pointer.

But, like a pointer, it's just a number. If it contains an address then the address could be valid or invalid in this process. It could be aligned or unaligned, it could be to a location on the current thread's stack or another thread's stack or the managed heap or some unmanaged heap. It's just a number; whether the interpretation of that number as a pointer is meaningful or safe is up to you to determine.

梦明 2024-10-04 09:50:37
  • 是的,这只是一个指针。仅此而已。

  • 我认为您已经接受了“结构位于堆栈上,类位于堆上”的神话。我强烈建议您阅读 Eric Lippert 的 关于此的博客文章 (和另一个)。

  • Yes, it's just a pointer. That's all.

  • I think you've bought into the "structs are on the stack, classes are on the heap" myth. I strongly recommend that you read Eric Lippert's blog entry on this (and another one).

生生不灭 2024-10-04 09:50:37

大家都说IntPtr是一个结构体,那么它里面有什么呢?我只能想到一个32位/64位整数,这是它指向的地址。还有什么吗?

使用Reflector,您可以看到它只包含一个实例字段,类型为void*

private unsafe void* m_value;

IntPtr 位于哪里?如果它是一个结构体,我相信它应该位于 CLR 托管堆栈中。但是,如果我在执行 p/invoke 时将 IntPtr 传递给非托管方法怎么办,它不是传递给非托管堆栈吗?会被复制到非托管堆栈吗?进程地址空间中有托管堆和非托管堆,进程的堆栈也存在这种分离吗?

首先,结构不一定存储在堆栈上。这是一个常见的误解:它们可以存储在堆栈中,但这并不意味着情况总是如此(请参阅本文了解详细信息)。

此外,不存在“进程堆栈”:每个线程都有自己的堆栈。

我不知道 p/invoke 如何工作的所有细节,但基本上,是的,IntPtr 值被复制到非托管内存。

如果您想了解有关与非托管代码互操作的更多信息,您可能会对本文感兴趣< /a>.

Everyone said that IntPtr is a struct, so what's in it? I can only think of an 32bit/64bit integer which is the address it points to. Anything else?

Using Reflector, you can see that it contains only one instance field, of type void*

private unsafe void* m_value;

Where is the IntPtr located? If it is a struct, I believe it should be in the CLR managed stack. But what if I pass an IntPtr to an unmanaged method when doing p/invoke, isn't it passed to the unmanaged stack? Will it be copied to the unmanaged stack? There're managed and unmanaged heaps in a process address space, does this seperation also exist for a process' stack?

First, structs are not necessarily stored on the stack. This is a common misconception: they can be stored on the stack, but it doesn't mean it's always the case (see this article for details).

Also, there is no "process stack": each thread has its own stack.

I don't know all the details of how p/invoke works, but basically, yes, the IntPtr value is copied to unmanaged memory.

If you want more information about interop with unmanaged code, you might be interested in this article.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文