代码之家  ›  专栏  ›  技术社区  ›  Thom Smith

如何将空指针传递到C.NET中的win32 api?

  •  9
  • Thom Smith  · 技术社区  · 15 年前

    我正在查看注册表热键功能:

    http://msdn.microsoft.com/en-us/library/ms646309(VS.85).aspx

    BOOL RegisterHotKey(
      __in  HWND hWnd,
      __in  int id,
      __in  UINT fsModifiers,
      __in  UINT vk
    );
    

    我一直在用 IntPtr 传递第一个参数,这在大多数情况下都很好。但是现在我需要故意传递一个空指针作为第一个参数,它 国际乒乓球联合会 (故意)不会。我刚接触.NET,这让我很困惑。我该怎么做?

    1 回复  |  直到 11 年前
        1
  •  18
  •   JaredPar    15 年前

    使用 IntPtr.Zero 对于 NULL

    例如:

    public void Example() {
      ...
      RegisterHotKey(IntPtr.Zero, id, mod, vk);
    }
    
    [DllImportAttribute("user32.dll", EntryPoint="RegisterHotKey")]
    [return: MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
    public static extern bool RegisterHotKey(
      IntPtr hWnd, 
      int id, 
      uint fsModifiers, 
      uint vk);