c# 通过win32 api 得到指定Console application Content
已知的问题:
1. 调试的时候会报IO 异常,非调试环境是正常的
2. Windows 应用程序才可以使用,可以用非windows应用程序包一层
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using windowsApiAcitonSimulation.Win32Action; namespace winFormTest
{
static class Program
{ [DllImport("kernel32.dll", SetLastError = true)]
static extern bool AttachConsole(uint dwProcessId); [DllImport("kernel32.dll", SetLastError = true)]
static extern IntPtr GetStdHandle(int nStdHandle); [DllImport("kernel32.dll", SetLastError = true)]
static extern bool ReadConsoleOutputCharacter(IntPtr hConsoleOutput, [Out] StringBuilder lpCharacter, uint length, Coord bufferCoord, out uint lpNumberOfCharactersRead); [StructLayout(LayoutKind.Sequential)]
public struct Coord
{
public short X;
public short Y;
} public static string ReadCharacterAt(int x, int y, int length)
{
IntPtr consoleHandle = GetStdHandle(-); if (consoleHandle == IntPtr.Zero)
{
return null;
}
Coord position = new Coord
{
X = (short)x,
Y = (short)y
};
StringBuilder result = new StringBuilder(length);
uint read = ;
if (ReadConsoleOutputCharacter(consoleHandle, result, (uint)length, position, out read))
{
return result.ToString();
}
else
{
return null;
}
} /// <summary>
/// 关闭进程
/// </summary>
/// <param name="processName">进程名</param>
private static Process GetNgrokProcess(string processName)
{
Process[] myproc = Process.GetProcesses();
foreach (Process item in myproc)
{
if (item.ProcessName == processName)
{
return item;
}
}
return null;
}
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{ var process = GetNgrokProcess("ngrok"); //注意要是 Windows 应用程序才可以 AttachConsole成功
var flag = AttachConsole((uint)process.Id); Console.CursorLeft = ;
Console.CursorTop = ;
string content = ReadCharacterAt(, , ); //if (content?.IndexOf("reconnecting") > -1)
//{
// var ptr = WindowsApiHelp.FindWindow("ConsoleWindowClass", @"ngrok.bat");
// if (ptr == IntPtr.Zero)
// {
// ptr = WindowsApiHelp.FindWindow("ConsoleWindowClass", @"选择ngrok.bat");
// }
// var pid = GetCurrentProcessID(ptr);
// WindowsApiHelp.SendMessage(ptr, WindowsMessages.WM_CLOSE, 0, 0);
//} } }
}
c# 通过win32 api 得到指定Console application Content的更多相关文章
- C#调用Win32 api学习总结
从.NET平台调用Win32 API Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微 ...
- Win32 Console Application、Win32 Application、MFC三者之间的联系和区别
转自:http://blog.csdn.net/c_base_jin/article/details/52304845 在windows编程中,我们或多或少都听说这三个名称,分别是Win32 Cons ...
- win32 console application 如何修改图标?
win32 console application ,不要看这名字高端大气上档次,让你摸不着头脑,其实他就是我们最先学习c语言那种黑色窗口的东西......话说他怎么修改图标呢?第一种方法是:右键-〉 ...
- 用vs2008和vs2005创建win32 console application
http://blog.sina.com.cn/s/blog_4900be890100s735.html 对于经常使用vc6.0的人来说,创建一个win32 console application很简 ...
- 重温 Win32 API ----- 截屏指定窗体并打印
朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...
- 【.Net】从.NET平台调用Win32 API
小序 Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微软留给我们直接控制 ...
- 控制台——对WIN32 API的使用(user32.dll)
Win32 API概念:即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface).所有在Win32平台上运行的应用程序都可以调用这些函 ...
- C#调用Win32 api时的内存操作
一般情况下,C#与Win 32 Api的互操作都表现的很一致:值类型传递结构体,一维.二维指针传递IntPtr.在Win32 分配内存时,可以通过IntPtr以类似移动指针的方式读取内存.通过IntP ...
- 【C++】从零开始,只使用FFmpeg,Win32 API,实现一个播放器(一)
前言 起初只是想做一个直接读取视频文件然后播放字符动画的程序.我的设想很简单,只要有现成的库,帮我把视频文件解析成一帧一帧的原始画面信息,那么我只需要读取里面的每一个像素的RGB数值,计算出亮度,然后 ...
随机推荐
- xz格式解压
1.安装xz命令 yum install xz -y 2.将xz文件解压为tar文件 xz -d example.tar.xz 3.将tar文件解压 tar xf example.tar 如果无法安装 ...
- Discuz!基础的代码安全和代码规范
变量所有漏洞都来源于变量,因此变量首先要做的就是定义初始化.用任何一个变量前一定要先定义,初始化它虽然现在Discuz!X来说,GPC不会被全局覆盖了,但是大家写插件的过程中也不要忽视了因为在服务器p ...
- 网络I/O
贴几个超级不错的博客 1.Linux IO模式及 select.poll.epoll详解 2.网络 I/O 模型 3.同步异步阻塞非阻塞 4.三种模式的区别与联系
- matlab基础向7-8:画图
1.画直角坐标系的二维图 画直线: x1=[1 2 3]; y1=[4 5 6]; plot(x1,y1);%斜率为1的直线,穿过(1,4)(2,5)(3,6) 画抛物线y=x*x(-3<=x& ...
- 学习:简单使用MFC创建对话框窗口
MFC介绍:微软基础类库(英语:Microsoft Foundation Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows ...
- SpringCloud断路器(Hystrix)和服务降级案列
断路器(Hystrix) 为什么需要 Hystrix? 在微服务架构中,我们将业务拆分成一个个的服务,服务与服务之间可以相互调用(RPC).为了保证其高可用,单个服务又必须集群部署.由于网络原因或者自 ...
- JS的ES6的基础语法
JS的ES6 1.let let age = 12; (1). 作用: 与var类似, 用于声明一个变量 (2). 特点: 在块作用域内有效 不能重复声明 不会预处理, 不存在提升 (3). 应用: ...
- orm-1
- ORM什么是? 类名 ---> 数据库表 对象 ---> 记录 对象.属性 ---> 字段 - ORM的优缺点: 优点: 可跨平台,可以通过对象.属性取值,对象.方法,让该方法内 ...
- [已解决] Python logging 重复打印日志信息
问题描述 问题代码如下: def get_logger(logger_name): """得到日志对象""" logger = loggin ...
- linux jar/war包 后台运行
1. 基础版,当前ssh窗口锁定,按CTRL+C打断程序运行:或关闭窗口,程序退出 java -jar flowable-modeler.war 2. 改进版,当前ssh窗口不锁定,窗口关闭时,程序终 ...