??????????????????????copydata????????????????????н????Windows?????????????????????????
?????????????Winform???????????????
??????????
public class ImportFromDLL
{
public const int WM_COPYDATA = 0x004A;
//???÷??й????
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT
{
public int dwData;    //not used
public int cbData;    //????
[MarshalAs(UnmanagedType.LPStr)]
public string lpData;
}
[DllImport("User32.dll")]
public static extern int SendMessage(
IntPtr hWnd????????  // handle to destination window
int Msg????????      // message
IntPtr wParam????   // first message parameter
ref COPYDATASTRUCT pcd // second message parameter
);
[DllImport("User32.dll"?? EntryPoint = "FindWindow")]
public static extern IntPtr FindWindow(string lpClassName?? string lpWindowName);
[DllImport("Kernel32.dll"?? EntryPoint = "GetConsoleWindow")]
public static extern IntPtr GetConsoleWindow();
}
???????????
private void SendMessage()
{
//????????
string filepath = @"D:GetMsg.exe";//???????????·??
string strText= "hello world!";//????????//???????????е????????????????????
Process[] processes = Process.GetProcesses();
Process process = null;
foreach (Process p in processes)
{
try
{
//????????????Щ???????????????????????
if (p.ProcessName != "System" && p.ProcessName != "Idle")
{
if (p.ProcessName == "GetMsg")
{
process = p;
break;
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//???????????????δ???У????????
if (process == null)
{//??????????????
process = System.Diagnostics.Process.Start(filepath);
Thread.Sleep(100);//????????????????????????????????????????
}
//???????????
IntPtr hwndRecvWindow = process.MainWindowHandle;
//??????????
IntPtr hwndSendWindow = Process.GetCurrentProcess().Handle;
//???COPYDATA??
ImportFromDLL.COPYDATASTRUCT copydata = new ImportFromDLL.COPYDATASTRUCT();
copydata.cbData = Encoding.Default.GetBytes(strText).Length; //???? ??????strText.Length;
copydata.lpData = strText;//????
//???????
ImportFromDLL.SendMessage(hwndRecvWindow?? ImportFromDLL.WM_COPYDATA?? hwndSendWindow?? ref copydata);
return;
}
???????????
protected override void WndProc(ref Message m)
{
if (m.Msg == ImportFromDLL.WM_COPYDATA)//????Message.Msg????????????ImportFromDLL.WM_COPYDATA??????????????????
{
ImportFromDLL.COPYDATASTRUCT copyData = (ImportFromDLL.COPYDATASTRUCT)m.GetLParam(typeof(ImportFromDLL.COPYDATASTRUCT));//???????
MessageBox.Show(copyData.lpData);
}
base.WndProc(ref m);
}
???????
????1?????????????????????C++??JAVA????????????????????
????2??????????????????δ?????????????????????????????
????Thread.Sleep(100);//??????????????????????????????????
?????ó?????????????ó???????????????????????????????????????????????????????
????public static Process Start(string fileName?? string arguments);
????public static Process Start(string fileName?? string userName?? SecureString password?? string domain);
????public static Process Start(string fileName?? string arguments?? string userName?? SecureString password?? string domain);
?????????????????????API?????