Fastest way to IPC from a DLL to an EXE

c++ / delphi package - dll injection and api hooking
iconic
Site Admin
Posts: 1065
Joined: Wed Jun 08, 2005 5:08 am

Re: inter-process callback possible?

Post by iconic »

That would be a blunder on Microsoft. Every APC's dream :wink:

--Iconic
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

Re: inter-process callback possible?

Post 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.
iconic
Site Admin
Posts: 1065
Joined: Wed Jun 08, 2005 5:08 am

Re: inter-process callback possible?

Post 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
abalonge
Posts: 11
Joined: Tue Jun 26, 2012 10:14 pm

Re: inter-process callback possible?

Post by abalonge »

@iconic, You are correct as far as my rewording is concerned..and I have updated the title :) Your thoughts, nevertheless were interesting.
Last edited by abalonge on Wed Mar 31, 2021 4:11 pm, edited 1 time in total.
abalonge
Posts: 11
Joined: Tue Jun 26, 2012 10:14 pm

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

Post 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]));
  
Last edited by abalonge on Wed Mar 31, 2021 4:08 pm, edited 1 time in total.
madshi
Site Admin
Posts: 10753
Joined: Sun Mar 21, 2004 5:25 pm

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

Post 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.
abalonge
Posts: 11
Joined: Tue Jun 26, 2012 10:14 pm

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

Post by abalonge »

Yes, both are in the same thread. I will test in different ways, that makes sense.
abalonge
Posts: 11
Joined: Tue Jun 26, 2012 10:14 pm

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

Post by abalonge »

Thanks for all your help, madshi & iconic
iconic
Site Admin
Posts: 1065
Joined: Wed Jun 08, 2005 5:08 am

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

Post by iconic »

No problem, good luck :D

--Iconic
Post Reply