Beispiel #1
0
        public static SafeCoTaskMemHandle FromStringAnsi(string s)
        {
            SafeCoTaskMemHandle handle;

            var memory = IntPtr.Zero;

            try
            {
                memory = Marshal.StringToCoTaskMemAnsi(s);

                handle = new SafeCoTaskMemHandle(memory, true);

                memory = IntPtr.Zero;
            }
            finally
            {
                if (memory != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(memory);
                }
            }

            return(handle);
        }
Beispiel #2
0
        public static SafeCoTaskMemHandle Allocate(int size)
        {
            SafeCoTaskMemHandle handle;

            var memory = IntPtr.Zero;

            try
            {
                memory = Marshal.AllocCoTaskMem(size);

                handle = new SafeCoTaskMemHandle(memory, true);

                memory = IntPtr.Zero;
            }
            finally
            {
                if (memory != IntPtr.Zero)
                {
                    Marshal.FreeCoTaskMem(memory);
                }
            }

            return(handle);
        }