我有两个应用程序,它们与消息通信,如果我运行两个编译的exe,一切都按预期工作.但是当我从delphi ide运行(调试)发送器(bds2006,没有运气时尝试使用delphi 7)时,sendmessage不会发送任何内容.
似乎ide会阻止向其他应用程序发送消息.
我在win7 64bit和borland 2006上使用WM_COPYDATA.
任何的想法?
寄件人:
procedure TForm1.Button1Click(Sender: TObject); var dst: THandle; stringToSend : string; copyDataStruct : TCopyDataStruct; begin stringToSend := 'Hello'; copyDataStruct.dwData := 0; //use it to identify the message contents copyDataStruct.cbData := 1 + Length(stringToSend) ; copyDataStruct.lpData := PChar(stringToSend) ; SendData(copyDataStruct) ; end; procedure TForm1.SendData(const copyDataStruct: TCopyDataStruct) ; var receiverHandle : THandle; res : integer; begin receiverHandle := findwindow( pchar('TForm2'),pchar('Form2') ); if receiverHandle = 0 then begin ShowMessage('CopyData Receiver NOT found!') ; Exit; end; res := SendMessage(receiverHandle,WM_COPYDATA,Integer(Handle),Integer(@copyDataStruct)) ; end; end.
接收方部分:
TForm2 = class(TForm) private procedure WMCopyData(var Msg: TWMCopyData ); message WM_COPYDATA; public end; var Form2: TForm2; implementation {$R *.dfm} { TReceiver } procedure TForm2.WMCopyData( var Msg: TWMCopyData ); begin ShowMessage( 'Received' ); end;
解决方法
这是一个疯狂的猜测.您正在运行以管理员身份接收邮件的应用程序.在Vista及更高版本中,完整性级别保护可以阻止向具有更高完整性级别的进程传递消
从SendMessage
文档:
Message sending is subject to UIPI (User Interface Privilege Isolation). The thread of a process can send messages only to message queues of threads in processes of lesser or equal integrity level.