⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 win32utilities.cs

📁 在windows mobile 5上的地图查看器
💻 CS
字号:
using System;
using System.Runtime.InteropServices;
using Win32 = Microsoft.WindowsMobile.Utilities.Win32Utilities.SafeNativeMethods;

namespace Microsoft.WindowsMobile.Utilities
{
    /// <summary>
    /// Calls to OS where .netcf lacks.  
    /// when .netcf 2.0 is out we should put utility classes
    /// like this into a "friend internal" assembly so we only have one
    /// </summary>
    public class Win32Utilities
    {
        private Win32Utilities()    {}

        public static Guid CreateGuid()
        {
            Win32.Guid data;
            Win32.CoCreateGuid(out data);
            return new Guid(data.data1, data.data2, data.data3, 
                data.byte0, data.byte1, data.byte2, data.byte3, data.byte4,  
                data.byte5, data.byte6, data.byte7);
        }


        public static long MakeLong(byte[] bytes)
        {
            if (bytes == null || bytes.Length > 8)
            {
                throw new ArgumentException("bytes array cannot be more than 8 elements");
            }
            
            long madeLong = 0;
            
            for (int index = bytes.Length - 1; index >= 0; index--)
            {
                madeLong = madeLong << 8;
                madeLong = madeLong | bytes[index];
            }
            
            return madeLong;
        }

        public static DateTime LocalFileTimeToLocalDateTime(byte[] time)
        {
                if (time == null)
                {
                    throw new ArgumentException();
                }
                
                return DateTime.FromFileTime(MakeLong(time));
        }
            
        public static DateTime UtcFileTimeToLocalDateTime(byte[] time)
        {
                if (time == null)
                {
                    throw new ArgumentException();
                }
                
                return DateTime.FromFileTimeUtc(MakeLong(time)).ToLocalTime();
        }


        public static IntPtr LocalAlloc(int byteCount)
        {
            IntPtr ptr = Win32.LocalAlloc(Win32.LMEM_ZEROINIT, byteCount);
            if (ptr == IntPtr.Zero)
            {
                throw new OutOfMemoryException();
            }

            return ptr;
        }

        public static void LocalFree(IntPtr hMem)
        {
            IntPtr ptr = Win32.LocalFree(hMem);
            if (ptr != IntPtr.Zero)
            {
                throw new ArgumentException();
            }
        }

        public static IntPtr StringToPtr(string str)
        {
            // get bytes of the string
            byte[] bytes = System.Text.Encoding.Unicode.GetBytes(str);

            // allocate string on native side
            IntPtr strPtr = LocalAlloc(bytes.Length);

            // copy the string into the native buffer
            for (int index = 0; index < bytes.Length; index++)
            {
                Marshal.WriteByte(strPtr, index, bytes[index]);
            }

            // return the buffer.  this will need to be freed
            return strPtr;
        }

        /// <summary>
        /// Returns the full path to the currently running executable.
        /// </summary>
        /// <returns>The full path to the currently running executable</returns>
        public static string GetCurrentExecutablePath()
        {
            IntPtr sz = LocalAlloc(Win32.MAX_PATH * 2);
            IntPtr hModule = Win32.GetModuleHandle(null);
            Win32.GetModuleFileName(hModule, sz, Win32.MAX_PATH);
            string fileName = Marshal.PtrToStringUni(sz);
            LocalFree(sz);
            return fileName;
        }

        public sealed class SafeNativeMethods 
        {
            public const int MAX_PATH = 260;

            #region Misc PInvoke
            public struct WindowMessage
            {
                public IntPtr hwnd;
                public int   msg;
                public int   wparam;
                public int   lparam;
                public int   time;
                public int    ptx;
                public int    pty;
            }

            public struct Guid
            {
                public int data1;
                public short data2;
                public short data3;
                public byte  byte0;
                public byte  byte1;
                public byte  byte2;
                public byte  byte3;
                public byte  byte4;
                public byte  byte5;
                public byte  byte6;
                public byte  byte7;
            }
 
            [DllImport("coredll.dll")]
            public static extern int GetMessage(out WindowMessage m, IntPtr hwnd, int min, int max);

            [DllImport("coredll.dll")]
            public static extern int PeekMessage(out WindowMessage m, IntPtr hwnd, int min, int max, int removeMsg);
 
            [DllImport("coredll.dll")]
            public static extern int TranslateMessage(ref WindowMessage m);
 
            [DllImport("coredll.dll")]
            public static extern int DispatchMessage(ref WindowMessage m);


            [System.Runtime.InteropServices.DllImport("coredll.dll")]
            public extern static bool SetWindowText(IntPtr hWnd, string lpString);

            public const int LMEM_ZEROINIT = 0x40;
            [System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint="#33", SetLastError=true) ]
            public static extern IntPtr LocalAlloc(int flags, int byteCount);

            [System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint="#36", SetLastError=true) ]
            public static extern IntPtr LocalFree(IntPtr hMem); 

            [System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint="#537") ]
            public static extern int GetModuleFileName(IntPtr hModule, IntPtr lpFileName, int nSize); 

            [System.Runtime.InteropServices.DllImport("coredll.dll", EntryPoint="#1177") ]
            public static extern IntPtr GetModuleHandle(string moduleName); 


            [System.Runtime.InteropServices.DllImport("ole32.dll", SetLastError=true) ]
            public static extern IntPtr CoCreateGuid(out Win32.Guid pGuid); 

            #endregion

            #region Registry PInvoke
            [System.Runtime.InteropServices.DllImport("coredll.dll")]
            public extern static int RegOpenKeyEx(IntPtr hKey, string lpSubKey, int ulOptions, 
                int samDesired, ref IntPtr phkResult);
     
            [System.Runtime.InteropServices.DllImport("coredll.dll")]
            public extern static int RegCloseKey(IntPtr hKey);

            [System.Runtime.InteropServices.DllImport("coredll.dll")]
            public extern static int RegDeleteKey(IntPtr hKey, string lpSubKey);

            [System.Runtime.InteropServices.DllImport("coredll.dll")]
            public extern static int RegCreateKeyEx(IntPtr hKey, string lpSubKey, int reserved, 
                string lpClass, int dwOptions, int samDesired, IntPtr lpSecurityAttributes, 
                ref IntPtr phkResult, ref IntPtr lpdwDisposition);

            [System.Runtime.InteropServices.DllImport("coredll.dll")]
            public extern static int RegSetValueEx(IntPtr hKey, string lpValueName, int reserved,
                int dwType, byte[] lpData, int dbData);

            [System.Runtime.InteropServices.DllImport("coredll.dll")]
            public extern static int RegQueryValueEx(IntPtr hKey, string lpValueName, int reserved,
                ref int dwType, byte[] lpData, ref int lpcbData);

            #endregion

            /// <summary>
            /// constructor so default constructor is not created since all methods are static
            /// </summary>
            private SafeNativeMethods() {}
        }

    }


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -