简体   繁体   English

使用.net监视非托管程序

[英]Monitoring Unmanaged program using .net


I have a exe program written using c++ , 我有一个使用c++编写的exe程序,
I want to perform some actions when that exe is opened, closed or minimized 当我opened, closed or minimizedexe时,我想执行一些操作
I wonder whether this kind of functionality is possible using c#.net ? 我想知道使用c#.net是否可以实现这种功能?

you can send windows message. 您可以发送Windows消息。 you also control on the window by using pipe name 您还可以使用管道名称在窗口上进行控制

A simple message pump looks like this:
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct MSG
{
    public IntPtr hwnd;
    public int message;
    public IntPtr wParam;
    public IntPtr lParam;
    public int time;
    public int pt_x;
    public int pt_y;
}

[DllImport("user32.dll", CharSet = CharSet.Ansi)]
public static extern bool GetMessage([In, Out] ref MSG msg, IntPtr hWnd, int MsgFilterMin,  int uMsgFilterMax);

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr DispatchMessage([In] ref MSG msg);


[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern uint RegisterWindowMessage(string lpString);

[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);


    // #1
    MSG msg = new MSG();
    while (GetMessage(ref msg, IntPtr.Zero, 0, 0))
    {
        DispatchMessage(ref msg);
    }

    // #2
    uint UWM_UART_CTRL_TRS = Win32Api.RegisterWindowMessage("BT_UARTCTRL_TRANSFER");
    Win32Api.SendMessage(HWND_BROADCAST, UWM_UART_CTRL_TRS, (IntPtr)0, (IntPtr)0);

    // #3
    uint UWM_UART_CTRL_TRS = Win32Api.RegisterWindowMessage("BT_UARTCTRL_TRANSFER");
    Win32Api.SendMessage(HWND_BROADCAST, UWM_UART_CTRL_TRS, (IntPtr)1, (IntPtr)0);

Native .NET Process class has a functionality for an event fired when a process is closed . 本机.NET Process类具有关闭进程时触发事件的功能。

Also process monitoring in C# is partially discussed in this post . 在这篇文章中还将部分讨论C#中的过程监视。

Considering this, you'll have to use Windows Management Instrumentation to track wide range of specific process events. 考虑到这一点,您必须使用Windows Management Instrumentation来跟踪各种特定的过程事件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM