예제 #1
0
        internal IntPtr ToUniStr(bool allocateFromHeap)
        {
            IntPtr ptr;

            EnsureNotDisposed();

            // Demand Permission
            //new CryptographicPermission( CryptographicPermissionFlags.Decrypt ).Demand();

            if (allocateFromHeap)
            {
                ptr = MarshalEx.AllocHGlobal((m_length + 1) * 2);
            }
            else
            {
                ptr = MarshalEx.AllocHLocal((m_length + 1) * 2);
                //ptr = MarshalEx.AllocCoTaskMem( (m_length+1) * 2 );
            }

            try
            {
                this.UnProtectMemory();
                Win32Native.memset(ptr, 0, (uint)((m_length + 1) * 2));
                //Win32Native.ZeroMemory( ptr, (uint)((m_length+1) * 2) );
                Marshal.Copy(m_buffer, 0, ptr, m_length * 2);
                return(ptr);
            }
            finally
            {
                // Assert Permission
                //new CryptographicPermission( CryptographicPermissionFlags.Encrypt ).Assert();
                ProtectMemory();
            }
        }
예제 #2
0
 internal void InitFromData(byte[] data, int datalength)
 {
     m_cbdata = datalength;
     m_lpData = MarshalEx.AllocHGlobal(m_cbdata);
     if (data != null)
     {
         Marshal.Copy(data, 0, m_lpData, m_cbdata);
     }
     m_hdr      = new WaveHdr((int)m_lpData.ToInt32(), m_cbdata);
     m_cbHeader = m_hdr.ToByteArray().Length;
     m_lpHeader = MarshalEx.AllocHGlobal(m_cbHeader);
     byte[] hdrbits = m_hdr.ToByteArray();
     Marshal.Copy(hdrbits, 0, m_lpHeader, m_cbHeader);
 }
예제 #3
0
        private FileVersionInfo(string fileName)
        {
            //get the filename sans path
            m_filename = System.IO.Path.GetFileName(fileName);

            int handle = 0;
            int len    = 0;

            //get size of version info
            len = GetFileVersionInfoSize(fileName, ref handle);

            if (len > 0)
            {
                //allocate buffer
                IntPtr buffer = MarshalEx.AllocHGlobal(len);
                //get version information
                if (GetFileVersionInfo(fileName, handle, len, buffer))
                {
                    IntPtr fixedbuffer = IntPtr.Zero;
                    int    fixedlen    = 0;
                    //get language independant version info
                    //this is a pointer within the main buffer so don't free it
                    if (VerQueryValue(buffer, "\\", ref fixedbuffer, ref fixedlen))
                    {
                        //allocate managed memory
                        m_fixedversioninfo = new byte[fixedlen];
                        //copy to managed memory
                        Marshal.Copy(fixedbuffer, m_fixedversioninfo, 0, fixedlen);
                    }
                    else
                    {
                        throw new WinAPIException("Error retrieving language independant version");
                    }
                }
                else
                {
                    throw new WinAPIException("Error retrieving FileVersionInformation");
                }

                //free native buffer
                MarshalEx.FreeHGlobal(buffer);
            }
        }
예제 #4
0
        /// <summary>
        /// Get an array of available network adapters
        /// </summary>
        /// <returns>aArray of AdapterInfo classes</returns>
        public static AdapterInfo[] GetAdaptersInfo()
        {
            ArrayList adapters = new ArrayList();

            int cb  = 0;
            int ret = GetAdaptersInfoCE(IntPtr.Zero, ref cb);

            IntPtr pInfo = MarshalEx.AllocHGlobal(cb);             //LPTR

            ret = GetAdaptersInfoCE(pInfo, ref cb);
            if (ret == 0)
            {
                AdapterInfo info = new AdapterInfo(pInfo, 0);
                while (info != null)
                {
                    adapters.Add(info);
                    info = info.Next;
                }
            }
            MarshalEx.FreeHGlobal(pInfo);

            return((AdapterInfo[])adapters.ToArray(Type.GetType("OpenNETCF.Net.AdapterInfo")));
        }
