मैं एक सांत्वना applicacion पर पाठ की क्लिपबोर्ड पर भेज सकते

वोट
0

मैं एक छोटे से सांत्वना आवेदन सी ​​# पर मेरे कार्यक्रमों के लिए एक छोटा सा CLI बनाने के लिए है, मैं क्लिपबोर्ड कुछ पाठ भेजने और फिर इसे विजुअल स्टूडियो में पेस्ट करना चाहते, मैं हमेशा की तरह की कोशिश की लेकिन असफल, त्रुटि का कहना है कि क्लिपबोर्ड है मान्यता प्राप्त नहीं, whinle मैं एक वर्ग कि मैं GitHub पर पाया साथ कोशिश कर रहा हूँ एक सर्फिंग के बाद, लेकिन कुछ भी क्लिपबोर्ड पर है, मैं वर्डपैड पर पाठ, वहाँ में खरीद कुछ भी नहीं पेस्ट करने के लिए प्रयास करें। कुछ भी नहीं की तरह है यह क्लिपबोर्ड तक पहुंचने या इसे नष्ट कर दिया गया है।

यह वर्ग है

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;


namespace MiCli
{
    public class Clippy
    {
        [DllImport(kernel32.dll)]
        private static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);

        [DllImport(kernel32.dll)]
        private static extern uint GetLastError();

        [DllImport(kernel32.dll)]
        private static extern IntPtr LocalFree(IntPtr hMem);

        [DllImport(kernel32.dll)]
        private static extern IntPtr GlobalFree(IntPtr hMem);

        [DllImport(kernel32.dll)]
        private static extern IntPtr GlobalLock(IntPtr hMem);

