Passing messages between exe

Source: Internet
Author: User

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices;

Namespace winformsendmsg
{
Partial class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. textbox textbox1;
Private system. Windows. Forms. Button button1;

Const int wm_copydata = 0x004a;

Public form1 ()
{
Initializecomponent ();
}

 

 

[Dllimport ("user32.dll", entrypoint = "sendmessage")]
Private Static extern int sendmessage (
Int hwnd, // handle to destination window
Int MSG, // message
Int wparam, // first Message Parameter
Ref copydatastruct lparam // second Message Parameter
);

[Dllimport ("user32.dll", entrypoint = "findwindow")]
Private Static extern int findwindow (string lpclassname, string
Lpwindowname );

Private void button#click (Object sender, system. eventargs E)
{
Int window_handler = findwindow (null, @ "receiver form ");
If (window_handler = 0)
{

}
Else
{
Byte [] Sarr = system. Text. encoding. Default. getbytes (this. textbox1.text );
Int Len = Sarr. length;

Copydatastruct CDs;
CDs. dwdata = (intptr) 100;
CDs. lpdata = This. textbox1.text;
CDs. cbdata = Len + 1;
Sendmessage (window_handler, wm_copydata, 0, ref CDs );

}

}
}

Public struct copydatastruct
{
Public intptr dwdata;
Public int cbdata;
[Financialas (unmanagedtype. lpstr)]
Public String lpdata;
}

}

 

Using system;
Using system. drawing;
Using system. collections;
Using system. componentmodel;
Using system. Windows. forms;
Using system. Data;
Using system. runtime. interopservices;

Namespace windowsformgetmsg
{
Partial class form1: system. Windows. Forms. Form
{
Private system. Windows. Forms. textbox textbox1;

Const int wm_copydata = 0x004a;

Public form1 ()
{
Initializecomponent ();
}

 

Protected override void defwndproc (Ref system. Windows. Forms. Message m)
{
Switch (M. msg)
{
Case wm_copydata:
Copydatastruct mystr = new copydatastruct ();
Type mytype = mystr. GetType ();
Mystr = (copydatastruct) M. getlparam (mytype );
This. textbox1.text = mystr. lpdata;
Break;
Default:
Base. defwndproc (ref m );
Break;

}

}

 

}

Public struct copydatastruct
{
Public intptr dwdata;
Public int cbdata;
[Financialas (unmanagedtype. lpstr)]
Public String lpdata;
}
}

/Files/njnudt/exe.rar

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.