How to use process communication-sendmessage

Source: Internet
Author: User

How to use process communication-sendmessage

The students who have used SendMessage for process communication know that this function is generally used in conjunction with FindWindow. Find the process handle through FindWindow, and then use SendMessage to send information to the handle. This interface is relatively simple to use, but it was also a great effort, just for the record.

First step: Designing the User interface

The user interface needs to have a few elements, set the local window name, the target window name, send the data, and accept the data.

Step Two: Reference interface

WIN32 API functions

[DllImport ("User32.dll", EntryPoint = "SendMessage")]

private static extern int SendMessage (int hWnd, int Msg, int wParam, ref copydatastruct LParam);

[DllImport ("User32.dll", EntryPoint = "FindWindow")]

private static extern int FindWindow (string lpclassname, string lpwindowname);

const int wm_copydata = 0X004A;

public struct COPYDATASTRUCT

{

Public IntPtr dwdata;

public int cbdata;

[MarshalAs (UNMANAGEDTYPE.LPSTR)]

public string lpdata;

}

When I did it, I changed the Wm_copydata to another value, like C + +, and I just needed to define the message as wm_user+1 to send the data to the message. But when doing, found that the wrong, just contact C # do not know where I understand the wrong or how. Change to another value. Can only be set into 0x004a;

Step three: Send a message

private void Buttonsend_click (object sender, EventArgs e)

{

string wndname = Textboxdestwindowname.text;

String message = Textboxsenddata.text;

int hWnd = FindWindow (null, wndname);

if (hWnd! = 0)

{

byte[] Sarr = System.Text.Encoding.Default.GetBytes (message);

int len = Sarr. Length;

COPYDATASTRUCT CDs;

Cds.dwdata = (INTPTR) convert.toint16 (1);//can be any value

Cds.cbdata = len + 1;//Specifies the number of bytes in the Lpdata memory area

Cds.lpdata = message;//The data sent to the process of the target window

SendMessage (hWnd, wm_copydata, 0, ref CDs);

}

}

Fourth step: Accept the message

protected override void Defwndproc (ref Message m)

{

Switch (m.msg)

{

Case WM_COPYDATA:

copydatastruct CDs = new COPYDATASTRUCT ();

Type t = CDs. GetType ();

CDS = (COPYDATASTRUCT) m.getlparam (t);

string strresult = Cds.lpdata;

String strtype = Cds.dwData.ToString ();

Recvonedatapacket (Strresult, strtype);

Break

Default

Base. Defwndproc (ref m);

Break

}

}

private void Recvonedatapacket (String strresult, String strtype)

{

throw new NotImplementedException ();

LISTBOXRECV.ITEMS.ADD (strresult);

}

This interface, when it was done, always made mistakes, right here in this place.

CDS = (COPYDATASTRUCT) m.getlparam (t);

I don't know why, perhaps because of the definition of the message value. I'm not sure, but I'll have to pay attention later.

Code Address: Https://github.com/liningg/sendmessage

How to use process communication-sendmessage

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.