gpt4 book ai didi

c# - 使用 .net 监控非托管程序

转载 作者:太空宇宙 更新时间:2023-11-04 11:42:33 26 4
gpt4 key购买 nike


我有一个使用 c++ 编写的 exe 程序,
我想在 exe 打开、关闭或最小化
时执行一些操作我想知道使用 c#.net 是否可以实现这种功能?

最佳答案

可以发送windows消息。您还可以使用管道名称在窗口上进行控制

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);

关于c# - 使用 .net 监控非托管程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20822804/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com