예제 #5
0
        /// <summary>
        /// Retrieves notification information associated with a handle.
        /// </summary>
        /// <param name="handle">Handle to the user notification to retrieve.</param>
        /// <returns>The requested UserNotification.</returns>
        public static UserNotificationInfoHeader GetUserNotification(int handle)
        {
            //buffer size
            int size = 0;

            //first query for buffer size required
            CeGetUserNotification(handle, 0, ref size, IntPtr.Zero);

            //create a marshallable buffer
            IntPtr buffer = MarshalEx.AllocHGlobal(size);

            //call native getter
            if (!CeGetUserNotification(handle, (uint)size, ref size, buffer))
            {
                throw new Win32Exception(Marshal.GetLastWin32Error(), "Error getting UserNotification");
            }

            UserNotificationInfoHeader nih = UserNotificationInfoHeader.FromPtr(buffer);

            //free native memory
            MarshalEx.FreeHGlobal(buffer);

            return(nih);
        }
예제 #6
0
        /// <summary>
        /// Places nonpersistent data on the system clipboard.
        /// </summary>
        /// <param name="data">The data to place on the clipboard.</param>
        /// <exception cref="System.ArgumentNullException">The value of data is null.</exception>
        public static void SetDataObject(IDataObject data)
        {
            if (data == null)
            {
                throw new ArgumentNullException("The value of data is null");
            }
            else
            {
                if (!OpenClipboard(IntPtr.Zero))
                {
                    throw new System.Runtime.InteropServices.ExternalException("Could not open Clipboard");
                }

                if (!EmptyClipboard())
                {
                    throw new System.Runtime.InteropServices.ExternalException("Unable to empty Clipboard");
                }

                //from here on we are only supporting unicode text, but it would be possible to support other formats
                if (data.GetDataPresent(DataFormats.UnicodeText))
                {
                    string unicodedata;
                    IntPtr hClipboard;

                    try
                    {
                        //extract unicode string from supplied data
                        unicodedata = data.GetData(DataFormats.UnicodeText).ToString();
                    }
                    catch
                    {
                        throw new FormatException("Clipboard data not in a recognised format");
                    }

                    //marshall the string
                    hClipboard = MarshalEx.StringToHGlobalUni(unicodedata);

                    //pass data to clipboard
                    if (SetClipboardData(ClipboardFormats.UnicodeText, hClipboard) == IntPtr.Zero)
                    {
                        throw new System.Runtime.InteropServices.ExternalException("Could not put data on Clipboard");
                    }
                }
                else if (data.GetDataPresent(DataFormats.Bitmap))
                {
                    System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)data.GetData(DataFormats.Bitmap);

                    int bs = bmp.Width * 3 * bmp.Height;
                    //allocate unmanaged memory
                    IntPtr hClipboard = MarshalEx.AllocHGlobal(bs + 41);
                    byte[] buffer     = new byte[40];

                    MemoryStream strm = new MemoryStream(buffer);
                    BinaryWriter wr   = new BinaryWriter(strm);
                    wr.Write(40);
                    wr.Write(bmp.Width);
                    wr.Write(bmp.Height);
                    wr.Write((short)1);
                    wr.Write((short)0x18);
                    // remaining bytes will be 0
                    wr.Close();

                    MarshalEx.WriteByteArray(hClipboard, 0, buffer);
                    int pos = 40;
                    for (int r = bmp.Height - 1; r >= 0; r--)
                    {
                        for (int c = 0; c < bmp.Width; c++)
                        {
                            int color = bmp.GetPixel(c, r).ToArgb();

                            byte red   = (byte)((color & 0x00ff0000) >> 16);
                            byte green = (byte)((color & 0x0000ff00) >> 8);
                            byte blue  = (byte)(color & 0x000000ff);

                            MarshalEx.WriteByte(hClipboard, pos++, blue);
                            MarshalEx.WriteByte(hClipboard, pos++, green);
                            MarshalEx.WriteByte(hClipboard, pos++, red);
                        }
                    }


                    //pass data to clipboard
                    if (SetClipboardData(ClipboardFormats.Bitmap, hClipboard) == IntPtr.Zero)
                    {
                        throw new System.Runtime.InteropServices.ExternalException("Could not put data on Clipboard");
                    }
                }

                if (!CloseClipboard())
                {
                    throw new System.Runtime.InteropServices.ExternalException("Could not close Clipboard");
                }
            }
        }