        [DllImport(kernel32.dll)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GlobalUnlock(IntPtr hMem);

        [DllImport(kernel32.dll, EntryPoint = CopyMemory, SetLastError = false)]
        public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

        [DllImport(user32.dll)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool OpenClipboard(IntPtr hWndNewOwner);

        [DllImport(user32.dll)]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseClipboard();

        [DllImport(user32.dll)]
        private static extern IntPtr SetClipboardData(uint uFormat, IntPtr data);

        public enum ResultCode
        {
            Success = 0,

            ErrorOpenClipboard = 1,
            ErrorGlobalAlloc = 2,
            ErrorGlobalLock = 3,
            ErrorSetClipboardData = 4,
            ErrorOutOfMemoryException = 5,
            ErrorArgumentOutOfRangeException = 6,
            ErrorException = 7,
            ErrorInvalidArgs = 8,
            ErrorGetLastError = 9
        };

        public class Result
        {
            public ResultCode ResultCode { get; set; }

            public uint LastError { get; set; }

            public bool OK
            {
                // ReSharper disable once RedundantNameQualifier
                get { return Clippy.ResultCode.Success == ResultCode; }
            }
        }

        [STAThread]
        public static Result PushStringToClipboard(string message)
        {
            var isAscii = (message != null && (message == Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(message))));
            if (isAscii)
            {
                return PushUnicodeStringToClipboard(message);
            }
            else
            {
                return PushAnsiStringToClipboard(message);
            }
        }

        [STAThread]
        public static Result PushUnicodeStringToClipboard(string message)
        {
            return __PushStringToClipboard(message, CF_UNICODETEXT);
        }

        [STAThread]
        public static Result PushAnsiStringToClipboard(string message)
        {
            return __PushStringToClipboard(message, CF_TEXT);
        }

        // ReSharper disable InconsistentNaming
        const uint CF_TEXT = 1;
        const uint CF_UNICODETEXT = 13;
        // ReSharper restore InconsistentNaming

        [STAThread]
        private static Result __PushStringToClipboard(string message, uint format)
        {
            try
            {
                try
                {
                    if (message == null)
                    {
                        return new Result { ResultCode = ResultCode.ErrorInvalidArgs };
                    }

                    if (!OpenClipboard(IntPtr.Zero))
                    {
                        return new Result { ResultCode = ResultCode.ErrorOpenClipboard, LastError = GetLastError() };
                    }

                    try
                    {
                        uint sizeOfChar;
                        switch (format)
                        {
                            case CF_TEXT:
                                sizeOfChar = 1;
                                break;
                            case CF_UNICODETEXT:
                                sizeOfChar = 2;
                                break;
                            default:
                                throw new Exception(Not Reachable);
                        }

                        var characters = (uint)message.Length;
                        uint bytes = (characters + 1) * sizeOfChar;

                        // ReSharper disable once InconsistentNaming
                        const int GMEM_MOVABLE = 0x0002;
                        // ReSharper disable once InconsistentNaming
                        const int GMEM_ZEROINIT = 0x0040;
                        // ReSharper disable once InconsistentNaming
                        const int GHND = GMEM_MOVABLE | GMEM_ZEROINIT;

                        // IMPORTANT: SetClipboardData requires memory that was acquired with GlobalAlloc using GMEM_MOVABLE.
                        var hGlobal = GlobalAlloc(GHND, (UIntPtr)bytes);
                        if (hGlobal == IntPtr.Zero)
                        {
                            return new Result { ResultCode = ResultCode.ErrorGlobalAlloc, LastError = GetLastError() };
                        }

                        try
                        {
                            // IMPORTANT: Marshal.StringToHGlobalUni allocates using LocalAlloc with LMEM_FIXED.
                            //            Note that LMEM_FIXED implies that LocalLock / LocalUnlock is not required.
                            IntPtr source;
                            switch (format)
                            {
                                case CF_TEXT:
                                    source = Marshal.StringToHGlobalAnsi(message);
                                    break;
                                case CF_UNICODETEXT:
                                    source = Marshal.StringToHGlobalUni(message);
                                    break;
                                default:
                                    throw new Exception(Not Reachable);
                            }

                            try
                            {
                                var target = GlobalLock(hGlobal);
                                if (target == IntPtr.Zero)
                                {
                                    return new Result { ResultCode = ResultCode.ErrorGlobalLock, LastError = GetLastError() };
                                }

                                try
                                {
                                    CopyMemory(target, source, bytes);
                                }
                                finally
                                {
                                    var ignore = GlobalUnlock(target);
                                }

                                if (SetClipboardData(format, hGlobal).ToInt64() != 0)
                                {
                                    // IMPORTANT: SetClipboardData takes ownership of hGlobal upon success.
                                    hGlobal = IntPtr.Zero;
                                }
                                else
                                {
                                    return new Result { ResultCode = ResultCode.ErrorSetClipboardData, LastError = GetLastError() };
                                }
                            }
                            finally
                            {
                                // Marshal.StringToHGlobalUni actually allocates with LocalAlloc, thus we should theorhetically use LocalFree to free the memory...
                                // ... but Marshal.FreeHGlobal actully uses a corresponding version of LocalFree internally, so this works, even though it doesn't
                                //  behave exactly as expected.
                                Marshal.FreeHGlobal(source);
                            }
                        }
                        catch (OutOfMemoryException)
                        {
                            return new Result { ResultCode = ResultCode.ErrorOutOfMemoryException, LastError = GetLastError() };
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            return new Result { ResultCode = ResultCode.ErrorArgumentOutOfRangeException, LastError = GetLastError() };
                        }
                        finally
                        {
                            if (hGlobal != IntPtr.Zero)
                            {
                                var ignore = GlobalFree(hGlobal);
                            }
                        }
                    }
                    finally
                    {
                        CloseClipboard();
                    }
                    return new Result { ResultCode = ResultCode.Success };
                }
                catch (Exception)
                {
                    return new Result { ResultCode = ResultCode.ErrorException, LastError = GetLastError() };
                }
            }
            catch (Exception)
            {
                return new Result { ResultCode = ResultCode.ErrorGetLastError };
            }
        }
    }
}

और इस कार्यक्रम है

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace MiCli
{   

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            String comando = Inicio;
            String respuesta = ;
            //int[] vector;

            Console.WriteLine(CLI asp.net mvc);
            Console.WriteLine(Introduzca su opción);

            do
            {
                comando = Console.ReadLine();

                switch (comando)
                {
                    case hp:
                        respuesta = [HttpPost];
                        break;
                    case hv:
                        respuesta = [HttpPost] + Environment.NewLine + [ValidateAntiForgeryToken];
                        break;
                    case vaft:
                        respuesta = [ValidateAntiForgeryToken];
                        break;

                }

                Clippy.PushStringToClipboard(respuesta);

                //Console.WriteLine(respuesta);
            } while (comando != salir);

            Environment.Exit(1);

        }
    }
}
03/12/2019 को 00:03
का स्रोत उपयोगकर्ता
अन्य भाषाओं में...                            


1 जवाब

मैं एक सांत्वना applicacion पर पाठ की क्लिपबोर्ड पर भेज सकते

वोट
0

