Page 2 of 2

Re: inter-process callback possible?

Posted: Wed Mar 31, 2021 9:03 am
by iconic
That would be a blunder on Microsoft. Every APC's dream :wink:

--Iconic

Re: inter-process callback possible?

Posted: Wed Mar 31, 2021 9:07 am
by madshi
Hehe, yes! I was a bit scared because I know that SendMessage() internally handles messages. So calling SendMessage() inside of a hook callback function can be a bad idea.

Re: inter-process callback possible?

Posted: Wed Mar 31, 2021 9:11 am
by iconic
No worries :D Only certain (very few) APIs can interrupt a thread, causing an alertable state, where pending APCs enqueued are executed.

--Iconic

Re: inter-process callback possible?

Posted: Wed Mar 31, 2021 2:52 pm
by abalonge
@iconic, You are correct as far as my rewording is concerned..and I have updated the title :) Your thoughts, nevertheless were interesting.

Re: Fastest way to IPC from a DLL to an EXE

Posted: Wed Mar 31, 2021 3:44 pm
by abalonge
I will not using this in a service or a thread.

I have added PostMessage to my testing application and it came in slower than SendMessage.

Results:
Calling PostMessage X 5000000
9.199 seconds for PostMessage

Calling SendMessage X 5000000
1.594 seconds for SendMessage

Calling callback X 5000000
0.250 seconds for Callback

This is between my DLL and EXE, the DLL is notifing the EXE by using many calls to the EXE's main form handle via PostMessage, SendMessage... I also did a Callback for comparison. Of course this test is not inter-process, but now you can see why I wanted to use a Callback in IPC :)
Maybe my test is not accounting for async vs sync (PostMessage vs SendMessage) calling methods?
Here is the meat of how I tested:

In the DLL:

Code: Select all

procedure Send_PostMessage(mode, opt: integer); stdcall;
begin
  PostMessage(CB_Handle, WM_USER + 10, mode, opt);
end;
In the EXE:

Code: Select all

  procedure SetCallBackFunc(myCBFunction: TCallBackFunction); stdcall; external 'messageDLL.dll';
  procedure TestCallBack(myCBFunction: TCallBackFunction); stdcall; external 'messageDLL.dll';
  procedure Send_PostMessage(mode, opt: integer); stdcall; external 'messageDLL.dll';
  procedure Send_SendMessage(mode, opt: integer); stdcall; external 'messageDLL.dll';
	...
  // for each I am doing...	
  SW := TStopwatch.StartNew;
  for ci := 1 to NumMessages do begin
    case which of
    1: Send_PostMessage(1, 1);
    2: Send_SendMessage(1, 1);
    3: TestCallBack(CallMeTest);
    end;
  end;
  SW.Stop();
  Display(Format('%.3f seconds for '+ss, [SW.Elapsed.TotalSeconds]));
  

Re: Fastest way to IPC from a DLL to an EXE

Posted: Wed Mar 31, 2021 4:08 pm
by madshi
I'm not sure why SendMessage is faster for you, that seems weird to me. However, I think you're testing all this within the same thread? It might make sense to use a separate thread to do the Post/SendMessage calls. That should be a better simulation about how this would perform across process boundaries than running both message sending and message receiving in the same thread.

Callbacks are ultra fast within the same process. But simple callbacks like that don't work across process boundaries.

In any case, of course you can measure SendMessage vs PostMessage across process boundaries and measure which is faster. I would assume PostMessage should be faster. Speed can be measured in different ways, though:

1) How long does the Post/SendMessage call take in the other process?
2) How high is the average delay between sending the message and receiving it in your Delphi process.

It's quite possible that some methods are better for 1) and others for 2). So in order to get a full picture, you should probably measure both.

Re: Fastest way to IPC from a DLL to an EXE

Posted: Wed Mar 31, 2021 4:23 pm
by abalonge
Yes, both are in the same thread. I will test in different ways, that makes sense.

Re: Fastest way to IPC from a DLL to an EXE

Posted: Wed Mar 31, 2021 6:47 pm
by abalonge
Thanks for all your help, madshi & iconic

Re: Fastest way to IPC from a DLL to an EXE

Posted: Thu Apr 01, 2021 1:42 am
by iconic
No problem, good luck :D

--Iconic