C#使用SendMessage函数将字符串发送到另一个Appliaction [英] C# Use SendMessage Function To Send A String To Another Appliaction

查看:279
本文介绍了C#使用SendMessage函数将字符串发送到另一个Appliaction的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!

i我正在尝试将字符串发送到另一个应用程序。

我尝试这样做的方法是使用windows api中的SendMessage函数,我正在将WParam参数设置为字符串值(当我导入该函数时,我确实使用了此参数的字符串类型)。

现在在接收应用程序中我已将其设置为接收消息并将WParam值的内容放入文本框中,但问题是它没有字符串(我使用了测试字符串Hello World!)它只有一堆数字并将这些数字放入文本框。我在做错了什么?

这里是我在发送应用程序中使用的代码:

这些是我用于SendMessage方法的参数

hello everyone!
i am trying to send a string to another application.
the way i am trying to do this is by using the SendMessage function in the windows api, and i am setting the WParam parameter to a string value(when i imported the function i did use a string type for this parameter).
now in the receiving app i have it setup to receive the message and to put the contents of the WParam value into a textbox, but the problem is it doesn't have a string(i used a test string of "Hello World!") it just has a bunch of numbers and it puts those numbers into the text box. so what am i doing wrong?
here is the code i am using in my sending app:
these are the parameters i am using for the SendMessage Method

[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern string SendMessage(int hWnd, int msg, string wParam, IntPtr lParam);



and this line is ran when a button in the form is clicked:

ParentFormHandle = ParentHandle.ToInt32();
SendMessage(ParentFormHandle, 40, "Hello World!", IntPtr.Zero);



这是我在使用的代码复活的应用程序:


this is the code i am using in the reciving app:

protected override void WndProc(ref Message m)
{
    if (m.Msg == 40)
    {
        YourTextBoxNameHere.Text += m.WParam.ToString();
    }
    else
    {
        base.WndProc(ref m);
    }
}



这里是WParam属性的结果,字符串为Hello World!:

39061140

现在,每当我关闭发送应用程序并再次打开它时,此值将会改变。

如果有人有更好的方法将字符串发送到应用程序请请告诉我。

感谢您提前提供的所有帮助,

MasterCodeon


here is the result of the WParam property with the string "Hello World!":
39061140
now every time i close the sending app and open it back up again this value will change.
if anyone has a better way to send a string to an application please let me know.
thanks for all of your help in advance,
MasterCodeon

推荐答案

1)您的P / Invoke看起来不正确。

2)如果你想发送cutom消息,请使用 WM_USER + X [ ^ ],从0x0400开始而不是40十进制。

3)ParentFormHanlde ???它看起来不是我的另一个窗口...

4)在这里查看一个工作样本(甚至消息是其他的):http://www.codingvision.net/miscellaneous/c-send-text-to-notepad [ ^ ]

5)你应该致电 base.WndProc !但你最好不要干扰WndProc!但是如果你这样做,你应该考虑那些应用程序不共享内存,因此你需要复制在使用它之前发送的字符串指针。请看这里: https://boycook.wordpress.com/ 2008/07/29 / c-win32-messaging-with-sendmessage-and-wm_copydata / [ ^ ]。您可以在此处找到帮助程序类: https://gist.github.com/BoyCook/5075907 [ ^ ]

6)你有更好的IPC方法比Windows Messages API。这不是为了那个。您可以在这里看一下例如:进程间通信(IPC)简介和示例代码 [ ^ ], http: //techmikael.blogspot.hu/2010/02/blazing-fast-ipc-in-net-4-wcf-vs.html [ ^ ]
1) Your P/Invoke looks incorrect.
2) If you want to send cutom message, use WM_USER+X[^], which start from 0x0400 and not 40 decimal.
3) ParentFormHanlde??? It looks not an other window's handle to me...
4) Look here for a working sample (even the message is other): http://www.codingvision.net/miscellaneous/c-send-text-to-notepad[^]
5) You should call base.WndProc! But you better not interfere with WndProc! But if you do, you should consider that those applications don't share memory, thus you need to copy the string pointer sent before using it. Look here: https://boycook.wordpress.com/2008/07/29/c-win32-messaging-with-sendmessage-and-wm_copydata/[^]. You can find the helper class here: https://gist.github.com/BoyCook/5075907[^]
6) You have far better approaches for IPC than Windows Messages APIs. It is just not for that. You can look aroud here for example: Inter-Process Communication (IPC) Introduction and Sample Code[^], http://techmikael.blogspot.hu/2010/02/blazing-fast-ipc-in-net-4-wcf-vs.html[^]


好吧,我做了它是啊!

关于这个问题的一些背景信息:

i试图为我的表单设置一个选项框,我想在
我的主应用程序和选项应用程序。所以我尝试了属性方法但它不起作用,这是因为我将表单声明为Form类而不是它自己的类:

All right i did it yeah!
some background info on this question:
i was trying to setup an options box for my form and i wanted to pass string values between
my main app and the options app. so i tried the properties approach but it wouldn't work, this was due to the fact that i was declaring the form as a Form class not its own class:
//This Is The Wrong Way To Do It
Form newform = new MyFormOne();
//This Is The Correct Way To Do It
MyFormOne newform = new MyFormOne();



当我问这个问题时我不知道我做错了所以我决定使用windows api将字符串发送到另一个应用程序,但不久之后,我意识到我搞砸了并解决了我的问题。

谢谢大家的帮助,

MasterCodeon


when i asked this question i didn't know that i did that wrong so i decided to use the windows api to send a string to another app, but then soon after, i realized i screwed up and fixed my problem.
thank you all for your help,
MasterCodeon


这篇关于C#使用SendMessage函数将字符串发送到另一个Appliaction的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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