첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ COPYDATASTRUCT.cs

using System;
using System.Runtime.InteropServices;

namespace TestProject
{
    /// <summary>
    /// 데이터 복사 구조체
    /// </summary>
    public struct COPYDATASTRUCT
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// 데이터 핸들
        /// </summary>
        public IntPtr DataHandle;

        /// <summary>
        /// 데이터 카운트
        /// </summary>
        public uint DataCount;

        /// <summary>
        /// 데이터
        /// </summary>
        [MarshalAs(UnmanagedType.LPStr)]
        public string Data;

        #endregion
    }
}

 

▶ WIN32APIHelper.cs

using System;
using System.Runtime.InteropServices;
using System.Text;

namespace TestProject
{
    /// <summary>
    /// WIN32 API 헬퍼
    /// </summary>
    public static class WIN32APIHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Import
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 윈도우 찾기 - FindWindow(className, windowName)

        /// <summary>
        /// 윈도우 찾기
        /// </summary>
        /// <param name="className">클래스명</param>
        /// <param name="windowName">윈도우명</param>
        /// <returns>윈도우 핸들</returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr FindWindow(string className, string windowName);

        #endregion
        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter);

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage
        (
            IntPtr             windowHandle,
            uint               message,
            IntPtr             wordParameter,
            ref COPYDATASTRUCT longParameter
        );

        #endregion
        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter);

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern IntPtr SendMessage(IntPtr windowHandle, uint message, IntPtr wordParameter, StringBuilder longParameter);

        #endregion
        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter);

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage
        (
            IntPtr                                  windowHandle,
            uint                                    message,
            IntPtr                                  wordParameter,
            [MarshalAs(UnmanagedType.LPStr)] string longParameter
        );

        #endregion
        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter);

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll", EntryPoint = "SendMessageW")]
        public static extern IntPtr SendMessageW
        (
            IntPtr                                   windowHandle,
            uint                                     message,
            IntPtr                                   wordParameter,
            [MarshalAs(UnmanagedType.LPWStr)] string longParameter
        );

        #endregion
        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter);

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll")]
        public static extern IntPtr SendMessage(IntPtr windowHandle, uint message, int wordParameter, int longParameter);

        #endregion
        #region 메시지 보내기 - SendMessage(windowHandle, message, wordParameter, longParameter);

        /// <summary>
        /// 메시지 보내기
        /// </summary>
        /// <param name="windowHandle">윈도우 핸들</param>
        /// <param name="message">메시지</param>
        /// <param name="wordParameter">WORD 매개 변수</param>
        /// <param name="longParameter">LONG 매개 변수</param>
        /// <returns>처리 결과</returns>
        [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = false)]
        public static extern IntPtr SendMessage(HandleRef windowHandle, uint message, IntPtr wordParameter, IntPtr longParameter);

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region Field

        /// <summary>
        /// WM_COPYDATA
        /// </summary>
        public const int WM_COPYDATA = 0x004A;

        #endregion
    }
}

 

▶ MainForm.cs

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace TestProject
{
    /// <summary>
    /// 메인 폼
    /// </summary>
    public partial class MainForm : Form
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - MainForm()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainForm()
        {
            InitializeComponent();

            this.executeButton.Click += executeButton_Click;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        #region 윈도우 프로시저 처리하기 - WndProc(message)

        /// <summary>
        /// 윈도우 프로시저 처리하기
        /// </summary>
        /// <param name="message">메시지</param>
        protected override void WndProc(ref Message message)
        {
            switch(message.Msg)
            {
                case WIN32APIHelper.WM_COPYDATA :

                    COPYDATASTRUCT copyDataStruct1 = (COPYDATASTRUCT)Marshal.PtrToStructure(message.LParam, typeof(COPYDATASTRUCT));
                    COPYDATASTRUCT copyDataStruct2 = (COPYDATASTRUCT)message.GetLParam(typeof(COPYDATASTRUCT));

                    this.listBox.Items.Add($"WPARAM : {copyDataStruct1.Data}");
                    this.listBox.Items.Add($"LPARAM : {copyDataStruct2.Data}");

                    break;

                default:

                    break;

            }

            base.WndProc(ref message);
        }

        #endregion

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region 실행 버튼 클릭시 처리하기 - executeButton_Click(sender, e)

        /// <summary>
        /// 실행 버튼 클릭시 처리하기
        /// </summary>
        /// <param name="sender">이벤트 발생자</param>
        /// <param name="e">이벤트 인자</param>
        private void executeButton_Click(object sender, EventArgs e)
        {
            string message = $"메시지 : {DateTime.Now.ToString()}";

            COPYDATASTRUCT data = new COPYDATASTRUCT();

            data.DataHandle = (IntPtr)(1024 + 604);
            data.DataCount  = (uint)message.Length * sizeof(char);
            data.Data       = message;

            IntPtr windowHandle = Handle;

            WIN32APIHelper.SendMessage(windowHandle, WIN32APIHelper.WM_COPYDATA, IntPtr.Zero, ref data);
        }

        #endregion
    }
}
728x90
반응형
그리드형
Posted by 사용자 icodebroker

댓글을 달아 주세요