2010-03-24 39 views
7

我看RegisterHotKey功能:如何在C#.Net中將空指針傳遞給Win32 API?

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在第一個參數,它工作在大多數情況下,精細遍。但是現在我需要故意傳遞一個空指針作爲第一個參數,而IntPtr(故意)不會這樣做。我剛接觸.Net,這讓我感到困惑。我怎樣才能做到這一點?

+0

其它方式傳遞的第一個參數:https://stackoverflow.com/questions/47997942/how-do-i-handle-optional -c-dll-struct-arguments -in -c-sharp – River

回答

15

使用IntPtr.ZeroNULL

例如:

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

啊,我的印象是IntPtr.Zero代表了一個非空指針,其整數值爲0. –

+2

@Thom,'IntPtr.Zero'是一個指針的地址爲0.它實際上沒有指向任何內容,因爲解引用地址0幾乎肯定會導致崩潰或異常RTS。 C++值NULL具有相同的行爲(地址爲0的指針),因此它與「IntPtr.Zero」很好地匹配 – JaredPar

相關問題