views:

90

answers:

3

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.

+1  A: 

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.

Serg
Wrong how ? my fist parm sStr is the handle to the receiving window, WM_CopyData is the message, Handle is the handle to the dll and @copyDataStruct is the data thats to be sent. I dont se the fault here.
Roise
@Rosie: What data type is `sStr`?
Andreas Rejbrand
sStr is a string. Which i manually type in the Handel for the targeting form.
Roise
@Roise: If sStr is a string, it isn't a Window handle (HWND). HWND's are numeric (LONGWORD). String <> LONGWORD in any way, shape, or form.
Ken White
+1  A: 

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));
Andreas Rejbrand
Did not help, but thanks for the suggestion
Roise
Well, it is more than a suggestion. It will *definitely* not work if you write `Integer(sStr)`. When I did a test, `Integer('12345') = 4605000`.
Andreas Rejbrand
Yes well im using it, sorry if i sounded sharp or did not take in what you said, im just saying that it did not solve my problem, its been a long day of programming :)
Roise
This morning it suddenly worked out of the blue, dont what was wrong but im shure this helped some, did not to any changes. So ill set this as the answer :)
Roise
A: 

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;
Ken White