views:

74

answers:

4

Is it possible to use a Mutex Object for a application which users c++ dll to do background work and a c# to display it. Bot these use a common resource ie db . So can Mutex be used to lock this resource. In my db c++ will insert to db and c# will read it.

+3  A: 

There is a thing called "named mutex", which is OS object and can be shared between different libraries and applications, only by specifying its name on creation/use.
Refer to http://msdn.microsoft.com/en-us/library/ms682411%28VS.85%29.aspx

alemjerus
A: 

In addition to alemjerus answer:

http://msdn.microsoft.com/en-us/library/ms682411(VS.85).aspx

Maybe this works for C#:

http://www.pinvoke.net/default.aspx/kernel32/CreateMutex.html

Arthur
+1  A: 

You should lock the db instead of using a Mutex since there is always a possibility that other applications might access the same resource. Even if you know it's only your dll and application that currently accesses the resource, someone could write an application to access the same resource and break the scheme.

Have a look at SQLite PRAGMA (locking_mode and synchronous).

Sani Huttunen
A: 

You have to use this constructor in C# in the System.Threading namespace:

public Mutex( bool initiallyOwned, string name, out bool createdNew )

The C part is covered in the other answers - thus there is no need for Interop/PInvoke.

weismat