Welcome to ErranLi's Blog!

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  106 Posts :: 1 Stories :: 97 Comments :: 0 Trackbacks

常用链接

留言簿(12)

搜索

  •  

积分与排名

  • 积分 - 152211
  • 排名 - 146

最新评论

阅读排行榜

 
http://www.cppblog.com/erran/archive/2012/05/19/175360.html

 C#消息处理函数引用 
  /// <summary> 
/// <para> 版 权 : Copyright (c) 20010-2011 </para>
/// <para> 项 目 : xxxxx/RD/xxxx </para> 
/// <para> 文件名称 : </para>
/// <para> 创 建 人 : lizhi </para>
/// <para> 创建日期 : 2010-11-22 </para>
/// <remarks> 备 注 : 
///     ddgooo@sina.com
/// </remarks>
/// </summary>
///
 
     public class Win32Message
    {
        
/// <summary>
        
/// 消息
        
/// </summary>
        public const int WM_COPYDATA = 0x004A;

        
/// <summary>
        
/// WM_COPYDATA消息结构体
        
/// </summary>
        [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
        
public struct COPYDATASTRUCT
        {
            
/// <summary>
            
/// 用户定义数据
            
/// </summary>
            public IntPtr   dwData;
            
/// <summary>
            
/// 数据大小
            
/// </summary>
            public int      cbData;
            
/// <summary>
            
/// 指向数据的指针
            
/// </summary>            
            public IntPtr lpData;
        }

        
/// <summary>
        
/// 注册消息用
        
/// 在win7中,如果以管理员方式运行,需要加入该语句,注册WM_COPYDATA消息,避免过滤掉该消息。
        
/// </summary>
        
/// <param name="msg"></param>
        
/// <param name="flags"></param>
        
/// <returns></returns>
        [DllImport("user32")]
        
public static extern bool ChangeWindowMessageFilter(int msg, int flags);
        

        
/// <summary>
        
/// 发送WM_COPYDATA消息
        
/// </summary>
        
/// <param name="hWnd"></param>
        
/// <param name="msg"></param>
        
/// <param name="wParam"></param>
        
/// <param name="lParam"></param>
        
/// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "SendMessage")]
        
public static extern int SendMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam);
        
/// <summary>
        
/// 发送WM_COPYDATA消息
        
/// </summary>
        
/// <param name="hWnd"></param>
        
/// <param name="msg"></param>
        
/// <param name="wParam"></param>
        
/// <param name="lParam"></param>
        
/// <returns></returns>
        [DllImport("user32")]
        
public static extern bool PostMessage(int hWnd, int msg, IntPtr wParam, IntPtr lParam); 
                        
        
/// <summary>
        
/// 查找句柄
        
/// </summary>
        
/// <param name="lpClassName"></param>
        
/// <param name="lpWindowName"></param>
        
/// <returns></returns>
        [DllImport("User32.dll", EntryPoint = "FindWindow")]
        
public static extern IntPtr FindWindow(string lpClassName,string lpWindowName);

        
/// <summary>
        
/// 查找句柄
        
/// </summary>
        
/// <param name="hwndParent"></param>
        
/// <param name="hwndChildAfter"></param>
        
/// <param name="lpszClass"></param>
        
/// <param name="lpszWindow"></param>
        
/// <returns></returns>
        [DllImport("user32.dll", EntryPoint = "FindWindowEx")]
        
public static extern IntPtr FindWindowEx(IntPtr hwndParent,IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

    }
C# 消息解析函数

/// <summary> 
/// <para> 版 权 : Copyright (c) 20010-2011 </para>
/// <para> 项 目 : xxxxx/RD/xxxx </para> 
/// <para> 文件名称 : </para>
/// <para> 创 建 人 : lizhi </para>
/// <para> 创建日期 : 2010-11-22 </para>
/// <remarks> 备 注 : 
///     ddgooo@sina.com
/// </remarks>
/// </summary>
///
 
    /// <summary>
    
/// 脚本测试类
    
/// </summary> 
    public class TestingMessage
    {   
        
/// <summary>
        
/// 消息 开始
        
/// </summary>
        public const int WM_USER_START = 0x04DD;
        
/// <summary>
        
/// 消息 继续
        
/// </summary>
        public const int WM_USER_CONTINUE = 0x05F8;
        
/// <summary>
        
/// 消息 停止
        
/// </summary>
        public const int WM_USER_ABORT = 0x06F9;

        
/// <summary>
        
/// 最后的消息数据
        
/// </summary>
        public string testingScript = null;

        
/// <summary>
        
/// 初始化
        
/// </summary>
        public bool tetsingEnable = false;

        
/// <summary>
        
/// 初始化
        
/// </summary>
        
/// <returns></returns>
        public bool Initiate()
        {    
            Win32Message.ChangeWindowMessageFilter(Win32Message.WM_COPYDATA, 
1);

            tetsingEnable 
= true;

            
return true;
        }

        
/// <summary>
        
/// 解析消息
        
/// </summary>
        
/// <param name="msg"></param>
        
/// <returns></returns>
        public bool DoMessage(ref System.Windows.Forms.Message msg)
        {
            
bool tested = false;

            
switch (msg.Msg)
            {
                
case Win32Message.WM_COPYDATA :
                    DoMsgCopyData(
ref msg);
                    tested 
= true;
                    
break;

                
case WM_USER_START :
                    DoMsgStart(
ref msg);
                    tested 
= true;
                    
break;

                
case WM_USER_CONTINUE :
                    DoMsgContinue(
ref msg);
                    tested 
= true;
                    
break;

                
case WM_USER_ABORT :
                    DoMsgAbort(
ref msg);
                    tested 
= true;
                    
break;

                
default :
                    
break;
            }

            
return tested;
        }
        
        
/// <summary>
        
/// 处理拷贝数据
        
/// </summary>
        
/// <param name="msg"></param>
        
/// <returns></returns>
        public bool DoMsgCopyData(ref System.Windows.Forms.Message msg)
        {
            
bool tested = false;

            
if (tetsingEnable)
            {
                
try
                {
                    Win32Message.COPYDATASTRUCT mystr 
= new Win32Message.COPYDATASTRUCT();
                    Type mytype 
= mystr.GetType();
                    mystr 
= (Win32Message.COPYDATASTRUCT)msg.GetLParam(mytype);
                    Byte[] scriptbytes 
= new Byte[mystr.cbData];
                    Marshal.Copy(mystr.lpData, scriptbytes, 
0, mystr.cbData);
                    System.Text.Encoding utf8 
= System.Text.Encoding.UTF8;
                    testingScript 
= utf8.GetString(scriptbytes);

                    System.Windows.Forms.MessageBox.Show(testingScript);

                    tested 
= true;
                }
                
catch
                {
                    tested 
= false;
                }
            }

            
return tested;
        }


        
/// <summary>
        
/// 开始
        
/// </summary>
        
/// <param name="msg"></param>
        
/// <returns></returns>
        public bool DoMsgStart(ref System.Windows.Forms.Message msg)
        {
            
bool tested = false;

            
if (tetsingEnable)
            {
                
try
                {
                    
if (testingScript != null)
                    {
                        tested 
= XXXX.Parser.TestingParser.Instance.TestRunning(testingScript);

                        System.Windows.Forms.MessageBox.Show(
"SERVER DoMsgStart");
                    }
                }
                
catch
                {
                    tested 
= false;
                }
            }

            
return tested;
        }

        
        
/// <summary>
        
/// 继续
        
/// </summary>
        
/// <param name="msg"></param>
        
/// <returns></returns>
        public bool DoMsgContinue(ref System.Windows.Forms.Message msg)
        {
            
bool tested = false;

            
if (tetsingEnable)
            {
                
try
                {
                    
if (testingScript != null)
                    {
                        tested 
=  XXXX .Parser.TestingParser.Instance.TestContinue();

                        System.Windows.Forms.MessageBox.Show(
"SERVER DoMsgContinue");
                    }
                }
                
catch
                {
                    tested 
= false;
                }
            }

            
return tested;
        }


        
/// <summary>
        
/// 继续
        
/// </summary>
        
/// <param name="msg"></param>
        
/// <returns></returns>
        public bool DoMsgAbort(ref System.Windows.Forms.Message msg)
        {
            
bool tested = false;

            
if (tetsingEnable)
            {
                
try
                {
                    
if (testingScript != null)
                    {
                        tested 
=  XXXX .Parser.TestingParser.Instance.TestAbort();
                        testingScript 
= null;

                        System.Windows.Forms.MessageBox.Show(
"SERVER DoMsgAbort");
                    }
                }
                
catch
                {
                    tested 
= false;
                }
            }

            
return tested;
        }

    }
C# 窗体
/// <summary> 
/// <para> 版 权 : Copyright (c) 20010-2011 </para>
/// <para> 项 目 : xxxxx/RD/xxxx </para> 
/// <para> 文件名称 : </para>
/// <para> 创 建 人 : lizhi </para>
/// <para> 创建日期 : 2010-11-22 </para>
/// <remarks> 备 注 : 
///     ddgooo@sina.com
/// </remarks>
/// </summary>
///
 
   public partial class FormMain : Form
    {

        
/// <summary>
        
/// 消息解析
        
/// </summary>
        private TestingMessage testingMessage = null;

        
/// <summary>
        
/// 构造
        
/// </summary>
        public FormMain()
        {
            testingMessage 
= new TestingMessage();

            InitializeComponent();
        }

        
/// <summary>
        
/// 加载
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void FormMain_Load(object sender, EventArgs e)
        {
            testingMessage.Initiate();
        }

        
/// <summary>
        
/// 关闭
        
/// </summary>
        
/// <param name="sender"></param>
        
/// <param name="e"></param>
        private void FormMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Windows.Forms.Message msg 
= new Message();
            testingMessage.DoMsgAbort(
ref msg);
            testingMessage 
= null;
        }

        
/// <summary>
        
/// 消息处理函数
        
/// </summary>
        
/// <param name="m"></param>
        protected override void DefWndProc(ref System.Windows.Forms.Message msg)
        {
            
if (!testingMessage.DoMessage(ref msg))
            {
                
base.DefWndProc(ref msg);
            }
        }

    }

