单声道使dllimport有条件?

时间:2018-03-26 22:38:05

标签: c# .net mono monodevelop dllimport

所以,我有以下代码

    [DllImport( "kernel32.dll" )]
    static extern int GetCurrentThreadId();

    [DllImport( "user32.dll" )]
    private static extern bool GetWindowRect( IntPtr hWnd, ref Rectangle lpRect );

    [DllImport( "user32.dll" )]
    private static extern int MoveWindow( IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint );

    [DllImport( "user32.dll" )]
    [return: MarshalAs( UnmanagedType.Bool )]
    private static extern bool SetWindowPos( IntPtr hWnd, IntPtr hWndInsertAfter, int x, int y, int cx, int cy, SetWindowPosFlags uFlags );

    [DllImport( "User32.dll" )]
    public static extern UIntPtr SetTimer( IntPtr hWnd, UIntPtr nIDEvent, uint uElapse, TimerProc lpTimerFunc );

    [DllImport( "User32.dll" )]
    public static extern IntPtr SendMessage( IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam );

    [DllImport( "user32.dll" )]
    public static extern IntPtr SetWindowsHookEx( int idHook, HookProc lpfn, IntPtr hInstance, int threadId );

    [DllImport( "user32.dll" )]
    public static extern int UnhookWindowsHookEx( IntPtr idHook );

    [DllImport( "user32.dll" )]
    public static extern IntPtr CallNextHookEx( IntPtr idHook, int nCode, IntPtr wParam, IntPtr lParam );

    [DllImport( "user32.dll" )]
    public static extern int GetWindowTextLength( IntPtr hWnd );

    [DllImport( "user32.dll" )]
    public static extern int GetWindowText( IntPtr hWnd, StringBuilder text, int maxLength );

    [DllImport( "user32.dll" )]
    public static extern int EndDialog( IntPtr hDlg, IntPtr nResult );

我想让程序与mono兼容,我得到了我需要使用托管函数,但是如果在Windows上我可以使用DLLImport,如果没有,我可以使用自定义的,或者我必须坚持使用所有平台上的两个中的一个?

1 个答案:

答案 0 :(得分:1)

在这些情况下,使用条件编译可能是更清晰的方法,并为每个平台都有一个库。

#if WINDOWS 
    // Load Stuff 
#endif



#if LINUX
    // Load other stuff
#endif