我要向另一用VC做的应用程序发送消息,再在目标程序里处理此消息,该怎么做?主要是Delphi这边怎么写?

解决方案 »

  1.   

    转载我收集的一段代码,也许对你有帮助
    unit memfileu;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      StdCtrls;type
      TForm1 = class(TForm)
        Button1: TButton;
        Button2: TButton;
        Memo1: TMemo;
        Button3: TButton;
        procedure Button1Click(Sender: TObject);
        procedure Button2Click(Sender: TObject);
        procedure Button3Click(Sender: TObject);
      private
        { Private declarations }
      public
        { Public declarations }
      end;
      RPtValue     =record
    //             x,y:double;
                 Pt_id:integer;
                 pt_value:single;
                 pt_state:integer;
    //             pt_unit:string[10];
      end;var
      Form1: TForm1;implementation{$R *.DFM}//读memfile
    procedure TForm1.Button1Click(Sender: TObject);
    var
      hMap:THandle;
      data,d:^RPtValue;
      i:integer;
    begin
      hMap:=CreateFileMapping(-1,nil,PAGE_READONLY,0,1024,'JZS_SHARE_MEMORY');
      if hMap=NULL then exit;
      data:=Mapviewoffile(hMap,FILE_MAP_READ,0,0,0);
      d:=data;
      for i:=0 to 25 do
      begin
        memo1.Lines.Add(inttostr(d^.Pt_id)+':'+floattostr(d^.pt_value)+':'+inttostr(d^.pt_state));
        inc(d);
      end;
    end;//创建/写memfile
    procedure TForm1.Button2Click(Sender: TObject);
    var
      hFile:THandle;
      hMapping:THandle;
      data,d:^integer;
      i:integer;
    begin
      hMapping:=CreateFileMapping($ffffffff,nil,PAGE_READWRITE ,0,1024,pchar('JZS_SHARE_MEMORY'));
      data:=MapViewOfFile(hMapping,FILE_MAP_WRITE,0,0,0);    d:=data;
      for i:=0 to 25 do
      begin
        d^:=ord('a')+i;
        inc(d);
      end;end;procedure TForm1.Button3Click(Sender: TObject);
    var
      r:  RPtValue;
    begin
      showmessage(inttostr(sizeof(r)));
    end;end.
      

  2.   


        sunjian1213(sunny) :具体怎么写,能给个简单的代码吗?
         ljmanage(过客) :是自定义消息,而且我怎么找到目标程序的主窗口再发送?
      

  3.   

    我怎么找到目标程序的主窗口再发送?
    用findwindow啊,只要知道它的窗口标题就可以了
      

  4.   

    很多种方式啊。对你这个问题,直接采用消息发送WM_CopyData最好。
    SendMessage(hWnd,WM_CopyData,Handle,  pcds)。
      

  5.   

    是啊,先用findWindow函数找到句柄,再用sendMessage函数发送就行了
      

  6.   

    能告诉我你怎么解决的吗?
    能不能看看你的代码?DELPHI和VC中涉及到内存共享的部分?
    谢谢!
      

  7.   


      其实很简单,我把我的程序贴出来以供参考  在Delphi程序中定义一自定义消息
      const WM_INITCOMM=WM_USER+102;
    下面是发送消息:
      var
       handle : HWND;
    begin
        handle:=FindWindow(nil,'目标窗口的标题');
        try
          PostMessage(handle,WM_INITCOMM,0,0);
        except
        end;
    end; 
      VC程序中:
      也自定义一消息,消息ID与上面一样
      头文件中加入
        #define WM_INITCOMM (WM_USER+102)
      映射列表中加入
        afx_msg LRESULT  MyMessage();  CPP文件中加入在//}}AFX_MSG_MAP后END_MESSAGE_MAP()前加入
        ON_MESSAGE(WM_INITCOMM,MyMessage) 加入 LRESULT CMainFrame::MyMessage()
         {
             //消息处理
         }