MFC DLL 消息发送端

CTestingMessage::CTestingMessage(
void)
{
    initializeSuccessed 
= false;

    processAppHandle 
= NULL;

    processAppName 
= _T("\\\\XXXX\\XXXX.XXXXX.XXXX.exe");
    processWindowName 
= _T("XXXXXXXX");    
}

CTestingMessage::
~CTestingMessage(void)
{
}


CTestingMessage
* CTestingMessage::Instance()
{
    
static CTestingMessage* ins = NULL;
    
if (ins == NULL)
    {
        ins 
= new CTestingMessage();
    }
    
return ins;
}

bool CTestingMessage::InitTesting(void)
{
    
bool bFlag = false;

    processMutex.Lock();

    
try
    {
        ::ChangeWindowMessageFilter(WM_COPYDATA, 
1);
        
/*
        TCHAR exepath[MAX_PATH];
        memset(exepath, 0, MAX_PATH);
        GetModuleFileName(NULL, exepath, MAX_PATH);    

        CString strdir = exepath;
        strdir = strdir.Left(strdir.ReverseFind('\\'));
        
        CString szAppName = strdir;
        szAppName += processAppName;

        MessageBox(NULL, szAppName, _T(""), 0);

        PROCESS_INFORMATION pi;    
                    
        bFlag = ::CreateProcess(szAppName, 
            NULL, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, NULL, &pi);

        if (bFlag)
        {
            processAppHandle = pi.hProcess;
        } 
*/
        bFlag 
= true;
    }
    
catch(CException* e)
    {
        bFlag 
= false;    
        processAppHandle 
= NULL;
    }
    
    initializeSuccessed 
= bFlag;

    processMutex.Unlock();

    
return bFlag;
}


