tags:

views:

158

answers:

1

I'm looking at the RegisterHotKey Function:

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
);

I've been using IntPtr to pass in the first argument,m which works fine in most cases. But now I need to deliberately pass a null pointer as the first argument, which IntPtr (deliberately) will not do. I'm new to .Net, and this has me perplexed. How can I do this?

+5  A: 

Use IntPtr.Zero for NULL

For example:

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);
JaredPar
Ah, I was under the impression that IntPtr.Zero represented a non-null pointer to an integer value of 0.
Thom Smith
@Thom, `IntPtr.Zero` is a pointer with the address of 0. It actually points to nothing because dereferencing the address 0 will almost certainly lead to a crash or exception of sorts. The C++ value NULL has the same behavior (pointer with address of 0) hence it matches nicely with `IntPtr.Zero`
JaredPar