开发者

Problems with IPC SendMessage

开发者 https://www.devze.com 2023-01-18 19:37 出处:网络
Hi I\'m having problems with my 开发者_开发技巧SendMessage. It seams like the message never reaches my form (I\'m using Winspector Spy to see which messages that are registered in my form), which is

Hi I'm having problems with my 开发者_开发技巧SendMessage.

It seams like the message never reaches my form (I'm using Winspector Spy to see which messages that are registered in my form), which is strange because I'm shure I'm sending to the correct Handle for the current form.

The SendMessage is within a dll but that should not have any thing to do with it.

//sStr is just an input where i type in the Handler address;  
SendMessage(Integer(sStr),WM_COPYDATA, Integer(Handle),Integer(@copyDataStruct));

SendMessage returns 0 every time.

On the receiving end:

procedure WMCopyData(var Msg: TWMCopyData); message WM_CopyData;

procedure TMainForm.WMCopyData(var Msg: TWMCopyData);
var
 s : string;
begin
    s := PChar(Msg.CopyDataStruct.lpData);
    showmessage(s);
 //Send something back
    msg.Result := 2006;
end;

I have also tried other messages like WM_CLOSE. Do any one know why this fails? I'm using Delphi 5.


The problem is that you cannot write

Integer(sStr)

to convert a string representing an integer (e.g. '12345') to an integer (12345).

Use

StrToInt(sStr)

instead.

Indeed, technically, a string is only a pointer to the string header + data. I guess that Integer(sStr) simply returns that pointer. (Or, actually, simply treats the sStr like an integer).

That is, you should do

SendMessage(StrToInt(sStr), WM_COPYDATA, Handle, cardinal(@copyDataStruct));


The SendMessage definition is

function SendMessage(hWnd: HWND; Msg: UINT; wParam: WPARAM; lParam: LPARAM):LRESULT; stdcall;

Updated

For Msg = wm_copydata:

  • The first argument is a handle to the window receiving the data
  • The third argument is a handle to the window passing the data

If you name the first argument sStr I assume it is not a handle but a string.


I think the problem is that you're trying to use the name or something for your window, and that won't work.

Try this instead:

var
  Wnd: HWnd;
begin
  Wnd := GetForegroundWindow(); // Assumes your target window is up front
  // Fill in CopyData structure here.
  SendMessage(Wnd, WM_COPYDATA, SomeWParamValue, Cardinal(@CopyDataStruct));
end;
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号