Vba 如何将通过WM_COPYDATA发送的数据提取到VB6应用程序中?

Vba 如何将通过WM_COPYDATA发送的数据提取到VB6应用程序中?,vba,vb6,Vba,Vb6,我在本文中提到了使用win32 api(SendMessage、FindWindow、WM#U COPYDATA)将消息从一个C#.NET应用程序发送到另一个C#.NET应用程序的代码 我的要求是目标应用程序是VB6应用程序(我们可以访问其源代码)。。我正在使用以下代码向VB6 exe应用程序发送消息。但是,如何在VB6应用程序中接收消息?(以下是C#NET中发送/接收消息的代码) 发送信息的代码:- #region Using directives using System; using Sy

我在本文中提到了使用win32 api(SendMessage、FindWindow、WM#U COPYDATA)将消息从一个C#.NET应用程序发送到另一个C#.NET应用程序的代码

我的要求是目标应用程序是VB6应用程序(我们可以访问其源代码)。。我正在使用以下代码向VB6 exe应用程序发送消息。但是,如何在VB6应用程序中接收消息?(以下是C#NET中发送/接收消息的代码)

发送信息的代码:-

#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

        /// <summary>
        /// An application sends the WM_COPYDATA message to pass data to another 
        /// application
        /// </summary>
        internal const int WM_COPYDATA = 0x004A;


        /// <summary>
        /// The COPYDATASTRUCT structure contains data to be passed to another 
        /// application by the WM_COPYDATA message. 
        /// </summary>
        [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
        }


        /// <summary>
        /// The class exposes Windows APIs to be used in this code sample.
        /// </summary>
        [SuppressUnmanagedCodeSecurity]
        internal class NativeMethod
        {
            /// <summary>
            /// 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. 
            /// </summary>
            /// <param name="hWnd">
            /// Handle to the window whose window procedure will receive the message.
            /// </param>
            /// <param name="Msg">Specifies the message to be sent.</param>
            /// <param name="wParam">
            /// Specifies additional message-specific information.
            /// </param>
            /// <param name="lParam">
            /// Specifies additional message-specific information.
            /// </param>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int Msg,
                IntPtr wParam, ref COPYDATASTRUCT lParam);


            /// <summary>
            /// 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.
            /// </summary>
            /// <param name="lpClassName">Class name</param>
            /// <param name="lpWindowName">Window caption</param>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        }

        #endregion
    }
}
#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

        /// <summary>
        /// An application sends the WM_COPYDATA message to pass data to another 
        /// application.
        /// </summary>
        internal const int WM_COPYDATA = 0x004A;


        /// <summary>
        /// The COPYDATASTRUCT structure contains data to be passed to another 
        /// application by the WM_COPYDATA message. 
        /// </summary>
        [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
    }
}
#使用指令的区域
使用制度;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
使用系统安全;
#端区
命名空间CSSendWM_COPYDATA
{
公共部分类主窗体:窗体
{
公共表格(
{
初始化组件();
}
私有void btnSendMessage_单击(对象发送者,事件参数e)
{
//找到目标窗口句柄。
IntPtr hTargetWnd=NativeMethod.FindWindow(null,“CSReceiveWM_COPYDATA”);
如果(hTargetWnd==IntPtr.Zero)
{
MessageBox.Show(“无法找到\“CSReceiveWM\u COPYDATA\”窗口”,
“错误”,MessageBoxButtons.OK,MessageBoxIcon.Error);
返回;
}
//使用要发送的数据准备COPYDATASTRUCT结构。
MyStruct MyStruct;
int n编号;
如果(!int.TryParse(this.tbNumber.Text,out nNumber))
{
MessageBox.Show(“数字的值无效!”);
返回;
}
myStruct.Number=nnnumber;
myStruct.Message=this.tbMessage.Text;
//将托管结构封送到本机内存块。
int myStructSize=Marshal.SizeOf(myStruct);
IntPtr pMyStruct=Marshal.AllocHGlobal(myStructSize);
尝试
{
Marshal.StructureToPtr(myStruct、pmystuct、true);
COPYDATASTRUCT cds=新的COPYDATASTRUCT();
cds.cbData=myStructSize;
cds.lpData=pmystuct;
//通过WM_COPYDATA消息将COPYDATASTRUCT结构发送到
//接收窗口。(应用程序必须使用SendMessage,
//而不是PostMessage发送WM_COPYDATA,因为接收
//应用程序必须在保证有效的情况下接受。)
NativeMethod.SendMessage(hTargetWnd,WM_COPYDATA,this.Handle,ref-cds);
int result=Marshal.GetLastWin32Error();
如果(结果!=0)
{
MessageBox.Show(String.Format(
“发送消息(WM_COPYDATA)失败,错误0x{0:X}”,结果);
}
}
最后
{
弗里赫全球元帅(pMyStruct);
}
}
[StructLayout(LayoutKind.Sequential,CharSet=CharSet.Unicode)]
内部结构MyStruct
{
公共整数;
[Marshallas(UnmanagedType.ByValTStr,SizeConst=256)]
公共字符串消息;
}
#区域本机API签名和类型
/// 
///应用程序发送WM_COPYDATA消息以将数据传递给另一个应用程序
///应用
/// 
内部常数int WM_COPYDATA=0x004A;
/// 
///COPYDATASTRUCT结构包含要传递给另一个结构的数据
///WM_COPYDATA消息的应用程序。
/// 
[StructLayout(LayoutKind.Sequential)]
内部结构COPYDATASTRUCT
{
public IntPtr dwData;//指定要传递的数据
public int cbData;//以字节为单位指定数据大小
public IntPtr lpData;//指向要传递的数据的指针
}
/// 
///该类公开要在此代码示例中使用的Windows API。
/// 
[SuppressUnmanagedCodeSecurity]
内部类NativeMethod
{
/// 
///将指定的消息发送到一个或多个窗口。SendMessage
///函数调用指定窗口的窗口过程,并执行以下操作
///直到窗口过程处理完消息后才返回。
/// 
/// 
///其窗口过程将接收消息的窗口的句柄。
/// 
///指定要发送的消息。
/// 
///指定其他特定于消息的信息。
/// 
/// 
///指定其他特定于消息的信息。
/// 
/// 
[DllImport(“user32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
公共静态外部IntPtr SendMessage(IntPtr hWnd、int Msg、,
IntPtr wParam,ref COPYDATASTRUCT lParam);
/// 
///FindWindow函数检索顶级窗口的句柄
///其类名和窗口名与指定的字符串匹配。此
///函数不搜索子窗口。此函数不
///执行区分大小写的搜索。
/// 
///类名
///窗口标题
/// 
[DllImport(“user32.dll”,CharSet=CharSet.Auto,SetLastError=true)]
公共静态外部IntPtr FindWindow(字符串lpClassName,字符串lpWindowName);
}
#端区
}
}
接收信息的代码:-

#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

        /// <summary>
        /// An application sends the WM_COPYDATA message to pass data to another 
        /// application
        /// </summary>
        internal const int WM_COPYDATA = 0x004A;


        /// <summary>
        /// The COPYDATASTRUCT structure contains data to be passed to another 
        /// application by the WM_COPYDATA message. 
        /// </summary>
        [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
        }


        /// <summary>
        /// The class exposes Windows APIs to be used in this code sample.
        /// </summary>
        [SuppressUnmanagedCodeSecurity]
        internal class NativeMethod
        {
            /// <summary>
            /// 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. 
            /// </summary>
            /// <param name="hWnd">
            /// Handle to the window whose window procedure will receive the message.
            /// </param>
            /// <param name="Msg">Specifies the message to be sent.</param>
            /// <param name="wParam">
            /// Specifies additional message-specific information.
            /// </param>
            /// <param name="lParam">
            /// Specifies additional message-specific information.
            /// </param>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr SendMessage(IntPtr hWnd, int Msg,
                IntPtr wParam, ref COPYDATASTRUCT lParam);


            /// <summary>
            /// 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.
            /// </summary>
            /// <param name="lpClassName">Class name</param>
            /// <param name="lpWindowName">Window caption</param>
            /// <returns></returns>
            [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
            public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
        }

        #endregion
    }
}
#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

        /// <summary>
        /// An application sends the WM_COPYDATA message to pass data to another 
        /// application.
        /// </summary>
        internal const int WM_COPYDATA = 0x004A;


        /// <summary>
        /// The COPYDATASTRUCT structure contains data to be passed to another 
        /// application by the WM_COPYDATA message. 
        /// </summary>
        [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
    }
}
#使用指令的区域
使用制度;
使用System.Windows.Forms;
使用System.Runtime.InteropServices;
#端区
命名空间CSReceiveWM_COPYDATA
{
公共部分类主窗体:窗体
{
公共表格(
{
初始化组件();
}
P