4

We are trying to develop an application that send messages to a service, but we only have send message between two applications. We are developing in Delphi 7, Windows XP.

We need the service send back a message to the application, depending on the result of the action that the application sent to do to the service.

Well, some people said that it's impossible to send a message from an application to a service. So, we decided to make two services, and now we have to send messages from one service to another one. Now, the point is: how to send a message from a service to another one?

Johan
  • 74,508
  • 24
  • 191
  • 319
Howard
  • 43
  • 1
  • 5

3 Answers3

13

You cannot send window messages to a service. For one, services do not have a UI of their own, let alone windows. And second, even if you add your own hidden window to the service (actually, Delphi-based services do have the hidden TApplication window), window messages are not allowed to pass over session boundaries (services run in a separate session than user-mode applications).

There are many IPC (interprocess communications) mechanisms that work with services - named pipes, mailslots, TCP/IP and UDP sockets, just to name a few. You will have to use one of those instead.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Sorry, but it's false. You can send window messages to a service. Need only to set the appropriate parameter at the service creation. Then you create you own message handler. And locally, a GDI message with WM_COPYDATA is faster than any other IPC mechanism (at least if your message is some KB). To send the message back to the application, you simply use a PostMessage or SendMessage call to the client HWND instance (which has been sent with the first message). See http://synopse.info/fossil/finfo?name=SQLite3/SQLite3Commons.pas and TSQLRestClientURIMessage + TSQLRestServer.ExportServerMessage – Arnaud Bouchez Feb 20 '11 at 09:35
  • 1
    @A.Bouchez: what creation parameter are you referring to exactly? `SERVICE_INTERACTIVE_PROCESS`? That flag is no longer supported starting with Vista. Window messages CANNOT pass across session boundaries (in any version of Windows). The ONLY way a service can exchange window messages with an external app is if that app is running in the same session as the service. Services DO NOT run in the same session as most logged in users in XP and earlier, and NO users in Vista and later. – Remy Lebeau Feb 22 '11 at 08:31
7

I'd suggest named pipes. Someone on SO already provided a great example in Delphi (there are other examples on this site too)

Community
  • 1
  • 1
Mick
  • 13,248
  • 9
  • 69
  • 119
0

I use the Indy TCP Server with my Services and then I can communicate from the same machine or from other machines. It was a snap to add. I send a record back and forth, the beginning of the record tells the server what type of message and what the buffer contains and visa versa.

Richard