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的更多相关文章

  1. Java14版本特性【一文了解】

    「MoreThanJava」 宣扬的是 「学习,不止 CODE」,本系列 Java 基础教程是自己在结合各方面的知识之后,对 Java 基础的一个总回顾,旨在 「帮助新朋友快速高质量的学习」. 当然 ...

  2. kafka客户端发布record(消息)

    kafka客户端发布record(消息)到kafka集群. 新的生产者是线程安全的,在线程之间共享单个生产者实例,通常单例比多个实例要快. 一个简单的例子,使用producer发送一个有序的key/v ...

  3. 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 ...

  4. 记录类型中String的释放

    String能自动释放,在进行内存拷贝时需要进行手动释放.可以直接调用Finalize手工释放 如:TGraphicHideTab 记录中声明的Caption:string TGraphicHideT ...

  5. [LeetCode] Masking Personal Information 给个人信息打码

    We are given a personal information string S, which may represent either an email address or a phone ...

  6. [Swift]LeetCode831. 隐藏个人信息 | Masking Personal Information

    We are given a personal information string S, which may represent either an email address or a phone ...

  7. Masking Personal Information

    Masking Personal Information We are given a personal information string S, which may represent eithe ...

  8. 【LeetCode】831. Masking Personal Information 解题报告(Python)

    [LeetCode]831. Masking Personal Information 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzh ...

  9. Vertica 导出数据测试用例

    需求:构建简单的测试用例,完成演示Vertica导出数据的功能. 测试用例:导出test业务用户t_jingyu表中的数据. 一.初始化测试环境 二.导出数据 2.1 vsql命令说明帮助 2.2 导 ...

随机推荐

  1. Python 字典(Dictionary) get()方法

    描述 Python 字典(Dictionary) get() 函数返回指定键的值,如果值不在字典中返回默认值. 语法 get()方法语法: dict.get(key, default=None) 参数 ...

  2. PHP中超全局变量$GLOBALS和global的区别

    一.超全局变量$GLOBALS PHP超全局变量有很多,如下的都属于超全局变量(Superglobal): $GLOBALS,$_SERVER,$_GET,$_POST,$_FILES,$_COOKI ...

  3. Ubuntu 升级到13.10之后出现Apache2启动失败的问题

    昨天看到Ubuntu 13.04提示有新的发行版Ubuntu 13.10了,手痒了一下,没有忍住就升级了. 结果升级完毕之后发现Apache2服务启动失败了,失败信息是: Invalid comman ...

  4. nginx 负载均衡相关知识

    Nginx ("engine x") 是一个高性能的 HTTP 和 反向代理 服务器,也是一个 IMAP/POP3/SMTP 代理服务器. Nginx 是由 Igor Sysoev ...

  5. 【转】linux mknod命令解析

    转自:http://www.cnblogs.com/cobbliu/archive/2011/07/05/2389014.html 个人觉得linux的软件设计思想异常强大,比如把所有的设备都当做文件 ...

  6. css3 --- 翻页动画 --- javascript --- 3d --- Action

    用css3和javascript做一个翻页动画<Action> 如有疑问请参照我的上一篇随笔:http://www.cnblogs.com/kodoyang/p/Html_Css3_Car ...

  7. PropertyPlaceholderConfigurer加载属性配置文件:

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  8. no symbol version for module_layout

    内核模块编译helloworld: no symbol version for module_layout, 尝试各种解决办法, 都没搞定, 版本也是对的. dmesg提示no symbol vers ...

  9. 解决g++:command not found(centos7.0)

    问题背景,因为装了虚拟机,系统为centos7.0,由于是纯净版,没有gcc,使用命令yum install gcc安装了gcc,但是没安装g++,导致g++:command not found问题. ...

  10. linux c多线程编程范例

    #include <stdio.h> #include <pthread.h> #include <unistd.h> #include <stdlib.h& ...