मैं एक छोटे से सांत्वना आवेदन सी ​​# पर मेरे कार्यक्रमों के लिए एक छोटा सा CLI बनाने के लिए है, मैं क्लिपबोर्ड कुछ पाठ भेजने और फिर इसे विजुअल स्टूडियो में पेस्ट करना चाहते, मैं हमेशा की तरह की कोशिश की लेकिन असफल, त्रुटि का कहना है कि क्लिपबोर्ड है मान्यता प्राप्त नहीं, whinle मैं एक वर्ग कि मैं GitHub पर पाया साथ कोशिश कर रहा हूँ एक सर्फिंग के बाद, लेकिन कुछ भी क्लिपबोर्ड पर है, मैं वर्डपैड पर पाठ, वहाँ में खरीद कुछ भी नहीं पेस्ट करने के लिए प्रयास करें। कुछ भी नहीं की तरह है यह क्लिपबोर्ड तक पहुंचने या इसे नष्ट कर दिया गया है।

यह वर्ग है

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;


namespace MiCli
{
    public class Clippy
    {
        [DllImport("kernel32.dll")]
        private static extern IntPtr GlobalAlloc(uint uFlags, UIntPtr dwBytes);

        [DllImport("kernel32.dll")]
        private static extern uint GetLastError();

        [DllImport("kernel32.dll")]
        private static extern IntPtr LocalFree(IntPtr hMem);

        [DllImport("kernel32.dll")]
        private static extern IntPtr GlobalFree(IntPtr hMem);

        [DllImport("kernel32.dll")]
        private static extern IntPtr GlobalLock(IntPtr hMem);

