How to Send Information (String, Image, Record) Between Two Applications
http://delphi.about.com/od/windowsshellapi/a/wm_copydata.htm
here are many situation when you need to allow for two applications to communicate. If you do not want to mess with TCP and sockets communication (because both applications are running on the same machine), you can *simply* send (and properly receive) a special Windows message: WM_COPYDATA.
Since handling Windows messages in Delphi is simple, issuing a SendMessage API call along with the WM_CopyData filled with the data to be sent is quite straight forward.
WM_CopyData and TCopyDataStruct
The WM_COPYDATA message enables you to send data from one application to another. The receiving application receives the data in a TCopyDataStruct record .
The TCopyDataStruct is defined in the Windows.pas unit and wraps the COPYDATASTRUCT structure that contains the data to be passed.
Here's the declaration and the description of the TCopyDataStruct record:
type
TCopyDataStruct = packed record
dwData : DWORD;
// up to bits of data to be passed to the receiving application
cbData : DWORD;
// the size, in bytes, of the data pointed to by the lpData member
lpData : Pointer;
// Points to data to be passed to the receiving application. This member can be nil.
end;
Send a String over WM_CopyData
For a "Sender" application to send data to "Receiver" the CopyDataStruct must be filled and passed using the SendMessage function. Here's how to send a string value over WM_CopyData:
procedure TSenderMainForm.SendString( );
var
stringToSend : string;
copyDataStruct : TCopyDataStruct;
begin
stringToSend := 'About Delphi Programming';
copyDataStruct.dwData := ; // use it to identify the message contents
copyDataStruct.cbData := + Length( stringToSend );
copyDataStruct.lpData := PChar( stringToSend );
SendData( copyDataStruct );
end;
The SendData custom function locates the receiver using the FindWindow API call:
procedure TSenderMainForm.SendData( const copyDataStruct : TCopyDataStruct );
var
receiverHandle : THandle;
res : integer;
begin
receiverHandle := FindWindow( PChar( 'TReceiverMainForm' ),
PChar( 'ReceiverMainForm' ) );
if receiverHandle = then
begin
ShowMessage( 'CopyData Receiver NOT found!' );
Exit;
end;
res := SendMessage( receiverHandle, WM_COPYDATA, integer( Handle ),
integer( @copyDataStruct ) );
end;
In the code above, the "Receiver" application was found using the FindWindow API call by passing the class name of the main form ("TReceiverMainForm") and the caption of the window ("ReceiverMainForm").
Note: The SendMessage returns an integer value assigned by the code that handled the WM_CopyData message.
Handling WM_CopyData - Receiving a String
The "Receiver" application handles the WM_CopyData mesage as in:
type
TReceiverMainForm = class( TForm )
private
procedure WMCopyData( var Msg : TWMCopyData ); message WM_COPYDATA;
end; type
// The TWMCopyData record is declared as:
TWMCopyData = packed record
Msg : Cardinal;
From : HWND; // Handle of the Window that passed the data
CopyDataStruct : PCopyDataStruct; // data passed
Result : Longint; // Use it to send a value back to the "Sender"
end; implementation procedure TReceiverMainForm.WMCopyData( var Msg : TWMCopyData );
var
s : string;
begin
s := PChar( Msg.CopyDataStruct.lpData );
// Send something back msg.Result := ;
end;
Sending String, Custom Record or an Image?
The accompanying source code demonstrates how to send a string, record (complex data type) and even graphics (bitmap) to another application.
If you cannot wait the download, here's how to send a TBitmap graphics:
procedure TSenderMainForm.SendImage( );
var
ms : TMemoryStream;
bmp : TBitmap;
copyDataStruct : TCopyDataStruct;
begin
ms := TMemoryStream.Create;
try
bmp := self.GetFormImage;
try
bmp.SaveToStream( ms );
finally
bmp.Free;
end;
copyDataStruct.dwData := Integer( cdtImage ); // identify the data
copyDataStruct.cbData := ms.Size;
copyDataStruct.lpData := ms.Memory;
SendData( copyDataStruct );
finally
ms.Free;
end;
end; // And how to receive it:
procedure TReceiverMainForm.HandleCopyDataImage( copyDataStruct : PCopyDataStruct );
var
ms : TMemoryStream;
begin
ms := TMemoryStream.Create;
try
ms.Write( copyDataStruct.lpData^, copyDataStruct.cbData );
ms.Position := ;
receivedImage.Picture.Bitmap.LoadFromStream( ms );
finally
ms.Free;
end;
end;
Downloadwm_copydata source code example.
How to Send Information (String, Image, Record) Between Two Applications的更多相关文章
- Java14版本特性【一文了解】
「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...
- kafka客户端发布record(消息)
kafka客户端发布record(消息)到kafka集群. 新的生产者是线程安全的,在线程之间共享单个生产者实例,通常单例比多个实例要快. 一个简单的例子,使用producer发送一个有序的key/v ...
- Method and apparatus for encoding data to be self-describing by storing tag records describing said data terminated by a self-referential record
A computer-implemented method and apparatus in a computer system of processing data generated by a f ...
- 记录类型中String的释放
String能自动释放,在进行内存拷贝时需要进行手动释放.可以直接调用Finalize手工释放 如:TGraphicHideTab 记录中声明的Caption:string TGraphicHideT ...
- [LeetCode] Masking Personal Information 给个人信息打码
We are given a personal information string S, which may represent either an email address or a phone ...
- [Swift]LeetCode831. 隐藏个人信息 | Masking Personal Information
We are given a personal information string S, which may represent either an email address or a phone ...
- Masking Personal Information
Masking Personal Information We are given a personal information string S, which may represent eithe ...
- 【LeetCode】831. Masking Personal Information 解题报告(Python)
[LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...
- Vertica 导出数据测试用例
需求:构建简单的测试用例,完成演示Vertica导出数据的功能. 测试用例:导出test业务用户t_jingyu表中的数据. 一.初始化测试环境 二.导出数据 2.1 vsql命令说明帮助 2.2 导 ...
随机推荐
- linux 开机自动启动脚本方法
通过现场对这次天津iptv demo项目的调测.对iptv这套系统有了更好的认识和理解.由于iptv本身需要安装许多服务.而现场实施中有没有把这些需要启动服务的脚本加入到开 机自动运行中.如果服务器重 ...
- 嵌入式 使用mp4v2将H264+AAC合成mp4文件
录制程序要添加新功能:录制CMMB电视节目,我们的板卡发送出来的是RTP流(H264视频和AAC音频),录制程序要做的工作是: (1)接收并解析RTP包,分离出H264和AAC数据流: (2)将H26 ...
- UML系列图------用例图介绍
UML-Unified Model Language 统一建模语言,又称标准建模语言.是用来对软件密集系统进行可视化建模的一种语言. 在UML系统开发中有三个主要的模型: 功能模型: 从用户的角度展示 ...
- Linux 文件的几种类型
文件的几种类型: 1.普通文件 普通文件就是一般意义上的文件,它们作为数据存储在系统磁盘中,可以随机访问文件的内容.Linux系统中的文件是面向字节的,文 件的内容以字节为单位进行存储与访问 ...
- VC远控(三)磁盘显示
服务端: 发送与接收命令 DWORD WINAPI SLisen(LPVOID lparam) { SOCKET client = (SOCKET)lparam; COMMAND command; w ...
- awk与sed简明教程
看到大牛写的关于awk和sed的简明教程,写得很好,为了尊重作者,就不全文转载了,这里标记下链接,方便以后查阅. awk简明教程:http://coolshell.cn/articles/9070.h ...
- 关于DIV+CSS和XHTML+CSS的理解
WEB标准是一系列标准的集合,并不是仅“DIV+CSS”布局就可以实现.以CSS网页布局只是标准的基础之一.“DIV+CSS”布局只是一种通俗的称呼罢了.而我们学习的目标在于以XHTML建立良好的语义 ...
- Python【基础第四篇】
一.迭代器(iterator) 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直到所有的元素被访问完结束.迭代器只能往前不会后退,不过这也没什么,因为人们很少在迭代途中往后退. ...
- 记录一下JS正则的坑
JS正则的单行模式有点问题 总之 . 符号匹配换行符号会有问题 暂时的解决方案是 html = html.replace(/[\r\n]/g,""); 附加一个 html = ht ...
- Yarn中的几种状态机
1 概述 为了增大并发性,Yarn采用事件驱动的并发模型,将各种处理逻辑抽象成事件和调度器,将事件的处理过程用状态机表示.什么是状态机? 如果一个对象,其构成为若干个状态,以及触发这些状态发生相互转移 ...