< WM_COPYDATA >
http://www.codeproject.com/KB/cs/ipc_wmcopy.aspx
http://www.codeproject.com/KB/threads/InterprocessCommunicator.aspx
http://www.codeproject.com/KB/cs/wm_copydata_use.aspx

 

< MessageWindow >
http://www.codeproject.com/KB/threads/IPCBetManagdUnmanagd.aspx

 

 

 

  1. const int WM_COPYDATA = 0x4A;
  2. [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern IntPtr SendMessage(IntPtr hwnd, int msg, IntPtr wparam, IntPtr lparam);
  3. ................
  4. //Process[] AppServerProcess = Process.GetProcessesByName("LoginServer");
  5. //.................
  6. //SendMessage(AppServerProcess[0].MainWindowHandle, WM_COPYDATA, IntPtr.Zero, iPtrForCdsMsg);
  7. COPYDATASTRUCT cds;
  8. cds.dwData = srcHandle;
    str = str + '\0';
  9. cds.cbData = str.Length + 1;
    cds.lpData = Marshal.AllocCoTaskMem(str.Length);
    cds.lpData = Marshal.StringToCoTaskMemAnsi(str);
    IntPtr iPtr = Marshal.AllocCoTaskMem(Marshal.SizeOf(cds));
    Marshal.StructureToPtr(cds, iPtr, true);
  10. // send to the MFC app
    SendMessage(destHandle, WM_COPYDATA, IntPtr.Zero, iPtr);
  11. // Don't forget to free the allocated memory 
    Marshal.FreeCoTaskMem(cds.lpData);
    Marshal.FreeCoTaskMem(iPtr);

     

    위 방식의 경우 보낼 때 마다 동적 할당을 하므로 메모리가 심하게 조각 날 수도 있다. 그러므로 한번 할당한 것을 계속 사용 하도록 한다.

     

  12.      public struct COPYDATASTRUCT
         {
             public IntPtr dwData;
             public int cbData;
             public IntPtr lpData;
         }
  13.      COPYDATASTRUCT  CdsMsg;
         IntPtr iPtrForCdsMsg;
    1.  
    2. public ServerCommunication()
      {
             CdsMsg.dwData = (IntPtr)12;
             CdsMsg.lpData = Marshal.AllocCoTaskMem(1024);
             iPtrForCdsMsg = Marshal.AllocCoTaskMem(Marshal.SizeOf(CdsMsg));
       }
  14.          ~ServerCommunication()
            {
                Marshal.FreeCoTaskMem(CdsMsg.lpData);
                Marshal.FreeCoTaskMem(iPtrForCdsMsg);
            }
  15.         public void ReceiveMsg(System.Windows.Forms.Message m)
            {
                COPYDATASTRUCT cds = new COPYDATASTRUCT();
                cds = (COPYDATASTRUCT)Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
                if (cds.cbData > 0)
                {
                    byte[] data = new byte[cds.cbData];
                    Marshal.Copy(cds.lpData, data, 0, cds.cbData);
                    Encoding unicodeStr = Encoding.ASCII;
                    char[] myString = unicodeStr.GetChars(data);
                    string returnText = new string(myString);
                    int iMsgIndex = (int)cds.dwData;
                    System.Windows.Forms.MessageBox.Show("ACK Received: " + returnText + iMsgIndex.ToString());
  16.                 m.Result = (IntPtr)1;
                }
            }
  17.         public void SendMsg()
            {
                Process[] AppServerProcess = Process.GetProcessesByName("LoginServer");
                if (AppServerProcess.Length < 1)
                {
                    return;
                }
  18.             string str = "Hello" + "\0";
               
                CdsMsg.dwData = AppServerProcess[0].MainWindowHandle;
                CdsMsg.cbData = str.Length;
                CdsMsg.lpData = Marshal.StringToCoTaskMemAnsi(str);
                Marshal.StructureToPtr(CdsMsg, iPtrForCdsMsg, true);
                SendMessage(AppServerProcess[0].MainWindowHandle, WM_COPYDATA, IntPtr.Zero, iPtrForCdsMsg);
            }

     

    1. protected override void WndProc(ref Message m)
      {
              switch (m.Msg)
              {
                   case WM_COPYDATA:
                          ServerComm.ReceiveMsg(m);
                          break;
  19.                 default:
                        // let the base class deal with it
                        base.WndProc(ref m);
                        break;
                }
  20.         }

     

신고
by 흥배 2012.09.17 09:00
| 1 |

티스토리 툴바