bool CTestingMessage::ExitTesting(void)
{
    
bool bFlag = false;

    processMutex.Lock();

    initializeSuccessed 
= false;

    
if (processAppHandle != NULL)
    {
        ::TerminateProcess(processAppHandle, 
0);
         CloseHandle(processAppHandle);
        processAppHandle 
= NULL;        
    }

    bFlag 
= true;    

    processMutex.Unlock();

    
return bFlag;
}


bool CTestingMessage::StartTesting(LPCSTR pScript)
{
    
bool bFlag = false;

    processMutex.Lock();    

    
if (initializeSuccessed)
    {
        HWND hWnd 
= ::FindWindow(NULL, processWindowName);
        
if (hWnd != NULL)
        {
            
// 发送数据
            COPYDATASTRUCT cd;
            cd.cbData 
= strlen(pScript);
            cd.dwData 
= WM_COPYDATA;
            cd.lpData 
= (void*)pScript;
            ::SendMessage(hWnd, WM_COPYDATA, 
0, (LPARAM)(&cd));

            
// 发送命令
            ::SendMessage(hWnd, WM_USER_START, 00);
            
            bFlag 
= true;
        }
    }

    processMutex.Unlock();

    
return bFlag;
}

bool CTestingMessage::AbortTesting()
{
    
bool bFlag = false;

    processMutex.Lock();
    
    
if (initializeSuccessed)
    {
        HWND hWnd 
= ::FindWindow(NULL, processWindowName);
        
if (hWnd != NULL)
        {            
            ::SendMessage(hWnd, WM_USER_ABORT, 
00);
            
            bFlag 
= true;
        }
    }

    processMutex.Unlock();

    
return bFlag;    
}

void CTestingMessage::ContinueTesting()
{
    
bool bFlag = false;

    processMutex.Lock();

    
if (initializeSuccessed)
    {
        HWND hWnd 
= ::FindWindow(NULL, processWindowName);
        
if (hWnd != NULL)
        {            
            ::SendMessage(hWnd, WM_USER_CONTINUE, 
00);
            
            bFlag 
= true;
        }
    }

    processMutex.Unlock();
}


posted on 2012-05-19 09:27 erran 阅读(720) 评论(0)  编辑 收藏 引用 所属分类: dotNet

只有注册用户登录后才能发表评论。
【推荐】超50万行VC++源码: 大型组态工控、电力仿真CAD与GIS源码库
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理