A la base je parlais surtout du premier ULONG_PTR dwData ou tu mettais ce qui te chantais.
Mais bon la memoire pointee par lpData est copiee quand tu envoie ce message. Ce qui est copie c’est lpData sur la longueur cbData. En fait c’est ton LPARAM de ton sendmessage qui est copié.
Tu peux le demontrer avec ce code (fait a l’arrache en 10 minutes).
Lance deux instances, dans une met le titre de la deuxiemme dans la textbox et clique sur le bouton.
[code]using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Runtime.InteropServices;
namespace WindowsApplication3
{
///
/// Summary description for Form1.
///
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.StatusBar statusBar1;
///
/// Required designer variable.
///
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows Form Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
this.textBox1 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.SuspendLayout();
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(88, 56);
this.textBox1.Name = “textBox1”;
this.textBox1.TabIndex = 0;
this.textBox1.Text = “”;
//
// button1
//
this.button1.Location = new System.Drawing.Point(88, 88);
this.button1.Name = “button1”;
this.button1.TabIndex = 1;
this.button1.Text = “button1”;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 249);
this.statusBar1.Name = “statusBar1”;
this.statusBar1.Size = new System.Drawing.Size(292, 22);
this.statusBar1.TabIndex = 2;
this.statusBar1.Text = “statusBar1”;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(292, 271);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox1);
this.Name = “Form1”;
this.Text = “Form1”;
this.Load += new System.EventHandler(this.Form1_Load);
this.ResumeLayout(false);
}
#endregion
///
/// The main entry point for the application.
///
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void Form1_Load(object sender, System.EventArgs e) {
this.Text = this.Handle.ToString();
}
private const int WM_COPYDATA = 0x4A;
[DllImport(“user32.dll”)]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam,
IntPtr data);
private void button1_Click(object sender, System.EventArgs e) {
IntPtr targetHandle = new IntPtr(long.Parse(textBox1.Text));
string text = “Blah bblah blah”;
IntPtr textPtr = Marshal.StringToHGlobalAnsi(text);
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds.data = 0;
cds.length = text.Length;
cds.pData = textPtr;
IntPtr data = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(COPYDATASTRUCT)));
Marshal.StructureToPtr(cds, data, false);
IntPtr result = SendMessage(targetHandle, WM_COPYDATA, IntPtr.Zero, data);
this.statusBar1.Text = “SENT LPARAM PTR: " + data.ToString() +”; " +cds.pData.ToString();
Marshal.FreeHGlobal(textPtr);
Marshal.FreeHGlobal(data);
}
protected override void WndProc(ref Message m) {
switch(m.Msg) {
case WM_COPYDATA:
COPYDATASTRUCT cds = new COPYDATASTRUCT();
cds = (COPYDATASTRUCT) Marshal.PtrToStructure(m.LParam, typeof(COPYDATASTRUCT));
this.statusBar1.Text = "REICEIVED LPARAM PTR: " + m.LParam.ToString() + "; " + cds.pData.ToString();
if (cds.length > 0) {
string text = Marshal.PtrToStringAnsi(cds.pData).Substring(0,cds.length);
}
base.WndProc(ref m);
break;
default:
base.WndProc (ref m);
break;
}
}
}
[StructLayout(LayoutKind.Sequential)]
public struct COPYDATASTRUCT {
public int data;
public int length;
public IntPtr pData;
}
}[/code]