与 .NET 中的非托管进程对话

发布于 2024-09-26 13:02:29 字数 364 浏览 12 评论 0原文

我正在使用 Process.Start 从 .NET 创建一个进程。新流程是一个旧版应用程序,用 C/C++ 编写。为了与其通信,我需要对其主线程执行相当于 PostThreadMessage 的操作。

我很乐意使用 P/Invoke 来调用 PostThreadMessage,但我不知道如何找到主线程。 Process 对象有一个线程集合,但文档说集合中的第一项不必是主线程。 Thread 对象本身似乎没有任何迹象表明它们是否是主要的。虽然我可以在创建进程后立即查看线程集合,但这并不能保证只有一个。

那么,有没有办法让我从 .NET 确定另一个进程的主线程,或者我是否需要求助于使用 Win32 的 CreateProcess?

谢谢,

鲍勃

I'm creating a process from .NET using Process.Start. The new process is a legacy app, written in C/C++. To communicate with it, I need to do the equivalent of PostThreadMessage to its primary thread.

I'd be happy to use P/Invoke to call PostThreadMessage, but I can't see how to find the primary thread. The Process object has a collection of threads, but the doc says the first item in the collection need not be the primary thread. The Thread objects themselves don't seem to have any indication of whether they're primary. And while I could look at the thread collection immediately after creating the process, that's no guarantee there would be only one.

So, is there a way for me to determine another process' primary thread from .NET, or do I need to resort to using Win32's CreateProcess?

Thanks,

Bob

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

枯寂 2024-10-03 13:02:29

如果进程有窗口,则可以使用 GetWindowThreadProcessId API 获取 GUI 线程,该线程通常是主线程(使用 Process.MainWindowHandle 获取窗口句柄) 。

另一种选择是枚举线程 (Process.Threads) 并根据 StartTime 选择第一个启动的线程:

Process process = Process.Start(...);
process.WaitForInputIdle();
ProcessThread primaryThread = process.Threads.OrderBy(t => t.StartTime).First();

但这可能不是一种非常准确的技术。 ..

If the process has a window, you can use the GetWindowThreadProcessId API to get the GUI thread, which usually is the primary thread (use Process.MainWindowHandle to get the window handle).

Another option would be to enumerate the threads (Process.Threads) and pick the first one that was started, based on the StartTime:

Process process = Process.Start(...);
process.WaitForInputIdle();
ProcessThread primaryThread = process.Threads.OrderBy(t => t.StartTime).First();

But it's probably not a very accurate technique...

北方的韩爷 2024-10-03 13:02:29

您不需要窗口来使用线程消息队列。一旦线程调用 GetMessage 或 PeekMessage 等用户函数,就会创建线程消息队列。

请在此处查看更多信息:关于消息和消息队列

您仍然需要通过某种方式确定“主”线程 ID(并且发布线程/进程也需要足够的权限)。

这里有一个答案: http ://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/42de8f6a-61f4-495e-a69d-bd018e07c6f7

(请参阅“nobugz”答案)

You don't need a Window to use thread message queues. Thread message queues are created as soon as the thread calls user functions like GetMessage or PeekMessage.

see more info here at: About Messages and Message Queues

Still you will need to determine the "primary" thread Id (and the posting thread/process will need sufficient rights as well) by some mean.

There is an answer here: http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/42de8f6a-61f4-495e-a69d-bd018e07c6f7

(see the "nobugz" answer)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文