J'ai réussi à créer un mutex .net comme ceci: SingleIns = new Mutex (true, AppName); pendant un moment. Cela fonctionne dans XP, Vista, mais apparemment pas dans Windows7. J'ai donc besoin de faire un appel interop à une bibliothèque Win32 pour que d'autres composants Com puissent identifier le mutex. J'ai trouvé le code suivant, mais le Win32Calls. n'est pas trouvé ... existe-t-il un assemblage ou une référence dont j'ai besoin? Merci à l'avance,Comment appeler win32 CreateMutex à partir de .Net
code trouvé de: http://www.pinvoke.net/default.aspx/kernel32/CreateMutex.html
utilisant System.Runtime.InteropServices;
[DllImport("kernel32.dll")]
public static extern IntPtr CreateMutex(IntPtr lpMutexAttributes, bool bInitialOwner, string lpName);
// create IntPtrs for use with CreateMutex()
IntPtr ipMutexAttr = new IntPtr(0);
IntPtr ipHMutex = new IntPtr(0);
try
{
// Create the mutex and verify its status BEFORE construction
// of the main form.
ipHMutex = Win32Calls.CreateMutex(ipMutexAttr,
true, "CompanyName_AppName_MUTEX");
if (ipHMutex != (IntPtr)0)
{
// check GetLastError value (MUST use this call. See MSDN)
int iGLE = Marshal.GetLastWin32Error();
// if we get the ERROR_ALREADY_EXISTS value, there is
// already another instance of this application running.
if (iGLE == Win32Calls.ERROR_ALREADY_EXISTS)
// So, don't allow this instance to run.
return;
}
else
{ // CreateMutex() failed.
// once the app is up and running, I log the failure from
// within the frmMain constructor.
bool m_bMutexFailed = true;
}
// construct the main form object and
//form = new frmMain();
// run the app.
//Application.Run(form);
}
catch(Exception oEx)
{
//...handle it...
}
finally
{
// release the mutex
if (ipHMutex != (IntPtr)0)
Win32Calls.ReleaseMutex(ipHMutex);
// cleanup the main form object instance.
if (form != null) {
form.Dispose();
}
}
}
Hmm. Je viens de supprimer la partie Win32Calls de Win32Calls.CreateMutex et appelé directement CreateMutex ... il semble que cela pourrait fonctionner – mytwocents
Êtes-vous sur une version 64 bits de Windows 7? Un .NET 'Mutex' devrait essentiellement fonctionner de la même manière dans Windows 7 que dans les versions précédentes. –
Ceci est une meilleure référence ... http://msdn.microsoft.com/fr-fr/library/microsoft.win32.safehandles.safewaithandle.aspx – mytwocents