已知的问题
  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的更多相关文章

  1. C#调用Win32 api学习总结

    从.NET平台调用Win32 API Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微 ...

  2. Win32 Console Application、Win32 Application、MFC三者之间的联系和区别

    转自:http://blog.csdn.net/c_base_jin/article/details/52304845 在windows编程中,我们或多或少都听说这三个名称,分别是Win32 Cons ...

  3. win32 console application 如何修改图标?

    win32 console application ,不要看这名字高端大气上档次,让你摸不着头脑,其实他就是我们最先学习c语言那种黑色窗口的东西......话说他怎么修改图标呢?第一种方法是:右键-〉 ...

  4. 用vs2008和vs2005创建win32 console application

    http://blog.sina.com.cn/s/blog_4900be890100s735.html 对于经常使用vc6.0的人来说,创建一个win32 console application很简 ...

  5. 重温 Win32 API ----- 截屏指定窗体并打印

    朋友说在一个VC++6.0开发的项目中要增加打印窗体的功能,让帮忙写个代码供其调用. 这么老的IDE当然不想碰了,并且也不喜欢MFC笨拙不清晰的封装.所以决定採用纯Win32 API,然后用C++类简 ...

  6. 【.Net】从.NET平台调用Win32 API

    小序        Win32 API可以直接控制Microsoft Windows的核心,因为API(Application Programming Interface)本来就是微软留给我们直接控制 ...

  7. 控制台——对WIN32 API的使用(user32.dll)

    Win32 API概念:即为Microsoft 32位平台的应用程序编程接口(Application Programming Interface).所有在Win32平台上运行的应用程序都可以调用这些函 ...

  8. C#调用Win32 api时的内存操作

    一般情况下,C#与Win 32 Api的互操作都表现的很一致:值类型传递结构体,一维.二维指针传递IntPtr.在Win32 分配内存时,可以通过IntPtr以类似移动指针的方式读取内存.通过IntP ...

  9. 【C++】从零开始,只使用FFmpeg,Win32 API,实现一个播放器(一)

    前言 起初只是想做一个直接读取视频文件然后播放字符动画的程序.我的设想很简单,只要有现成的库,帮我把视频文件解析成一帧一帧的原始画面信息,那么我只需要读取里面的每一个像素的RGB数值,计算出亮度,然后 ...

随机推荐

  1. VS创建C工程

    打开VS编辑器: 填写项目名称,控制项目位置,创建: 在源文件中添加新项: 填写文件名称,修改后缀: 编写一个简单的hello word编译看看windows环境支持如何:

  2. dfs 正则表达式

    192. 通配符匹配 中文 English 判断两个可能包含通配符“?”和“*”的字符串是否匹配.匹配规则如下: '?' 可以匹配任何单个字符. '*' 可以匹配任意字符串(包括空字符串). 两个串完 ...

  3. 【转】tf.train.MonitoredTrainingSession()解析

    原文地址:https://blog.csdn.net/mrr1ght/article/details/81006343. 本文有删减. MonitoredTrainingSession定义 首先,tf ...

  4. P2220 [HAOI2012]容易题[小学数学]

    题目描述 为了使得大家高兴,小Q特意出个自认为的简单题(easy)来满足大家,这道简单题是描述如下: 有一个数列A已知对于所有的A[i]都是1~n的自然数,并且知道对于一些A[i]不能取哪些值,我们定 ...

  5. 2019牛客暑期多校训练营(第九场)A:Power of Fibonacci(斐波拉契幂次和)

    题意:求Σfi^m%p. zoj上p是1e9+7,牛客是1e9:  对于这两个,分别有不同的做法. 前者利用公式,公式里面有sqrt(5),我们只需要二次剩余求即可.     后者mod=1e9,5才 ...

  6. Async programming

    Asynchrony, in computer programming, refers to the occurrence of events independent of the mainprogr ...

  7. MD5加密封装

    1.固定返回固定长度字符串(16位或者32位) /// <summary> /// 用MD5加密字符串,可选择生成16位或者32位的加密字符串 /// </summary> / ...

  8. C# 其它模拟延迟

    System.Threading.Thread.Sleep(3000);//模拟延迟 如模拟上传图片等

  9. Zookeeper的缺点

    工作中对Zookeeper都是间接比较浅的使用,性能也没太高的要求所以对它的缺点也没太深的认识,从网上评价看,它还是有不少问题的. 1. 原生客户端坑太多,Curator(Curator是Netfli ...

  10. ubuntu 安装任何版本的Firefox

    https://ftp.mozilla.org/pub/firefox/releases/ 1.sudo gedit /usr/share/applications/firefox.desktop 2 ...