PostMessage无法传递字符串C# [英] PostMessage unable to pass a string C#

查看:736
本文介绍了PostMessage无法传递字符串C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的原型:

    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool PostMessage(int hhwnd, uint msg, IntPtr wparam, IntPtr lparam);

这是我的使用方式:

PostMessage(HWND_BROADCAST, msg, Marshal.StringToHGlobalAuto("bob"), IntPtr.Zero);

在另一个线程中,我可以截取此消息,但是当我尝试使用以下方法使鲍勃退回时:

In a different thread I can intercept this message, but when I try to get bob back using:

string str = Marshal.PtrToStringAuto(m.WParam); // where m = the Message object

我不会在str中碰到鲍勃.

I don't get bob in str.

我认为这一定是由于我在一个线程的堆栈中引用了"bob"字符串,而该引用在另一个线程的堆栈中绝对没有意义.但是如果是这样,这些wparam和lparam指针是否真的只用于在同一线程中传递的消息?

I'm thinking this has got to be due to the fact that I referenced the "bob" string on one thread's stack and that reference has absolutely no meaning in a different thread's stack. But if that's the case, are these wparam and lparam pointers only really used for messages being passed in the same thread?

编辑*更正:线程是进程.这是在进程之间而不是线程之间传递字符串的问题.

推荐答案

HGLOBAL不再以任何方式全局.自从Win16之后.而且HWND_BROADCAST看起来就像您正在将消息发送到其他进程,不必介意仅使用其他线程.

HGLOBALs arn't, in any way, global anymore. Not since win16. And HWND_BROADCAST looks like you are sending the message to a different process, never mind just a different thread.

因此,除非您使用操作系统知道如何封送的标准消息之一,否则您需要将字符串"bob"放在不同进程可以访问的共享内存区域中.

So, unless you either use one of the standard messages that the OS knows how to marshal, you need to place your string, "bob" in a shared memory area that different processes can access.

这篇关于PostMessage无法传递字符串C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