title: How to fetch the data which was sent via WM_COPYDATA into VB6 application?
body: <p>I have referred to code in this article to send messages from 1 C#.NET application to another C#.NET application using win32 api (SendMessage, FindWindow, WM_COPYDATA) <a href="http://code.msdn.microsoft.com/windowsdesktop/CSSendWMCOPYDATA-97e6644e" rel="nofollow">MSDN code sample application</a></p>
  
  <p><strong>My requirement is that the target app is an VB6 application ( we have access to its source code).. I am using following code to send message to the VB6 exe application. But, how do I receive the message in VB6 application? (Following are codes in C#.NET to send/receive message)</strong></p>
  
  <p>Code for sending message:-</p>
  
  <pre><code>#region Using directives
  using System;
  using System.Windows.Forms;
  using System.Runtime.InteropServices;
  using System.Security;
  #endregion
  
  
  namespace CSSendWM_COPYDATA
  {
      public partial class MainForm : Form
      {
          public MainForm()
          {
              InitializeComponent();
          }
  
          private void btnSendMessage_Click(object sender, EventArgs e)
          {
              // Find the target window handle.
              IntPtr hTargetWnd = NativeMethod.FindWindow(null, "CSReceiveWM_COPYDATA");
              if (hTargetWnd == IntPtr.Zero)
              {
                  MessageBox.Show("Unable to find the \"CSReceiveWM_COPYDATA\" window", 
                      "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                  return;
              }
  
              // Prepare the COPYDATASTRUCT struct with the data to be sent.
              MyStruct myStruct;
  
              int nNumber;
              if (!int.TryParse(this.tbNumber.Text, out nNumber))
              {
                  MessageBox.Show("Invalid value of Number!");
                  return;
              }
  
              myStruct.Number = nNumber;
              myStruct.Message = this.tbMessage.Text;
  
              // Marshal the managed struct to a native block of memory.
              int myStructSize = Marshal.SizeOf(myStruct);
              IntPtr pMyStruct = Marshal.AllocHGlobal(myStructSize);
              try
              {
                  Marshal.StructureToPtr(myStruct, pMyStruct, true);
  
                  COPYDATASTRUCT cds = new COPYDATASTRUCT();
                  cds.cbData = myStructSize;
                  cds.lpData = pMyStruct;
  
                  // Send the COPYDATASTRUCT struct through the WM_COPYDATA message to 
                  // the receiving window. (The application must use SendMessage, 
                  // instead of PostMessage to send WM_COPYDATA because the receiving 
                  // application must accept while it is guaranteed to be valid.)
                  NativeMethod.SendMessage(hTargetWnd, WM_COPYDATA, this.Handle, ref cds);
  
                  int result = Marshal.GetLastWin32Error();
                  if (result != 0)
                  {
                      MessageBox.Show(String.Format(
                          "SendMessage(WM_COPYDATA) failed w/err 0x{0:X}", result));
                  }
              }
              finally
              {
                  Marshal.FreeHGlobal(pMyStruct);
              }
          }
  
  
          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
          internal struct MyStruct
          {
              public int Number;
  
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
              public string Message;
          }
  
  
          #region Native API Signatures and Types
  
          /// &lt;summary&gt;
          /// An application sends the WM_COPYDATA message to pass data to another 
          /// application
          /// &lt;/summary&gt;
          internal const int WM_COPYDATA = 0x004A;
  
  
          /// &lt;summary&gt;
          /// The COPYDATASTRUCT structure contains data to be passed to another 
          /// application by the WM_COPYDATA message. 
          /// &lt;/summary&gt;
          [StructLayout(LayoutKind.Sequential)]
          internal struct COPYDATASTRUCT
          {
              public IntPtr dwData;       // Specifies data to be passed
              public int cbData;          // Specifies the data size in bytes
              public IntPtr lpData;       // Pointer to data to be passed
          }
  
  
          /// &lt;summary&gt;
          /// The class exposes Windows APIs to be used in this code sample.
          /// &lt;/summary&gt;
          [SuppressUnmanagedCodeSecurity]
          internal class NativeMethod
          {
              /// &lt;summary&gt;
              /// Sends the specified message to a window or windows. The SendMessage 
              /// function calls the window procedure for the specified window and does 
              /// not return until the window procedure has processed the message. 
              /// &lt;/summary&gt;
              /// &lt;param name="hWnd"&gt;
              /// Handle to the window whose window procedure will receive the message.
              /// &lt;/param&gt;
              /// &lt;param name="Msg"&gt;Specifies the message to be sent.&lt;/param&gt;
              /// &lt;param name="wParam"&gt;
              /// Specifies additional message-specific information.
              /// &lt;/param&gt;
              /// &lt;param name="lParam"&gt;
              /// Specifies additional message-specific information.
              /// &lt;/param&gt;
              /// &lt;returns&gt;&lt;/returns&gt;
              [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
              public static extern IntPtr SendMessage(IntPtr hWnd, int Msg,
                  IntPtr wParam, ref COPYDATASTRUCT lParam);
  
  
              /// &lt;summary&gt;
              /// The FindWindow function retrieves a handle to the top-level window 
              /// whose class name and window name match the specified strings. This 
              /// function does not search child windows. This function does not 
              /// perform a case-sensitive search.
              /// &lt;/summary&gt;
              /// &lt;param name="lpClassName"&gt;Class name&lt;/param&gt;
              /// &lt;param name="lpWindowName"&gt;Window caption&lt;/param&gt;
              /// &lt;returns&gt;&lt;/returns&gt;
              [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
              public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
          }
  
          #endregion
      }
  }
  </code></pre>
  
  <p>Code for receiving message:-</p>
  
  <pre><code>#region Using directives
  using System;
  using System.Windows.Forms;
  using System.Runtime.InteropServices;
  #endregion
  
  
  namespace CSReceiveWM_COPYDATA
  {
      public partial class MainForm : Form
      {
          public MainForm()
          {
              InitializeComponent();
          }
  
          protected override void WndProc(ref Message m)
          {
              if (m.Msg == WM_COPYDATA)
              {
                  // Get the COPYDATASTRUCT struct from lParam.
                  COPYDATASTRUCT cds = (COPYDATASTRUCT)m.GetLParam(typeof(COPYDATASTRUCT));
  
                  // If the size matches
                  if (cds.cbData == Marshal.SizeOf(typeof(MyStruct)))
                  {
                      // Marshal the data from the unmanaged memory block to a 
                      // MyStruct managed struct.
                      MyStruct myStruct = (MyStruct)Marshal.PtrToStructure(cds.lpData, 
                          typeof(MyStruct));
  
                      // Display the MyStruct data members.
                      this.lbNumber.Text = myStruct.Number.ToString();
                      this.lbMessage.Text = myStruct.Message;
                  }
              }
  
              base.WndProc(ref m);
          }
  
  
          [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
          internal struct MyStruct
          {
              public int Number;
  
              [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
              public string Message;
          }
  
  
          #region Native API Signatures and Types
  
          /// &lt;summary&gt;
          /// An application sends the WM_COPYDATA message to pass data to another 
          /// application.
          /// &lt;/summary&gt;
          internal const int WM_COPYDATA = 0x004A;
  
  
          /// &lt;summary&gt;
          /// The COPYDATASTRUCT structure contains data to be passed to another 
          /// application by the WM_COPYDATA message. 
          /// &lt;/summary&gt;
          [StructLayout(LayoutKind.Sequential)]
          internal struct COPYDATASTRUCT
          {
              public IntPtr dwData;       // Specifies data to be passed
              public int cbData;          // Specifies the data size in bytes
              public IntPtr lpData;       // Pointer to data to be passed
          }
  
          #endregion
      }
  }
  </code></pre>