Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Communicating between applications 1

Status
Not open for further replies.

Griffyn

Programmer
Jul 11, 2002
1,077
AU
Hi all,

I've written a service application and need to write a configuration program to adjust some of the service's settings. The service retrieves it's values from the registry.

In the past I've done this by either having the configuration program simply write to the registry and force the user to the stop and restart the service, or by implementing a TCP server in the service application and communicating that way. That has the advantage of allowing me to do remote configuring.

But - having started learning about windows messages and reading up on it, I'm thinking this would be a more efficient way of having my configuration program communicate with the running service application to upload new settings to it. I've read the Delphi help and understand how messages work, but not how to implement them in an inter-application environment - specifically with declaring a common MsgID for them to use.

Can anyone give me some links or pointers, or sample code?

Many thanks.
 
Hi, Griffyn. Windows messages are rather simple to implement. There is a predefined constant in Windows.pas called WM_USER. Choosing a message ID consists of the following:

const my_msg_id = WM_USER + 1;

Not advanced at all. Any value below WM_USER is reserved by the OS for internal usage so you should avoid choosing such values for your IDs. In order for your applications to communicate you have to make sure they share this msg_id. Sending a windows message is done like this:

SendMessage(window_handle, my_msg_id, lParam, hParam)

I am not sure of the last two parameters. They could be pointers. Check this in Win32 SDK. Another function to use is the PostMessage function which does almost the same and has the same parameters. window_handle is the handle of the main window of your other-side application. You can retrieve it using the FindWindow API function (in SDK too). I really don't have Win32 SDK in front of me right now so these are just some hints for you to look up. In order to receive the sent messages you can use a very convenient Delphi message mechanism which will handle them in a very straight-forward fashion.
Declare a member function in your form class like the following:

function Wm_my_msg_func(var Message : TMessage); message my_msg_id;

After this declaration, in the function body, you do your processing of the received message. Message specific information is passed through the last two SendMessage parameters... usually a pointer to a record or just simple numbers. The TMessage record contains the information passed through SendMessage's parameters.

I think this is pretty much it. At least the working basics. There's more on that in Delphi's online help. If I am able, I'll be glad to answer any questions you may have about the things written here.

best regards,
Ivan
 
Thanks Ivan,

Since posting my question, I found some more information on the web and have come up with the following:

Since services don't have window handles, in order to send a message to one, the service has to create an invisible window. I'm not sure that this is necessarily a good idea because the service could be hooking back into to parts of windows that reduce it's stability.

While the wParam and lParam parameters of SendMessage are simple LongIntegers, it's possible to send complex messages, including strings and bitmaps, via setting aside memory in the sending application and issuing the memory location of the string or bitmap via these parameters. I'm not sure how this works exactly because accessing another program's memory is an access violation. Perhaps there's a windows DLL call that allocates public memory?

At any rate, I've been able to use SendMessage to send strings by calling SendMessage for every character in the string.

I also experimented with using the predefined broadcast constant in delphi to send the message to all windows - but this is *very* slow - as in around one second per message.

For this current project, I decided on another way of using my configuration program - the service already uses MSAccess tables, so I created a new table that contains variable settings. The service simply looks for new settings using a TTimer object. That works very well.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top