从窗口句柄获取进程 ID,无需使用 GetWindowThreadProcessId (c#.net 4.0)

发布于 2024-11-15 17:10:08 字数 660 浏览 2 评论 0原文

我们的应用程序最近从 .net 3.5 切换到 .net 4。突然出现的一个问题是 GetWindowThreadProcessId 不再起作用。

微软有一个可用的修补程序,但必须在每台客户计算机上安装它会很痛苦,并且可能会导致其他问题。我更喜欢解决方法。

http://support.microsoft.com/kb/982638

问题是,正如微软所描述的:

“在 .NET Framework 远程处理应用程序中,您有一个 MarshalByRefObject 对象。该对象包含一个返回 IntPtr 类型的方法。该对象跨进程边界传递。当该对象被序列化时,会引发 SerializationException 异常,并且您收到以下错误消息: 流“无效”中的类型代码无效 出现此问题的原因是,当 CLR 序列化 IntPtr 时,远程处理代码将 IntPtr 类型视为基本类型。但是,当二进制格式化程序序列化 IntPtr 时,远程处理代码稍后会将 IntPtr 类型视为无效类型。”

那么,还有其他选择吗?是否有其他方法可以从窗口句柄获取进程 ID?或者我可以吗?以不同的方式编组我的 IntPtr 以避免这个错误?

Our application recently switched from .net 3.5 to .net 4. One issue that has cropped up is that GetWindowThreadProcessId no longer works.

Microsoft have a hotfix available, but it would be a pain to have to install that on every customer machine, and it could cause other issues. I would prefer a work around.

http://support.microsoft.com/kb/982638

The problem is, as described by microsoft:

"In a .NET Framework remoting application, you have a MarshalByRefObject object. This object contains a method that returns an IntPtr type. The object is passed across a process boundary. When the object is serialized, a SerializationException exception is thrown, and you receive the following error message:
Invalid type code in stream 'Invalid'
This issue occurs because the remoting code treats an IntPtr type as a primitive type when the CLR serializes the IntPtr. However, the remoting code later treats the IntPtr type as an invalid type when the Binary formatter serializes the IntPtr."

So, is there an alternative? Is there a different way for me to get a process id from a window handle? Or can I marshal my IntPtr differently to avoid this bug?

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

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

发布评论

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

评论(1

瞎闹 2024-11-22 17:10:08

您是否尝试用 void 替换返回值? (因为我认为你不需要线程ID)

[DllImport("user32.dll", SetLastError=true)]
static extern void GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

而不是:

[DllImport("user32.dll", SetLastError=true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

Did you try replacing the return value with a void? (Because i think you don't need the thread id)

[DllImport("user32.dll", SetLastError=true)]
static extern void GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);

instead of:

[DllImport("user32.dll", SetLastError=true)]
static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint lpdwProcessId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文