C# 发送WM_COPYDATA消息

using System.Runtime.InteropServices; 

[DllImport("User32.dll", EntryPoint = "FindWindow")]
        
private static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
        [DllImport(
"User32.dll", EntryPoint = "SendMessage")]   //用于发送信息给窗体   
        private static extern int SendMessage(IntPtr hWnd,
int Msg, IntPtr wParam, ref COPYDATASTRUCT lParam);
        [StructLayout(LayoutKind.Sequential)]

        
public struct COPYDATASTRUCT
        
{

            
public IntPtr dwData;

            
public int cbData;

            
public IntPtr lpData;

        }


          COPYDATASTRUCT cds;
            
string a = "032";
            
byte[] sarr = System.Text.Encoding.Default.GetBytes(a);

            cds.dwData 
= (IntPtr)0;
            cds.cbData 
= a.Length;
            cds.lpData 
= Marshal.AllocHGlobal(sarr.Length);
            Marshal.Copy(sarr, 
0, cds.lpData, sarr.Length);

            IntPtr p 
= FindWindow("#32770""GetMessage");
            SendMessage(p, 
0x004A, (IntPtr)0,ref cds);
posted @ 2007-09-21 17:40  ahuo  阅读(760)  评论(1编辑  收藏  举报