Example #1
0
        protected static void SendReturnData(int communicationNo, IntPtr receiveWindowHandle, object data)
        {
            IntPtr globalData = IntPtr.Zero;

            try
            {
                CopyDataProtocolInfo communicatonData = new CopyDataProtocolInfo(IntPtr.Zero, data);
                byte[] bin = communicatonData.Serialize();
                NativeMethods.COPYDATASTRUCT copyData = new NativeMethods.COPYDATASTRUCT();
                copyData.dwData = IntPtr.Zero;
                copyData.cbData = (uint)bin.Length;
                copyData.lpData = globalData = Marshal.AllocHGlobal(bin.Length);
                Marshal.Copy(bin, 0, copyData.lpData, bin.Length);
                NativeMethods.SendMessage(receiveWindowHandle, NativeMethods.WM_COPYDATA, new IntPtr(communicationNo), ref copyData);
            }
            catch
            {
                //対象アプリケーションプロセスでの実行なので例外は投げない。
            }
            finally
            {
                if (globalData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(globalData);
                }
            }
        }
        /// <summary>
        /// 送受信。
        /// </summary>
        /// <param name="targetWindowHandle">送信対象ウィンドウハンドル。</param>
        /// <param name="data">送信データ。</param>
        /// <param name="recieveWindow">受信ウィンドウ。</param>
        /// <returns>受信データ。</returns>
        internal static object SendAndRecieve(IntPtr targetWindowHandle, object data, ReceiveAfterSend recieveWindow)
        {
            //通信番号生成
            int communicationNo = 0;

            if (!recieveWindow.UniqueNoManager.CreateNo(out communicationNo))
            {
                throw new InformationException(ResourcesLocal.Instance.OutOfCommunicationNo);
            }

            //使用可能なスレッドであるかチェック
            if (!recieveWindow.CanUseThread)
            {
                throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorInvalidThreadCall);
            }

            //送受信
            IntPtr globalData = IntPtr.Zero;

            try
            {
                //通信データ作成
                CopyDataProtocolInfo communicationData = new CopyDataProtocolInfo(recieveWindow.Handle, data);

                //シリアライズ
                byte[] bin = communicationData.Serialize();

                //WM_COPYDATAでの送信用構造体に移し替え
                NativeMethods.COPYDATASTRUCT copyData = new NativeMethods.COPYDATASTRUCT();
                copyData.dwData = IntPtr.Zero;
                copyData.cbData = (uint)bin.Length;
                copyData.lpData = globalData = Marshal.AllocHGlobal(bin.Length);
                Marshal.Copy(bin, 0, copyData.lpData, bin.Length);

                //送信
                IntPtr sendRet = NativeMethods.SendMessage(targetWindowHandle, NativeMethods.WM_COPYDATA, new IntPtr(communicationNo), ref copyData);
                if (sendRet != ReceiveForm.SendCopyDataSuccess)
                {
                    throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorAppCommunication);
                }

                //受信データ取得
                object receiveData;
                if (!recieveWindow.GetReceiveData(communicationNo, out receiveData))
                {
                    throw new FriendlyOperationException(ResourcesLocal.Instance.ErrorAppCommunication);
                }
                return(receiveData);
            }
            finally
            {
                //グローバルメモリ解放
                if (globalData != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(globalData);
                }
                recieveWindow.UniqueNoManager.FreeNo(communicationNo);
            }
        }
Example #3
0
 private static bool ProcessCopyData(ref Message message, out CopyDataProtocolInfo data)
 {
     data = null;
     if (message.Msg == NativeMethods.WM_COPYDATA)
     {
         //デシリアライズ
         //これが失敗するということは通信の状態が正常ではないので、詳細は返さず、ただ、通信に失敗したことだけ通知する
         try
         {
             NativeMethods.COPYDATASTRUCT globalData = (NativeMethods.COPYDATASTRUCT)message.GetLParam(typeof(NativeMethods.COPYDATASTRUCT));
             byte[] bin = new byte[(int)globalData.cbData];
             Marshal.Copy(globalData.lpData, bin, 0, bin.Length);
             data           = CopyDataProtocolInfo.Deserialize(bin);
             message.Result = SendCopyDataSuccess;
         }
         catch
         {
             message.Result = IntPtr.Zero;
         }
         return(true);
     }
     return(false);
 }