如:Pro1中的Edit1.Text,Edit2.Text,ComboBox1.Text如何傳遞到Pro2中對應的Edit1,Edit2,ComboBox1
我試過TDATASTRUCT的COPYDATA,但是只會傳遞一個參數應該是用到內存映射文件,我不會用,請各位大俠幫忙,謝謝!

解决方案 »

  1.   

    两个单独的程序传输数据,我看只能socket了.內存当然可以,但不是每个程序员都能很好的驾驭它,起码我不能.
      

  2.   

    CreateFileMapping
    OpenFileMapping
    的API网上N多例子的:)http://lysoft.7u7.net
      

  3.   

    内存映射吧,虽然不好懂,但网上例子挺多的,而且也可以多了解windows底层的东西,不能老用简单的吧。
      

  4.   

    //下面是一个内存映射文件使用的例子(先是发送单元)
    unit Unit1;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;const
      WM_DATA = WM_USER + 1024;
    type
      PShareMem = ^TShareMem;
      TShareMem = record
        Data: array[0..255] of char;
      end;
      TForm1 = class(TForm)
        Button1: TButton;
        procedure Button1Click(Sender: TObject);
        procedure FormDestroy(Sender: TObject);
        procedure FormCreate(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }  end;var
      Form1: TForm1;
      PShare: PShareMem;
    implementation{$R *.DFM}
    var
      HMapping: THandle;
      HMapMutex: THandle;
    const
      MAPFILESIZE = 1000;
      REQUEST_TIMEOUT = 1000;procedure OpenMap;
    begin
       {创建一个文件映射内核对象}
      HMapping := CreateFileMapping(
        $FFFFFFFF,
        nil,
        PAGE_READWRITE,
        0,
        SizeOf(TShareMem),
                     {这个文件映射对象的名字用于与其他进程共享该对象,}
       pchar('Map Name')
        );
      if (hMapping = 0) then
      begin
        ShowMessage('不能创建内存映射文件');
        Application.Terminate;
        exit;
      end;
       {将文件数据映射到进程的地址空间}
       {当创建了一个文件映射对象之后,仍然必须让系统为文件的数据保留
        一个地址空间区域,并将文件的数据作为映射到该区域的物理存储器进行提交。
        }
      PShare := PShareMem(MapViewOfFile(HMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0));
      if PShare = nil then
      begin
        CloseHandle(HMapping);
        ShowMessage('Can''t View Memory Map');
        Application.Terminate;
        exit;
      end;  {既然我们通过pvFile得到了映象视图的起始地址,那么可以对视图做一些操作了}
    end;procedure CloseMap;
    begin
      if PShare <> nil then
        {从进程的地址空间中撤销文件数据的映象}
        UnMapViewOfFile(PShare);
      if HMapping <> 0 then
        CloseHandle(HMapping);
    end;function LockMap: Boolean;
    begin
      Result := true;
       {创建互斥对象}
      HMapMutex := CreateMutex(nil, false,
        pchar('MY MUTEX NAME GOES HERE'));
      if HMapMutex = 0 then
      begin
        ShowMessage('不能创建互斥对象');
        Result := false;
      end else begin
        if WaitForSingleObject(HMapMutex, REQUEST_TIMEOUT)
          = WAIT_FAILED then
        begin
          ShowMessage('不能对互斥对象加锁!');
          Result := false;
        end
      end
    end;procedure UnlockMap;
    begin
       {关闭文件映射对象和文件对象}
      ReleaseMutex(HMapMutex);
      CloseHandle(HMapMutex);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      str: pchar;
    begin
      str := pchar('简单的共享内存的例子');
      CopyMemory(@(pShare^.data), Str, Length(str));
      {发送消息表明有数据}
      PostMessage(FindWindow(nil, 'MyForm'), WM_DATA, 1, 1)
    end;procedure TForm1.FormDestroy(Sender: TObject);
    begin
      UnlockMap;
      CloseMap;
    end;procedure TForm1.FormCreate(Sender: TObject);
    begin
      OpenMap;
      LockMap;
    end;end.
      

  5.   

    //(以下是接收单元)
    unit Unit2;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;const
      WM_DATA = WM_USER + 1024;
    type
      PShareMem = ^TShareMem;
      TShareMem = record
        Data: array[0..255] of char;
      end;
      TMyForm = class(TForm)
        Memo1: TMemo;
        Button1: TButton;
        procedure FormCreate(Sender: TObject);
        procedure Button1Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
        procedure getShareInfo(var Msg: TMessage); message WM_DATA; {处理WM_DATA}
      end;var
      MyForm: TMyForm;
      PShare: PShareMem;
      MapHandle: THandle;implementation{$R *.DFM}procedure TMyForm.getShareInfo(var Msg: TMessage); {处理WM_DATA}
    begin
      if msg.LParam=1 then {是我们设定的消息参数}
        Memo1.Text := PShare^.Data;
    end;procedure TMyForm.FormCreate(Sender: TObject);
    begin
      MapHandle := OpenFileMapping(FILE_MAP_WRITE, {获取完全访问映射文件}
        False, {不可继承的}
        pchar('Map Name')); {映射文件名字}
      if MapHandle = 0 then
      begin
        ShowMessage('不能定位内存映射文件块!');
        Halt;
      end;
      PShare := PShareMem(MapViewOfFile(MapHandle, FILE_MAP_ALL_ACCESS, 0, 0, 0));
      if PShare = nil then
      begin
        CloseHandle(MapHandle);
        ShowMessage('Can''t View Memory Map');
        Application.Terminate;
        exit;
      end;
      FillChar(PShare^, SizeOf(TShareMem), 0);
    end;procedure TMyForm.Button1Click(Sender: TObject);
    begin
      CloseHandle(MapHandle);
      close;
    end;end.
      

  6.   

    发送单元窗体就一个Button(Name:="button1";caption:="发送数据");
    接收单元就一个memo(Name:memo1)和一个button(Name:="button1";caption:="关闭");
    呵呵,够清晰、够明了了吧!!!剩下的就自己照着例子去改吧!!
      

  7.   

    内存映射或者发送一个消息wm_copydata,但是要对所发送的数据进行tcopydatastruct(好像是,记不太清楚了)类型的定义
      

  8.   

    我觉得还是用 sendmessage来传递消息好一些,用wm_copydata这个东西蛮好的。