tags:

views:

72

answers:

4

I have a C++ application that needs to communicate to a C# application (a windows service) running on the same machine. I want the C++ application to be able to write as many messages as it wants, without knowing or caring when/if the C# app is reading them, or even if it's running. The C# app be able to should just wake up every now and then and request the latest messages, even if the C++ app has been shut down.

What is the simplest way to achieve this? I think this kind of thing is what MSMQ is for, but I haven't found a good way to do it in C++. I'm using Named Pipes right now, but that's not really working out as the way I'm doing it requires a connection between the two apps, and the C++ call to WriteLine blocks until the read takes place.

Currently the best solution I can think of is just writing the messages to a file with a timestamp on each message that the C# application checks periodically against its last update timestamp. That seems a little crude, though.

What is the simplest way to achieve this sort of messaging?

A: 

MSMQ definitely sounds like what you want, or the more basic reading and writing files written to a common area but then you need to watch contention on the files.

VC++ help on MSMQ.

Colin Desmond
+1  A: 

I would use a named pipe.

Alex Emelianov
+1  A: 

Well, the simplest way actually is using a file to store the messages. I would suggest using an embedded database like SQLite, though: the advantage will be better performance and a nice way to query for changes (i.e. SELECT * FROM messages WHERE timestamp > last_app_start).

Jim Brissom
Better performance than what? Than appending to the file in the writer app, and doing a seek to last-used read position at startup, followed by reads, in the reader app? Admittedly, the reader would also have to periodically lock the file to empty it.
Steve Jessop
SQLite looks like the way to go. It's remarkably easy to use, and it gives me the flexibility that I need, and a simplicity that I appreciate.
Mike Pateras
A: 

The requirement of both apps not always running at the same time but still being able to message each other definitely means you need a third component to store/queue messages. Whether you use a shared database/file or you write a third app that acts as a message store is up to you. Either way you will find sharing always causes contention.

Personally I would look at 0MQ before MSMQ but neither will solve your problem as is. An sqlite database would be my first choice.

bkersten