我的EXE是DELPHI做的,目的是接收SQL触发器传来的字符串,然后通后消息把字符串传给另一个正在运行的程序,
能找到另一个程序的PID(进程ID),但无法找到另一个在运行程序的窗口句柄.因此就无法接收到消息,后来想到一个办法,把另一个运行的程序运行时窗体的句柄放在一个INI文件中,这样就不用找这个窗口的句柄了,直接读INI就可得到,但调用SendMessage(winhand, WM_COPYDATA, 1, Integer(@Data));仍不能正常传递消息.发送消息程序
program send;{$APPTYPE CONSOLE}uses
  Forms,
  windows,
  sysutils,
  Messages,
  WinProcs,
  inifiles,
  all in 'all.pas';var
  Data: tagCOPYDATASTRUCT;
  pBuf: PAnsiChar;
  MYHWND: THandle;
  inifile: TIniFile;
  winhand:integer;
{$R *.res}begin
  Application.Initialize;
  Application.MainFormOnTaskbar := True;
    Application.Run;begin
   if FileExists(Trim(ExtractFilePath(Application.ExeName)+'com.ini')) = True then
    begin
    Try
      inifile := TIniFile.Create(Trim(ExtractFilePath(Application.ExeName))+'com.ini');
      winhand:= strtoint(inifile.ReadString('配置信息','窗口句柄','1'));
    Finally
      inifile.Free;
    end;
   end;
  GetMem(pBuf, Length(ParamStr(1)) + 1);
try    ZeroMemory(pBuf, Length(ParamStr(1)) + 1);
    StrPCopy(pBuf,ParamStr(1));
    Data.cbData:= Length(ParamStr(1));
    Data.dwData:= Length(ParamStr(1));
    Data.lpData:= pBuf;
    writeln(ParamStr(1));
    SendMessage(winhand, WM_COPYDATA, 1, Integer(@Data));
    writeln(winhand);
  finally
    FreeMem(pBuf);
end;
end;  sleep(3000);
end.接收消息程序
unit Unit2;interfaceuses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls,inifiles;
const
 My_MousL=WM_USER+100;
type
  TForm2 = class(TForm)
    Label1: TLabel;
    procedure FormShow(Sender: TObject);
  private
  j:integer;
    { Private declarations }
  public
 procedure Mymessage(var t:TMessage);message WM_COPYDATA;    { Public declarations }
  end;var
  Form2: TForm2;implementation{$R *.dfm}procedure TForm2.FormShow(Sender: TObject);
var inifile:tinifile;
begin
 if FileExists(Trim(ExtractFilePath(Application.ExeName)+'com.ini')) = True then
  begin
    Try
      inifile := TIniFile.Create(Trim(ExtractFilePath(Application.ExeName))+'com.ini');
       inifile.WriteString('配置信息','窗口句柄',inttostr(findwindow(nil,'form2')));
    Finally
      inifile.Free;
    end;
  end
end;procedure Tform2.Mymessage(var t: TMessage);
var
  Data  : ^tagCOPYDATASTRUCT;
  strMSG: string;
  mylen:integer;
begin
  Data:= Pointer(t.LParam);
   mylen:=integer(data.cbData);
  strMSG:= copy(PAnsichar(Data.lpData),1,mylen); label1.Caption:=strMsg;end;
end.

解决方案 »

  1.   

    SOCKET会占资源吧,不停的等待。
      

  2.   

    这个得看你运行是否在同一个机器上,同一机器用内存映射,不同的机子用socket通信 
      

  3.   

    仍然不行,单独运行就可,但用SQL 的XP_CMDSHELL调用,另一应用程序就不能共享数据.见代码
    APP1修改数据,APP2接收数据
    APP1代码
    program send;{$APPTYPE CONSOLE}uses
      sysutils,forms;
      {$I DLLDATA.INC}
    var
     GlobalData: PGlobalDLLData;
     procedure GetDLLData(var AGlobalData: PGlobalDLLData); StdCall External 'SHARELIB.DLL';
    {$R *.res}begin
      Application.Initialize;
      GetDLLData(GlobalData);
      GlobalData^.S :=ParamStr(1);
      GlobalData^.I:=12345;
      Application.Run;
      sleep(10000);
      application.Terminate;end.
    APP2代码
    unit MainFrmA2;interfaceuses
      Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
      ExtCtrls, StdCtrls;{$I DLLDATA.INC}type  TMainForm = class(TForm)
        tmTimer: TTimer;
        lblconid: TLabel;
        Memo1: TMemo;
        procedure tmTimerTimer(Sender: TObject);
      public
        GlobalData: PGlobalDLLData;
      end;{ Define the DLL's exported procedure }
    procedure GetDLLData(var AGlobalData: PGlobalDLLData); StdCall External 'SHARELIB.DLL';var
      MainForm: TMainForm;implementation{$R *.DFM}procedure TMainForm.tmTimerTimer(Sender: TObject);
    var sendstr:string;
        conid:string;
    begin
      GetDllData(GlobalData);  // Get access to the data
      { Show the contents of GlobalData's fields.}
      sendstr := GlobalData^.S;
      conid := IntToStr(GlobalData^.I);
      if conid<>lblconid.Caption  then
      begin
      memo1.Text:='已发送短信';
      lblconid.Caption:=conid;
      end;
    end;end.