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数值,计算出亮度,然后 ...
随机推荐
- php迭代器模式(iterator pattern)
... <?php /* The iterator pattern is used to traverse a container and access its elements. In oth ...
- 2019年杭电多校第二场 1008题Harmonious Army(HDU6598+最小割+建图)
题目链接 传送门 题意 有\(n\)个士兵,要你给他们分配职业.有\(m\)对关系,对于某一对关系\(u,v\),如果同为勇士则总能力增加\(a\),同法师则增加\(c\),一个勇士一个法师增加\(\ ...
- Educational Codeforces Round 69 D. Yet Another Subarray Problem
Educational Codeforces Round 69 (Rated for Div. 2) D. Yet Another Subarray Problem 题目链接 题意: 求\(\sum_ ...
- Python与设计模式--工厂模式
快餐点餐系统 想必大家一定见过类似于麦当劳自助点餐台一类的点餐系统吧.在一个大的触摸显示屏上,有3类可以选择的上餐品:汉堡等主餐.小食.饮料.当我们选择好自己需要的食物,支付完成后,订单就生成了.下面 ...
- Java16周作业
题目1:编写一个应用程序,利用Java多线程机制,实现时间的同步输出显示. /*使用Runnable接口使用类创建线程对象,重写run()方法**/ 代码 public class timetext ...
- js 用 hasOwnProperty() 判定属性是来自该对象成员,还是原型链
var People=function(){ this.name='liujinyu'; }; People.prototype={ ...
- export default 和 export 的主要区别
export default 和 export 的主要区别 在于对应的import的区别:export 对应的 import 需要知道 export抛出的变量名或函数名 import{a,b}expo ...
- 用插件maven-surefire-report-plugin生成html格式测试报告
在默认情况下,执行maven test/maven package/maven install命令时会在target/surefire-reports目录下生成txt和xml格式的输出信息. 其实ma ...
- be of + 名词
语法: “be of +抽象名词(词组)” 表示主语的某种形状或特征,相当于 "be+形容词" 例如: be of value=be valuable : be of inter ...
- thymeleaf做if判定
<div class="showing"> <h2>条件判断</h2> <p th:if="${testBoolean}&quo ...