master..xp cmdshell 执行EXE
我的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;
interface
uses
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.