中国程序员联盟 正在重新改版中ing 不便之处还请见谅 改版后将内容涉及java delphi .net php
 
  首页 | 数据库开发 | 网络通讯 | 多线程 | 多媒体开发 | 图像处理 | 程序人生 | 系统函数 | 控件开发 | Web服务
 
  当前位置:笨鱼delphi技术网>系统函数>文章内容

delphi using file mapping objects ( Shared Memory )

来源:国外 关于:轶名 发布时间:2007-07-03   [收藏] [推荐]
Question/Problem/Abstract:
How to interprocess between two applicationsin or more Delphi written applications?
Answer:

There are some situations in programming with the new software design programming languages that need communication between two or more processes (or instances of some executables).this technique is named Inter Process Communication (IPC) which is discussed in the operating system books.

There are some ways to do this. One way is the communication through sockets. This way is the main approach for programmers in Linux and one of the ways in Windows. Another way in Windows is using the messages (using PostMessage and SendMessage API functions). But the preferred way in Windows is Shared Memory which is mainly discussed in this article. I've tried all the three ways and the conclusion was the best performance for the Shared Memory model.
The idea is creating a named file-mapping object in both applications, of course with the same name. then with the SetSharedMem and GetSharedMem  procedures the applications can write and read the datafrom file mapping object ( Shared Memory).
Here I describe this process:

procedure InitSharedMemory;
var
  h:hwnd;
begin
  h := CreateFileMapping($FFFFFFFF,         // use paging file
                         0,                 // no security attributes
                         PAGE_READWRITE,    // read/write access
                         0,                 // size: high 32-bits
                         100,               // size: low 32-bits
                         'dllmemfilemap');  // name of map object
  if h=0 then
    exit;
  lpvMem := MapViewOfFile( h,              // object to map view of
                           FILE_MAP_WRITE, // read/write access
                           0,              // high offset:  map from
                           0,              // low offset:   beginning
                           0);             // default: map entire file
  if lpvMem=nil then
    exit;
end;
This procedure must be called before any accessto the shared memory. The CreateFileMapping function creates the named file-mapping object and returns the handle of the object. The first parameter is the handle of a file to be used for the file mapping. It can be $FFFFFFFF  to specify the operating system page file. The second parameter – security attribute  – should be 0. The third parameter should be PAGE_READWRITE. the two next parameters are the maximum size of the buffer. The first be 0 and the second be 100 for the size of 100 byte ( 0 * MAXINT + 100 ).
procedure SetSharedMem(lpszBuf:LPTSTR);
var
    lpszTmp : LPTSTR;
    i:integer;
begin
  lpszTmp := LPTSTR( lpvMem);
  i:=0;
  while (lpszBuf[i]<>chr(0)) do
    begin
     lpszTmp[i] := lpszBuf[i];
     inc(i);
   end;
  lpszTmp[i] := chr(0);
end;

This procedure is responsible for sending data to the shared memory. it takes a pointer to the data and copies it in the shared area. This area can be accessed by the other applications.
procedure GetSharedMem( lpszBuf:LPTSTR;  cchSize:DWORD);
var
   lpszTmp:LPTSTR;
   i:integer;
begin
  lpszTmp := LPTSTR( lpvMem);
  i:=0;
  dec(cchSize);
  while (lpszTmp[i]<>chr(0)) and(cchSize<>0) do
    begin
      lpszBuf[i] := lpszTmp[i];
      inc(i);
    end;
  lpszBuf[i] := chr(0);
end;

This procedure copies the data from shared memory to the variable in the first parameter with the maximum size of the second parameter.
A simple example project is attached that illustrates using shared memory in Borland Delphi. It should be executed twice and they can share data with each other.
 

[浏览: 次]   
上一篇:delphi Delphi_Delphi面向对象编程的20条规则   下一篇:delphi Smart threads with a central management
[收藏] [推荐] [返回顶部] [打印本页] [关闭窗口]  
    评论加载中…
google adsense热点文章
·delphi Delphi中ShellExecute的妙用
·delphi 如何快速读取文本文件
·delphi 如何判断输入值是否中文
·delphi 在应用层截获键盘消息
·delphi delphi实现服务开启与关闭
·delphi 实时记录事件日志
·delphi 使MEMO自动滚动
·delphi 如何区分键盘两个Enter键
·delphi 切换界面的方法
·delphi 汉字输入法的编程及使用
·delphi Delphi程序输入法自动切换最简
·delphi 程序缩小为任务右下角的小图标
     delphi技术网 | firefox 下载 | Avant Browser下载 | dedecms 技术网 | drupal 爱好者 | php 技术网
  Copyright@www.delphichm.com,2006-2009.All Rights Reserved.
 
程序员联盟 | delphi Java .net|QQ:707102932