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 导 ...
随机推荐
- 【转】ios开发之AppDelegate
创建应用程序之后之后,默认有AppDelegate.h文件与AppDelegate.m文件. AppDelegate为何物? AppDelegate为整个应用的一个代理,提供程序启动.退出等类似 ...
- svn 安装与设置
Subversion可以通过网络访问它的版本库,从而使用户可以在不同的电脑上使用.一定程度上可以说,允许用户在各自的地方修改同一份数据是促进协作. 运行Subversion服务器需要首先要建立一个版本 ...
- Excel2007条件格式怎么用
Excel2007的条件格式功能十分的强大实用,较2003版改进十分的大,下面我们以经验记录为例做一简单的操作示范.注意前部分有二点技巧可借鉴,即不规则选取和不规则统一填充. 工具/原料 EXCEL2 ...
- Apache加载PHP.ini顺序
网上找到一份关于Apache加载PHP.ini顺序的文档: (1) apache的httpd.conf中的PhpIniDir: (2) 注册表键值:HKEY_LOCAL_MACHINE->SOF ...
- Sciter使用心得
1. div双击事件 $(div).onMouse = function(evt) { switch(evt.type) { case Event.MOUSE_DCLI ...
- 使用源码编译wxpython-基于python2.7
1.前言 本文主要讲述在linux环境下进行编译wxpython,在windows下面安装wxpython很简单,只要下载,然后直接执行exe文件,下一步下一步即可安装,在linux下面,则具有很多步 ...
- Chapter9:顺序容器
现代C++程序应该使用标准库容器,而不是更原始的数据结构,例如内置数组. 新标准库容器的性能几乎肯定与最精心优化过的同类数据结构一样好. 当我们用一个对象来初始化容器时,或将一个对象插入到容器中时,实 ...
- 安卓动画之ObjectAnimator
ObjectAnimator 不仅仅移动位置,还移动了对象view 先来代码片段: //Y轴变换 ObjectAnimator oa = ObjectAnimator.ofFloat(imageVie ...
- java 抽象类和接口
接口和内部类为我们提供了一种将接口与实现分离的更加结构化的方法. 抽象类与接口是java语言中对抽象概念进行定义的两种机制,正是由于他们的存在才赋予java强大的面向对象的能力.他们两者之间对抽象概念 ...
- c语言的几个重要知识点
内存结构 这是核心中的核心,请仔细看完,充分理解,否则请不要看下一节内容. 每个程序一启动都有一个大小为4GB的内存,这个内存叫虚拟内存,是概念上的,真正能用到的,只是很小一部分,一般也就是在几百 ...