当前位置
主页 > 产品中心 > 产品一类 >
yobo体育app下载官网:在Wince/WM实现进程间通信
2022-08-22 04:51
本文摘要:做到WM上的进程间通信,用于WindowMessage构建两个进程间的通信,感觉MessageWindow不好用,所以就用别的方法构建接管WindowsMessage。

yobo体育app下载官网

做到WM上的进程间通信,用于WindowMessage构建两个进程间的通信,感觉MessageWindow不好用,所以就用别的方法构建接管WindowsMessage。再行来PCB一下必须用于的功能,命名为Cls_Message:viewplaincopytoclipboardprint?usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingMicrosoft.WindowsCE.Forms;usingSystem.Windows.Forms;classCls_Message{privatestructCOPYDATASTRUCT{publicintdwData;publicintcbData;publicIntPtrlpData;}//-------------------------------------------------------------------------------privateconstintWM_COPYDATA=0x004A;privateconstintGWL_WNDPROC=-4;privateconstintLMEM_FIXED=0x0000;privateconstintLMEM_ZEROINIT=0x0040;privateconstintLPTR=(LMEM_FIXED|LMEM_ZEROINIT);privateIntPtroldWndProc=IntPtr.Zero;privateWndProcDelegatenewWndProc;privateIntPtrformHandle;//-------------------------------------------------------------------------------delegateIntPtrWndProcDelegate(IntPtrhWnd,uintmsg,IntPtrwParam,IntPtrlParam);[DllImport(coredll.dll)]staticexternIntPtrCallWindowProc(IntPtrlpPrevWndFunc,IntPtrhWnd,uintMsg,IntPtrwParam,IntPtrlParam);[DllImport(coredll.dll,EntryPoint=GetWindowLong)]privatestaticexternIntPtrGetWindowLong(IntPtrhWnd,intnIndex);[DllImport(coredll.dll)]staticexternintSetWindowLong(IntPtrhWnd,intnIndex,IntPtrnewWndProc);[DllImport(coredll.dll,EntryPoint=FindWindow)]publicstaticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName);[DllImport(coredll.dll)]privatestaticexternintSendMessageW(IntPtrhWnd,intMsg,IntPtrwParam,IntPtrlParam);[DllImport(coredll.dll)]privatestaticexternIntPtrLocalAlloc(intflag,intsize);[DllImport(coredll.dll)]privatestaticexternIntPtrLocalFree(IntPtrp);///summary///初始化消息类////summary///paramname=handle拒绝接受消息的窗体的句柄/parampublicCls_Message(IntPtrformHandle){this.formHandle=formHandle;newWndProc=newWndProcDelegate(WndProc);oldWndProc=GetWindowLong(formHandle,GWL_WNDPROC);intsuccess=SetWindowLong(formHandle,GWL_WNDPROC,Marshal.GetFunctionPointerForDelegate(newWndProc));}///summary///消息处置////summaryprivateIntPtrWndProc(IntPtrhWnd,uintmsg,IntPtrwParam,IntPtrlParam){if(msg==WM_COPYDATA){COPYDATASTRUCTst=(COPYDATASTRUCT)Marshal.PtrToStructure(lParam,typeof(COPYDATASTRUCT));stringstr=Marshal.PtrToStringUni(st.lpData);MessageBox.Show(str);}returnCallWindowProc(oldWndProc,this.formHandle,msg,wParam,lParam);}staticprivateIntPtrAllocHGlobal(intcb){IntPtrhMemory=newIntPtr();hMemory=LocalAlloc(LPTR,cb);returnhMemory;}staticprivatevoidFreeHGlobal(IntPtrhMemory){if(hMemory!=IntPtr.Zero)LocalFree(hMemory);}///summary///发送到消息////summary///paramname=formTitle目标窗体的名称/param///paramname=message消息内容/paramstaticpublicvoidSendMessage(StringformTitle,Stringmessage){IntPtrhWndDest=FindWindow(null,formTitle);COPYDATASTRUCToCDS=newCOPYDATASTRUCT();oCDS.cbData=(message.Length+1)*2;oCDS.lpData=LocalAlloc(LPTR,oCDS.cbData);Marshal.Copy(message.ToCharArray(),0,oCDS.lpData,message.Length);oCDS.dwData=1;IntPtrlParam=AllocHGlobal(oCDS.cbData);Marshal.StructureToPtr(oCDS,lParam,false);SendMessageW(hWndDest,WM_COPYDATA,IntPtr.Zero,lParam);LocalFree(oCDS.lpData);FreeHGlobal(lParam);}}usingSystem;usingSystem.Collections.Generic;usingSystem.Text;usingSystem.Runtime.InteropServices;usingMicrosoft.WindowsCE.Forms;usingSystem.Windows.Forms;classCls_Message{privatestructCOPYDATASTRUCT{publicintdwData;publicintcbData;publicIntPtrlpData;}//-------------------------------------------------------------------------------privateconstintWM_COPYDATA=0x004A;privateconstintGWL_WNDPROC=-4;privateconstintLMEM_FIXED=0x0000;privateconstintLMEM_ZEROINIT=0x0040;privateconstintLPTR=(LMEM_FIXED|LMEM_ZEROINIT);privateIntPtroldWndProc=IntPtr.Zero;privateWndProcDelegatenewWndProc;privateIntPtrformHandle;//-------------------------------------------------------------------------------delegateIntPtrWndProcDelegate(IntPtrhWnd,uintmsg,IntPtrwParam,IntPtrlParam);[DllImport(coredll.dll)]staticexternIntPtrCallWindowProc(IntPtrlpPrevWndFunc,IntPtrhWnd,uintMsg,IntPtrwParam,IntPtrlParam);[DllImport(coredll.dll,EntryPoint=GetWindowLong)]privatestaticexternIntPtrGetWindowLong(IntPtrhWnd,intnIndex);[DllImport(coredll.dll)]staticexternintSetWindowLong(IntPtrhWnd,intnIndex,IntPtrnewWndProc);[DllImport(coredll.dll,EntryPoint=FindWindow)]publicstaticexternIntPtrFindWindow(stringlpClassName,stringlpWindowName);[DllImport(coredll.dll)]privatestaticexternintSendMessageW(IntPtrhWnd,intMsg,IntPtrwParam,IntPtrlParam);[DllImport(coredll.dll)]privatestaticexternIntPtrLocalAlloc(intflag,intsize);[DllImport(coredll.dll)]privatestaticexternIntPtrLocalFree(IntPtrp);///summary///初始化消息类////summary///paramname=handle拒绝接受消息的窗体的句柄/parampublicCls_Message(IntPtrformHandle){this.formHandle=formHandle;newWndProc=newWndProcDelegate(WndProc);oldWndProc=GetWindowLong(formHandle,GWL_WNDPROC);intsuccess=SetWindowLong(formHandle,GWL_WNDPROC,Marshal.GetFunctionPointerForDelegate(newWndProc));}///summary///消息处置////summaryprivateIntPtrWndProc(IntPtrhWnd,uintmsg,IntPtrwParam,IntPtrlParam){if(msg==WM_COPYDATA){COPYDATASTRUCTst=(COPYDATASTRUCT)Marshal.PtrToStructure(lParam,typeof(COPYDATASTRUCT));stringstr=Marshal.PtrToStringUni(st.lpData);MessageBox.Show(str);}returnCallWindowProc(oldWndProc,this.formHandle,msg,wParam,lParam);}staticprivateIntPtrAllocHGlobal(intcb){IntPtrhMemory=newIntPtr();hMemory=LocalAlloc(LPTR,cb);returnhMemory;}staticprivatevoidFreeHGlobal(IntPtrhMemory){if(hMemory!=IntPtr.Zero)LocalFree(hMemory);}///summary///发送到消息////summary///paramname=formTitle目标窗体的名称/param///paramname=message消息内容/paramstaticpublicvoidSendMessage(StringformTitle,Stringmessage){IntPtrhWndDest=FindWindow(null,formTitle);COPYDATASTRUCToCDS=newCOPYDATASTRUCT();oCDS.cbData=(message.Length+1)*2;oCDS.lpData=LocalAlloc(LPTR,oCDS.cbData);Marshal.Copy(message.ToCharArray(),0,oCDS.lpData,message.Length);oCDS.dwData=1;IntPtrlParam=AllocHGlobal(oCDS.cbData);Marshal.StructureToPtr(oCDS,lParam,false);SendMessageW(hWndDest,WM_COPYDATA,IntPtr.Zero,lParam);LocalFree(oCDS.lpData);FreeHGlobal(lParam);}}接下来张贴出有调用代码,构建自发性自收,如果要发给别的进程,只必须把SendMessage的第一个参数改回目标窗体的名称才可(当然目标窗体也必需提到了Cls_Message构建缴信息处理):viewplaincopytoclipboardprint?Cls_MessageclsMessage;//初始化publicForm1(){InitializeComponent();}privatevoidForm1_Load(objectsender,EventArgse){clsMessage=newCls_Message(this.Handle);//使本窗体需要接管WindowMessage}privatevoidbutton1_Click(objectsender,EventArgse){Cls_Message.。


本文关键词:yobo,体育,app,下载,官网,在,Wince,实现,进程,间,yobo体育app下载官网

本文来源:yobo体育app下载官网-www.jhjdhm.com

联系方式

电话:0249-504637967

传真:053-717550748

邮箱:admin@jhjdhm.com

地址:天津市天津市天津区赛仁大楼6236号