Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass null pointer to Win32 API in C# .Net?

Tags:

c#

null

pinvoke

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?

like image 539
Thom Smith Avatar asked Mar 24 '10 17:03

Thom Smith


1 Answers

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);
like image 126
JaredPar Avatar answered Sep 28 '22 23:09

JaredPar