第二部分,接到1楼后面就行。- rem c#辅助程序开始位置
- :helper_csharp_start
-
- using System;
- using System.Runtime.InteropServices;
- using System.Diagnostics;
- using System.IO;
- using System.Text;
-
- namespace bat_helper
- {
- class Program
- {
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern IntPtr GetStdHandle(int nStdHandle);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool SetStdHandle(
- uint nStdHandle,
- IntPtr hHandle
- );
-
- [DllImport("kernel32.dll", SetLastError = true)]
- internal static extern bool SetConsoleCursorPosition(
- IntPtr hConsoleOutput,
- COORD dwCursorPosition);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool GetConsoleScreenBufferInfo(
- IntPtr hConsoleOutput,
- ref CONSOLE_SCREEN_BUFFER_INFO ConsoleScreenBufferInfo);
-
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern uint GetLastError();
-
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool GetConsoleCursorInfo(
- IntPtr hConsoleOutput,
- out CONSOLE_CURSOR_INFO lpConsoleCursorInfo
- );
-
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool SetConsoleCursorInfo(
- IntPtr hConsoleOutput,
- [In] ref CONSOLE_CURSOR_INFO lpConsoleCursorInfo
- );
-
- [DllImport("kernel32.dll", SetLastError = true)]
- static extern bool AttachConsole(
- uint dwProcessId
- );
-
- [DllImport("kernel32.dll", SetLastError = true, ExactSpelling = true)]
- static extern bool FreeConsole();
-
- public struct CONSOLE_CURSOR_INFO
- {
- public uint dwSize;
- public bool bVisible;
- }
-
- public struct COORD
- {
- public short X;
- public short Y;
- };
-
- public struct SMALL_RECT
- {
- public ushort Left;
- public ushort Top;
- public ushort Right;
- public ushort Bottom;
- };
-
- public struct CONSOLE_SCREEN_BUFFER_INFO
- {
- public COORD dwSize;
- public COORD dwCursorPosition;
- public ushort wAttributes;
- public SMALL_RECT srWindow;
- public COORD dwMaximumWindowSize;
- };
-
- const int STD_INPUT_HANDLE = -10;
- const int STD_OUTPUT_HANDLE = -11;
- const int STD_ERROR_HANDLE = -12;
-
- const uint STD_OUTPUT_HANDLE_uint = 0xFFFFFFF5;
-
- const uint ATTACH_PARENT_PROCESS = 0xffffffff;
-
- static public IntPtr m_pOriginalStdOutputHandle = IntPtr.Zero;
-
- static public void WriteLine(string strText)
- {
- // 先恢复成程序启动时的标准输出句柄,然后输出,再恢复后调用WriteLine时的标准输出句柄
- IntPtr pOriginalStdOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
- SetStdHandle(STD_OUTPUT_HANDLE_uint, m_pOriginalStdOutputHandle);
- Console.WriteLine(strText);
- SetStdHandle(STD_OUTPUT_HANDLE_uint, pOriginalStdOutputHandle);
- return;
- }
-
- static int Main(string[] args)
- {
- try
- {
- // 先保存标准输出句柄,父进程重定向调用时,此句柄可能不是控制台,可能是文件或者其他什么句柄
- m_pOriginalStdOutputHandle = GetStdHandle(STD_OUTPUT_HANDLE);
-
- // 附加到父进程的Console,确保父进程重定向调用时,此程序也能获取到标准控制台输出
- FreeConsole();
- AttachConsole(ATTACH_PARENT_PROCESS);
-
- if (0 == args.Length)
- {
- return 87; // ERROR_INVALID_PARAMETER
- }
-
- if (args[0].ToLower() == "sleeputilkeypress")
- {
- if (args.Length < 2)
- {
- return 87; // ERROR_INVALID_PARAMETER
- }
- int iSleepTimeInMs = int.Parse(args[1]);
- long lStartTimeInMs = System.DateTime.Now.Ticks / 10000;
- while (true)
- {
- if (Console.KeyAvailable)
- {
- ConsoleKeyInfo key = Console.ReadKey(true);
- WriteLine(Convert.ToString(key.Key) + ";" + Convert.ToString(key.Modifiers));
- break;
- }
- long lCurrentTimeInMs = System.DateTime.Now.Ticks / 10000;
- long lDiffTimeInMs = lCurrentTimeInMs - lStartTimeInMs;
- if (lDiffTimeInMs >= iSleepTimeInMs)
- {
- break;
- }
- long lNeedSleepTime = iSleepTimeInMs - lDiffTimeInMs;
- if (lNeedSleepTime > 5)
- {
- lNeedSleepTime = 5;
- }
- System.Threading.Thread.Sleep(5);
- }
- return 0;
- }
- else if (args[0].ToLower() == "sleep")
- {
- if (args.Length < 2)
- {
- return 87; // ERROR_INVALID_PARAMETER
- }
- int iSleepTimeInMs = int.Parse(args[1]);
- System.Threading.Thread.Sleep(iSleepTimeInMs);
- return 0;
- }
- else if (args[0].ToLower() == "getcurrentdatetime")
- {
- WriteLine(System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff:ffffff"));
- return 0;
- }
- else if (args[0].ToLower() == "getcursorposition")
- {
- IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- CONSOLE_SCREEN_BUFFER_INFO oCONSOLE_SCREEN_BUFFER_INFO = new CONSOLE_SCREEN_BUFFER_INFO();
- bool bRes = GetConsoleScreenBufferInfo(iStdOut, ref oCONSOLE_SCREEN_BUFFER_INFO);
- if (!bRes)
- {
- uint uiLastErrorCode = GetLastError();
- return (int)uiLastErrorCode;
- }
-
- WriteLine(Convert.ToString(oCONSOLE_SCREEN_BUFFER_INFO.dwCursorPosition.X)
- + "," + Convert.ToString(oCONSOLE_SCREEN_BUFFER_INFO.dwCursorPosition.Y)
- + ";" + Convert.ToString(oCONSOLE_SCREEN_BUFFER_INFO.dwMaximumWindowSize.X)
- + "," + Convert.ToString(oCONSOLE_SCREEN_BUFFER_INFO.dwMaximumWindowSize.Y));
-
- return 0;
- }
- else if (args[0].ToLower() == "setcursorposition")
- {
- if (args.Length < 3)
- {
- return 87; // ERROR_INVALID_PARAMETER
- }
- COORD oCursorPosition;
- oCursorPosition.X = short.Parse(args[1]);
- oCursorPosition.Y = short.Parse(args[2]);
- IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- bool bRes = SetConsoleCursorPosition(iStdOut, oCursorPosition);
- if (!bRes)
- {
- uint uiLastErrorCode = GetLastError();
- return (int)uiLastErrorCode;
- }
- return 0;
- }
- else if (args[0].ToLower() == "getcursorvisiable")
- {
- CONSOLE_CURSOR_INFO oCONSOLE_CURSOR_INFO = new CONSOLE_CURSOR_INFO();
- IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- bool bRes = GetConsoleCursorInfo(iStdOut, out oCONSOLE_CURSOR_INFO);
- if (!bRes)
- {
- uint uiLastErrorCode = GetLastError();
- return ((int)uiLastErrorCode + 100); // 返回值大于100,肯定就是失败
- }
- return (oCONSOLE_CURSOR_INFO.bVisible ? 1 : 0);
- }
- else if (args[0].ToLower() == "setcursorvisiable")
- {
- if (args.Length < 2)
- {
- return 87; // ERROR_INVALID_PARAMETER
- }
-
- CONSOLE_CURSOR_INFO oCONSOLE_CURSOR_INFO = new CONSOLE_CURSOR_INFO();
- IntPtr iStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
- bool bRes = GetConsoleCursorInfo(iStdOut, out oCONSOLE_CURSOR_INFO);
- if (!bRes)
- {
- uint uiLastErrorCode = GetLastError();
- return (int)uiLastErrorCode;
- }
-
- oCONSOLE_CURSOR_INFO.bVisible = (0 == short.Parse(args[1]) ? false : true);
- bRes = SetConsoleCursorInfo(iStdOut, ref oCONSOLE_CURSOR_INFO);
- if (!bRes)
- {
- uint uiLastErrorCode = GetLastError();
- return (int)uiLastErrorCode;
- }
-
- return 0;
- }
-
- return 87; // ERROR_INVALID_PARAMETER
- }
- catch (Exception ex)
- {
- WriteLine("exception: " + ex.ToString());
- return 87; // ERROR_INVALID_PARAMETER
- }
- }
- }
- }
复制代码
|