本文介绍了是IntPtr.Zero相当于空?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图安装的ReadFile 异步根据的,我需要设置 lpNumberOfBytesRead

I am trying to setup ReadFile to run asynchronously and according to MSDN, I need to set lpNumberOfBytesRead to null:

对,如果这是一个异步操作,以避免潜在的错误结果这个参数使用NULL。

例如,如果我有以下几点:

For example, if I have the following:

  [DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  public static extern bool ReadFile(
     IntPtr hFile,
     out byte[] aBuffer,
     int cbToRead,
     IntPtr cbThatWereRead,
     ref OVERLAPPED pOverlapped
  );

和我这样称呼它(与具有4个参数是空的意图):

and I call it like this (with the intention of having the 4th parameter being null):

Win32API.ReadFile(readHandle, out data_read, Win32API.BUFFER_SIZE, IntPtr.Zero, ref over_lapped);

那将是一样的使用NULL调用?如果不是这样,我应该在报关或功能改变自称?

will that be the same as calling it with null? If not, what should I change in the declaration or in the function call itself?

我也好奇,如果我应该使用的SafeHandle HandleRef 而不是的IntPtr HFILE 引用?我知道,以确保我关闭与 CloseHandle的(IntPtr的)把手当我用它做的,只是不知道是否有任何行吟诗人理由使用其他两个在 IntPtr的选项。我也tryingn避免使用不安全的code。

I was also curious if I should be using SafeHandle or HandleRef instead of IntPtr for the hFile reference? I know to make sure that I close the handle with CloseHandle(IntPtr) when I'm done with it, just not sure if there is any othe reason to use the other two options over IntPtr. I am also tryingn to avoid using unsafe code.

编辑:事实证明,我不应该是第四个参数设置为 IntPtr.Zero ,无论如何,因为即使我异步运行,它仍然可以立即返回。请参见。啊,我喜欢矛盾的故事。

As it turns out, I shouldnt be setting the fourth parameter to IntPtr.Zero anyway, because even though I am running asynchronously, it could still return right away. See Asynchronous Disk I/O. Ahh, I love contradicting stories.

推荐答案

有关的P / Invoke的目的,就像你上市,你应该代替使用 IntPtr.Zero NULL 。请注意,这并不等同于C#关键字,但是。

For P/Invoke purposes like you've listed, you should use IntPtr.Zero in place of NULL. Note that this is not equivalent to the C# null keyword, however.

这篇关于是IntPtr.Zero相当于空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-23 17:06