Hi Jan,

The code you have below does work the way it was intended, I am including
my test code.

/**** NativeMethods.cs ***/
public class NativeMethods
{
 public NativeMethods(){}
 [StructLayout(LayoutKind.Sequential)]
 public struct CopyDataStruct
 {
 public string ID;
 public int Length;
 public string Data;
 }
 [DllImport("user32.dll", EntryPoint="FindWindow")]
 public extern static System.IntPtr FindWindow(string lpClasName,
                                    string lpWindowName);
 public const int WM_COPYDATA = 0x004a;
 [DllImport("user32.dll", EntryPoint="SendMessage")]
 public extern static int SendMessage(System.IntPtr hWnd,
                          int Msg, int wParam,
                          ref CopyDataStruct lParam);
}

/**** SendingForm.cs ***/
private void SendingForm_Load(object sender, System.EventArgs e)
{
 ReceivingForm rec = new ReceivingForm();
 rec.Show();
}

private void button1_Click(object sender, System.EventArgs e)
{
 NativeMethods.CopyDataStruct DataStruct = new NativeMethods.CopyDataStruct
();
 DataStruct.ID = "1";
 DataStruct.Data = "Sample Text";
 DataStruct.Length = DataStruct.Data.Length;
 IntPtr WHnd = NativeMethods.FindWindow(null, "ReceivingForm");
 if(!WHnd.Equals(System.IntPtr.Zero))
 {
  NativeMethods.SendMessage(WHnd, NativeMethods.WM_COPYDATA,
                            0, ref DataStruct);
 }
}

/**** ReceivingForm.cs ***/
protected override void WndProc(ref Message m)
{
 switch(m.Msg)
 {
 case(NativeMethods.WM_COPYDATA) :
  NativeMethods.CopyDataStruct ds =
(NativeMethods.CopyDataStruct)m.GetLParam(typeof
(NativeMethods.CopyDataStruct));
  string s = ds.Data;
  break;
 default :
  break;
 }
 base.WndProc(ref m);
}


Have you tried stepping through the code?  First thing is to make sure
that the resultant of the FindWindow() method succeeds (non-zero).  If
that is successful, set a breakpoint on the ReceivingForm.cs line within
the switch statement, within the debugger I was able to retrieve the
string data from the data structure.  Also, verify your WIN32 API
declarations with mine above.  Plus my main startup form is SendingForm.cs.

Hope this helps,
Keith

On Tue, 30 Apr 2002 02:40:27 -0700, Jan Dropczynski <j.dropczynski@THONA-
CONSULTING.COM> wrote:

>Hi Keith,
>sure I can post the code. ;) Here it is:
>
>/** In my sending form **/
>public const int WM_COPYDATA = 0x004A;
>
>// this is the data to be send
>[StructLayout(LayoutKind.Sequential)]
>public struct CopyDataStruct {
>  public string ID;
>  public int Length;
>  public string Data;
>}
>
>CopyDataStruct DataStruct = new CopyDataStruct();
>DataStruct.ID = 1;
>DataStruct.Data = "Sample Text";
>DataStruct.Length = DataStruct.Data.Length;
>IntPtr WHnd = FindWindow( null, "TestForm");
>// that works fine
>SendMessage( WHnd, WM_COPYDATA, this.Handle, DataStruct);
>
>/** In my receiving form **/
>protected override void WndProc (ref Message m) {
>  switch (m.Msg) {
>    case WindowMessaging.WM_COPYDATA:
>      // Here is nothing happens!! A message with that command seems
>      // never be arrived
>      CopyDataStruct DataStruct = (CopyDataStruct) m.GetLParam (typeof
>CopyDataStruct));
>      string s = DataStruct.Data;
>      break;
>    default:
>      base.WndProc(ref m);
>      break;
>  }
>}
>
>That's it! Hope you have an idea!
>Curiously is if I send a message with my own message command I can handle
>it!!
>Thanks and Regards,
>Jan
>
>You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
>subscribe to other DevelopMentor lists at http://discuss.develop.com.

You can read messages from the DOTNET archive, unsubscribe from DOTNET, or
subscribe to other DevelopMentor lists at http://discuss.develop.com.

Reply via email to