        [DllImport("kernel32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool GlobalUnlock(IntPtr hMem);

        [DllImport("kernel32.dll", EntryPoint = "CopyMemory", SetLastError = false)]
        public static extern void CopyMemory(IntPtr dest, IntPtr src, uint count);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool OpenClipboard(IntPtr hWndNewOwner);

        [DllImport("user32.dll")]
        [return: MarshalAs(UnmanagedType.Bool)]
        private static extern bool CloseClipboard();

        [DllImport("user32.dll")]
        private static extern IntPtr SetClipboardData(uint uFormat, IntPtr data);

        public enum ResultCode
        {
            Success = 0,

            ErrorOpenClipboard = 1,
            ErrorGlobalAlloc = 2,
            ErrorGlobalLock = 3,
            ErrorSetClipboardData = 4,
            ErrorOutOfMemoryException = 5,
            ErrorArgumentOutOfRangeException = 6,
            ErrorException = 7,
            ErrorInvalidArgs = 8,
            ErrorGetLastError = 9
        };

        public class Result
        {
            public ResultCode ResultCode { get; set; }

            public uint LastError { get; set; }

            public bool OK
            {
                // ReSharper disable once RedundantNameQualifier
                get { return Clippy.ResultCode.Success == ResultCode; }
            }
        }

        [STAThread]
        public static Result PushStringToClipboard(string message)
        {
            var isAscii = (message != null && (message == Encoding.ASCII.GetString(Encoding.ASCII.GetBytes(message))));
            if (isAscii)
            {
                return PushUnicodeStringToClipboard(message);
            }
            else
            {
                return PushAnsiStringToClipboard(message);
            }
        }

        [STAThread]
        public static Result PushUnicodeStringToClipboard(string message)
        {
            return __PushStringToClipboard(message, CF_UNICODETEXT);
        }

        [STAThread]
        public static Result PushAnsiStringToClipboard(string message)
        {
            return __PushStringToClipboard(message, CF_TEXT);
        }

        // ReSharper disable InconsistentNaming
        const uint CF_TEXT = 1;
        const uint CF_UNICODETEXT = 13;
        // ReSharper restore InconsistentNaming

        [STAThread]
        private static Result __PushStringToClipboard(string message, uint format)
        {
            try
            {
                try
                {
                    if (message == null)
                    {
                        return new Result { ResultCode = ResultCode.ErrorInvalidArgs };
                    }

                    if (!OpenClipboard(IntPtr.Zero))
                    {
                        return new Result { ResultCode = ResultCode.ErrorOpenClipboard, LastError = GetLastError() };
                    }

                    try
                    {
                        uint sizeOfChar;
                        switch (format)
                        {
                            case CF_TEXT:
                                sizeOfChar = 1;
                                break;
                            case CF_UNICODETEXT:
                                sizeOfChar = 2;
                                break;
                            default:
                                throw new Exception("Not Reachable");
                        }

                        var characters = (uint)message.Length;
                        uint bytes = (characters + 1) * sizeOfChar;

                        // ReSharper disable once InconsistentNaming
                        const int GMEM_MOVABLE = 0x0002;
                        // ReSharper disable once InconsistentNaming
                        const int GMEM_ZEROINIT = 0x0040;
                        // ReSharper disable once InconsistentNaming
                        const int GHND = GMEM_MOVABLE | GMEM_ZEROINIT;

                        // IMPORTANT: SetClipboardData requires memory that was acquired with GlobalAlloc using GMEM_MOVABLE.
                        var hGlobal = GlobalAlloc(GHND, (UIntPtr)bytes);
                        if (hGlobal == IntPtr.Zero)
                        {
                            return new Result { ResultCode = ResultCode.ErrorGlobalAlloc, LastError = GetLastError() };
                        }

                        try
                        {
                            // IMPORTANT: Marshal.StringToHGlobalUni allocates using LocalAlloc with LMEM_FIXED.
                            //            Note that LMEM_FIXED implies that LocalLock / LocalUnlock is not required.
                            IntPtr source;
                            switch (format)
                            {
                                case CF_TEXT:
                                    source = Marshal.StringToHGlobalAnsi(message);
                                    break;
                                case CF_UNICODETEXT:
                                    source = Marshal.StringToHGlobalUni(message);
                                    break;
                                default:
                                    throw new Exception("Not Reachable");
                            }

                            try
                            {
                                var target = GlobalLock(hGlobal);
                                if (target == IntPtr.Zero)
                                {
                                    return new Result { ResultCode = ResultCode.ErrorGlobalLock, LastError = GetLastError() };
                                }

                                try
                                {
                                    CopyMemory(target, source, bytes);
                                }
                                finally
                                {
                                    var ignore = GlobalUnlock(target);
                                }

                                if (SetClipboardData(format, hGlobal).ToInt64() != 0)
                                {
                                    // IMPORTANT: SetClipboardData takes ownership of hGlobal upon success.
                                    hGlobal = IntPtr.Zero;
                                }
                                else
                                {
                                    return new Result { ResultCode = ResultCode.ErrorSetClipboardData, LastError = GetLastError() };
                                }
                            }
                            finally
                            {
                                // Marshal.StringToHGlobalUni actually allocates with LocalAlloc, thus we should theorhetically use LocalFree to free the memory...
                                // ... but Marshal.FreeHGlobal actully uses a corresponding version of LocalFree internally, so this works, even though it doesn't
                                //  behave exactly as expected.
                                Marshal.FreeHGlobal(source);
                            }
                        }
                        catch (OutOfMemoryException)
                        {
                            return new Result { ResultCode = ResultCode.ErrorOutOfMemoryException, LastError = GetLastError() };
                        }
                        catch (ArgumentOutOfRangeException)
                        {
                            return new Result { ResultCode = ResultCode.ErrorArgumentOutOfRangeException, LastError = GetLastError() };
                        }
                        finally
                        {
                            if (hGlobal != IntPtr.Zero)
                            {
                                var ignore = GlobalFree(hGlobal);
                            }
                        }
                    }
                    finally
                    {
                        CloseClipboard();
                    }
                    return new Result { ResultCode = ResultCode.Success };
                }
                catch (Exception)
                {
                    return new Result { ResultCode = ResultCode.ErrorException, LastError = GetLastError() };
                }
            }
            catch (Exception)
            {
                return new Result { ResultCode = ResultCode.ErrorGetLastError };
            }
        }
    }
}

और इस कार्यक्रम है

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;



namespace MiCli
{   

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            String comando = "Inicio";
            String respuesta = "";
            //int[] vector;

            Console.WriteLine("CLI asp.net mvc");
            Console.WriteLine("Introduzca su opción");

            do
            {
                comando = Console.ReadLine();

                switch (comando)
                {
                    case "hp":
                        respuesta = "[HttpPost]";
                        break;
                    case "hv":
                        respuesta = "[HttpPost]" + Environment.NewLine + "[ValidateAntiForgeryToken]";
                        break;
                    case "vaft":
                        respuesta = "[ValidateAntiForgeryToken]";
                        break;

                }

                Clippy.PushStringToClipboard(respuesta);

                //Console.WriteLine(respuesta);
            } while (comando != "salir");

            Environment.Exit(1);

        }
    }
}
03/12/2019 को 00:03
का स्रोत उपयोगकर्ता

Cookies help us deliver our services. By using our services, you agree to our use of cookies. Learn more