PageRenderTime 9ms CodeModel.GetById 1ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/Visual Studio 2008/CSPInvokeDll/NativeMethod.cs

#
C# | 116 lines | 69 code | 19 blank | 28 comment | 0 complexity | 53c2eda633490edc997c5f570071d12b MD5 | raw file
  1/****************************** Module Header ******************************\
  2* Module Name:  NativeMethod.cs
  3* Project:      CSPInvokeDll
  4* Copyright (c) Microsoft Corporation.
  5* 
  6* The PInvoke signatures of the methods exported from the unmanaged DLLs.
  7* 
  8* This source is subject to the Microsoft Public License.
  9* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
 10* All other rights reserved.
 11* 
 12* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, 
 13* EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED 
 14* WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE.
 15\***************************************************************************/
 16
 17#region Using directives
 18using System;
 19using System.Runtime.InteropServices;
 20using System.Text;
 21using System.Security;
 22#endregion
 23
 24
 25namespace CSPInvokeDll
 26{
 27    // Function delegate of the 'PFN_COMPARE' callback function. 
 28    // The delegate type has the UnmanagedFunctionPointerAttribute. Using 
 29    // this attribute, you can specify the calling convention of the native 
 30    // function pointer type. In the native API's header file, the callback 
 31    // PFN_COMPARE is defined as __stdcall (CALLBACK), so here we specify 
 32    // CallingConvention.StdCall.
 33    [UnmanagedFunctionPointer(CallingConvention.StdCall)]
 34    delegate int CompareCallback(int a, int b);
 35
 36
 37    [SuppressUnmanagedCodeSecurity]
 38    class NativeMethod
 39    {
 40        [DllImport("CppDynamicLinkLibrary.dll", CharSet = CharSet.Auto,
 41            CallingConvention = CallingConvention.Cdecl)]
 42        public static extern int GetStringLength1(string str);
 43
 44        [DllImport("CppDynamicLinkLibrary.dll", CharSet = CharSet.Auto,
 45            CallingConvention = CallingConvention.StdCall)]
 46        public static extern int GetStringLength2(string str);
 47
 48        [DllImport("CppDynamicLinkLibrary.dll", CharSet = CharSet.Auto)]
 49        public static extern int Max(int a, int b, CompareCallback cmpFunc);
 50
 51        [DllImport("user32.dll", CharSet = CharSet.Auto)]
 52        public static extern MessageBoxResult MessageBox(IntPtr hWnd,
 53            string text, String caption, MessageBoxOptions options);
 54
 55        [DllImport("msvcrt.dll", CharSet = CharSet.Ansi,
 56            CallingConvention = CallingConvention.Cdecl)]
 57        public static extern int printf(string format, string arg1, string arg2);
 58    }
 59
 60
 61    /// <summary>
 62    /// Flags that define appearance and behaviour of a standard message 
 63    /// box displayed by a call to the MessageBox function.
 64    /// </summary>
 65    [Flags]
 66    enum MessageBoxOptions : uint
 67    {
 68        Ok = 0x000000,
 69        OkCancel = 0x000001,
 70        AbortRetryIgnore = 0x000002,
 71        YesNoCancel = 0x000003,
 72        YesNo = 0x000004,
 73        RetryCancel = 0x000005,
 74        CancelTryContinue = 0x000006,
 75
 76        IconHand = 0x000010,
 77        IconQuestion = 0x000020,
 78        IconExclamation = 0x000030,
 79        IconAsterisk = 0x000040,
 80        UserIcon = 0x000080,
 81
 82        IconWarning = IconExclamation,
 83        IconError = IconHand,
 84        IconInformation = IconAsterisk,
 85        IconStop = IconHand,
 86
 87        DefButton1 = 0x000000,
 88        DefButton2 = 0x000100,
 89        DefButton3 = 0x000200,
 90        DefButton4 = 0x000300,
 91
 92        ApplicationModal = 0x000000,
 93        SystemModal = 0x001000,
 94        TaskModal = 0x002000,
 95
 96        Help = 0x004000, // Help Button
 97        NoFocus = 0x008000,
 98
 99        SetForeground = 0x010000,
100        DefaultDesktopOnly = 0x020000,
101        Topmost = 0x040000,
102        Right = 0x080000,
103        RTLReading = 0x100000,
104    }
105
106
107    /// <summary>
108    /// Represents possible values returned by the MessageBox function.
109    /// </summary>
110    enum MessageBoxResult : uint
111    {
112        Ok = 1, Cancel, Abort, Retry, Ignore,
113        Yes, No, Close, Help, TryAgain, Continue,
114        Timeout = 32000
115    }
116}