.NET 访问和控制外部应用程序的变量和方法

.NET Access and control variables and methods of an external application

我需要帮助来确定这是否可能。我要申请;应用程序 1 和应用程序 2。 app1 是主要应用程序,app2 是辅助应用程序(启动服务,在机器启动时启动 app1 并确保 app1 始终 运行)。

有没有办法让我将这两个应用程序写入其中,如果应用程序 2 检测到事件,它可以连接到当前 运行 应用程序 1 并触发其方法之一?我可以从 app2 访问/修改 app1 的任何变量吗?

我已经进行了广泛的查看,但最接近我的答案的是 Win32 / API FindWindow 选项,用于按下按钮或更改文本。我认为这不是我要找的。

我想要的有可能吗?

感谢任何帮助,谢谢!

在应用程序之间进行通信的方法有很多种,每种方法都有不同的权衡。

两个应用程序在 Windows OS 中相互通信的最常见和首选的方法可能也是 Interprocess Communications (IPC) as discussed in another thread here. This alone gives you several different options among them being RPC, Pipes, Winsock and I'll lump WCF 在这里。

您还可以求助于操作文件并利用 FileSystemWatcher or create a similar solution for Registry watching

然而,

None 这些解决方案允许您改变应用程序数据或直接调用方法。很有可能你真的不需要这个,因为任何通信机制都允许你通过更明确的 API 间接改变程序状态。这就是抽象的基本思想。

您可以在技术上利用 Process class or possibly an AppDomain and come up with a workaround via Reflection. This isn't exactly what you are looking for either and maintenance can be troublesome. It is good to know about these technologies for other purposes though. Here are a few interesting posts on the subject: Cross AppDomain Call, Example use of AppDomain, Example use of Process, another handy type to look at

还有另一种方法,尽管对于您的情况来说有点过分,但为了完整起见,我还是会提到它。如果你处理 OS,你可以写一个 Kernel Mode application. This is mostly if you want to interface with new hardware and even in that case some drivers can still be written in user mode. If you are to dive into kernel mode application development you are no longer living in the safety zone of virtual address spaces, if you make a mistake you no longer crash your program, you blue screen the OS. During development it would be wise to use a hard disk cloning utility such as Clonezilla 从头开始​​。您还需要再次熟悉其他内核模式调试工具,这有点超出范围。

总结

我意识到 none 这些方法在技术上会给你你在问题中提到的确切行为,但我会说 IPC 方法是处理你正在创建的模式的最佳选择 Service/Stand单独应用对。