Well, you can declare lpMutexAttributes as IntPtr if you are going to
pass null. Also, you should declare the bInitialOwner parameter as bool,
and you should indicate the version of the function you want to use in the
DllImport attribute and you should set the return type to IntPtr (since it
is a handle). Putting it all together, you should have:
[DllImport("kernel32.dll", SetLastError=True, CharSet=CharSet.Auto)]
private static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool
bInitialOwner, string lpName);
Then, you can pass IntPtr.Zero to the first parameter.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
Bronislav said:
I tried to do exactly that but I cannot find how to call CreateMutex
through
p-invoke
I tried both declaretions
/*
[DllImport("kernel32.dll", SetLastError=true)]
private static extern int CreateMutexA(
ref SECURITY_ATTRIBUTES lpMutexAttributes,
int bInitialOwner,
string lpName);
*/
[DllImport("kernel32.dll", SetLastError=true)]
private static extern System.IntPtr CreateMutexA(
out int lpMutexAttributes,
bool bInitialOwner,
string lpName);
but when I call CreateMutex
lhndlRetHandle = CreateMutexA(null, true, lstrAppName);
It give me error that first parameter cannot be NULL
Thanks.
Nicholas Paldino said:
Bronislav,
You can call the static OpenExisting method on the Mutex class to get
a
named mutex instance. If it doesn't exist, then you can catch the
WaitHandleCannotBeOpenedException instance that is thrown and then create
a
new one.
Now, you do run the risk of running into a race condition here, as
two
instances of the app can try to open a mutex that doesn't exist, which is
different from the call to CreateMutex. If you want to avoid this race
condition, declare the CreateMutex API function and call it through the
P/Invoke layer, getting the handle. Then, create a new Mutex instance,
and
assign the handle to that if you want to use it.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
The Mutex class in my understanding is not complete analog of the
CreateMutex
API
because it not allowed to create difference instances of the programm
with
the different name and I need exactly this.
:
Bronislav,
There are a number of examples of how to do this on the web. Have
you
taken a look at the Mutex class? It will give you everything you need
to
do
to replicate this behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)
I need to run more then 1 instance of the same program but also I
need
to
know which instance is running.
In C++ code was:
int nInstance = 0;
char szAppName[32];
m_dwMutexReturn = ERROR_ALREADY_EXISTS;
while (( m_dwMutexReturn == ERROR_ALREADY_EXISTS ) && ( nInstance <
MAXINSTANCES ))
{
nInstance++;
itoa( nInstance, szTemp, 10 );
strcpy( szAppName, "ProductEngine" );
strcat( szAppName, szTemp );
m_hMutex = CreateMutex( NULL, TRUE, szAppName );
m_dwMutexReturn = GetLastError();
}
if ( nInstance == MAXINSTANCES )
m_sState->nInstance = 1;
else
m_sState->nInstance = nInstance;
but I can't port this code to C#.
Somebody knows how to do it?