10

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, 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?

1

1 Answer 1

20

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);
Sign up to request clarification or add additional context in comments.

2 Comments

Ah, I was under the impression that IntPtr.Zero represented a non-null pointer to an integer value of 0.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.