當前位置:編程學習大全網 - 源碼下載 - C# winform 兩個exe 通信 我不同解決方案下的兩個exe,如:A.exe,B.exe,我需要兩個程序互相通信?

C# winform 兩個exe 通信 我不同解決方案下的兩個exe,如:A.exe,B.exe,我需要兩個程序互相通信?

這種寫法妳看壹下

const int WM_COPYDATA = 0x004A;

[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 button1_Click(object sender, EventArgs e)

{

int WINDOW_HANDLER = FindWindow(null, "指南針優化軟件");

if (WINDOW_HANDLER == 0)

{

Process p = new Process();

p.StartInfo.FileName = Application.StartupPath + "\\..\\ZNZ.exe";

p.Start();

Application.Exit();

}

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;//數據大小

[MarshalAs(UnmanagedType.LPStr)]

public string lpData;//指向數據的指針

}

  • 上一篇:怎麽讀懂jquery源碼讀懂jquery源碼需要多久
  • 下一篇:旅遊發圈的簡單壹句話(精選120句)
  • copyright 2024編程學習大全網