Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
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
C#
[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:
C#
ParentFormHandle = ParentHandle.ToInt32();
SendMessage(ParentFormHandle, 40, "Hello World!", IntPtr.Zero);

this is the code i am using in the reciving app:
C#
protected override void WndProc(ref Message m)
{
    if (m.Msg == 40)
    {
        YourTextBoxNameHere.Text += m.WParam.ToString();
    }
    else
    {
        base.WndProc(ref m);
    }
}

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
Posted
Updated 17-May-23 3:13am
v6

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[^]
 
Share this answer
 
Comments
MasterCodeon 31-Dec-14 8:53am    
oh sorry i did call base.WndProc.
i am updating it now
MasterCodeon 31-Dec-14 8:57am    
ParentFormHanlde is a local variable that i set like this:
ParentFormHandle = ParentHandle.ToInt32();
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:
C#
//This Is The Wrong Way To Do It
Form newform = new MyFormOne();
//This Is The Correct Way To Do It
MyFormOne newform = new MyFormOne();

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900