Create Mutex/GetLasteError() = ERROR_INVALID_HANDLE

G

Guest

It seems like in a compact frameworks application if a user is very anxious
and clicks twice on the icon to start the application within a couple of
seconds, it starts two instances of the program, once the application is
running, it only sets the program to the top level window.

I found some code that makes sense that would eliminate the problem

[DllImport("coredll.dll", EntryPoint = "CreateMutexW")]
private static extern int CreateMutex(IntPtr lpMutexAttributes, bool
InitialOwner, string MutexName);


string mutexName = "XXXXX_MOBILE_APP";
CreateMutex(IntPtr.Zero, true, mutexName)
if(GetLastError() == ERROR_ALREADY_EXISTS)
ShutDown();

However everytime when I'm calling the the GetLastError() message I always
get a ERROR_INVALID_HANDLE return code. However I do get a non-NULL mutex
handle.

Any help would be appreciated!
 
G

Guest

You're not capturing the returned handle in this code - how do you know it's
not returning a valid handle?

-Chris
 
G

Guest

Chris -

The actual code is:
string mutexName = "CONNECTWISE_MOBILE_APP";
int mutex = CreateMutex(IntPtr.Zero, true, mutexName);
if (mutex != 0)
{
int lastError = Marshal.GetLastWin32Error();
Data.Logger.LogException("MainForm", "Create Mutex Failed
With->" + lastError.ToString());
Data.Logger.LogException("MainForm", "Mutex Is->" + mutex);

if (lastError == ERROR_ALREADY_EXISTS)
ShutDownApp();
}

This is what gets writtent to the log file:
7/5/06 6:51:42 AM
MainForm
Create Mutex Failed With->6
-----------


7/5/06 6:51:42 AM
MainForm
Mutex Is->1917590858
-----------


7/5/06 6:51:43 AM
MainForm
Create Mutex Failed With->6
-----------


7/5/06 6:51:43 AM
MainForm
Mutex Is->1917590858
-----------

Note I started two instances within a couple of seconds and the returned
mutexs are identical, but get from 6 from the GetLastError() return code
ERROR_INVALID_HANDLE.

You're not capturing the returned handle in this code - how do you know it's
not returning a valid handle?

-Chris


Kevin FL said:
It seems like in a compact frameworks application if a user is very
anxious
and clicks twice on the icon to start the application within a couple of
seconds, it starts two instances of the program, once the application is
running, it only sets the program to the top level window.

I found some code that makes sense that would eliminate the problem

[DllImport("coredll.dll", EntryPoint = "CreateMutexW")]
private static extern int CreateMutex(IntPtr lpMutexAttributes, bool
InitialOwner, string MutexName);


string mutexName = "XXXXX_MOBILE_APP";
CreateMutex(IntPtr.Zero, true, mutexName)
if(GetLastError() == ERROR_ALREADY_EXISTS)
ShutDown();

However everytime when I'm calling the the GetLastError() message I always
get a ERROR_INVALID_HANDLE return code. However I do get a non-NULL mutex
handle.

Any help would be appreciated!
 
Joined
Aug 3, 2007
Messages
1
Reaction score
0
I ran into the same problem, and found this thread via web search... it didn't have the answer, but I figured out a workaround and thought I'd post it :) .

My original code was something like this:
Code:
private bool TestMutex()
{
	IntPtr hMutex = CreateMutex(IntPtr.Zero, false, "BlahBlah"); 
	if (hMutex != IntPtr.Zero)
	{
		int err = GetLastError();
		CloseHandle(hMutex);
		if (err == ERROR_ALREADY_EXISTS)
			return false;
	}
	return true;
}
 

[size=2][color=#0000ff]private [/color][/size][size=2][color=#0000ff]const [/color][/size][size=2][color=#0000ff]int[/color][/size][size=2] ERROR_ALREADY_EXISTS = 183;
 
[[/size][size=2][color=#2b91af]DllImport[/color][/size][size=2]([/size][size=2][color=#a31515]"coredll.dll"[/color][/size][size=2], SetLastError = [/size][size=2][color=#0000ff]true[/color][/size][size=2])]
[/size][size=2][color=#0000ff]private [/color][/size][size=2][color=#0000ff]static [/color][/size][size=2][color=#0000ff]extern [/color][/size][size=2][color=#2b91af]IntPtr[/color][/size][size=2] CreateMutex([/size][size=2][color=#2b91af]IntPtr[/color][/size][size=2] lpMutexAttributes, [/size][size=2][color=#0000ff]bool[/color][/size][size=2] InitialOwner, [/size][size=2][color=#0000ff]string[/color][/size][size=2] MutexName);
 
[[/size][size=2][color=#2b91af]DllImport[/color][/size][size=2]([/size][size=2][color=#a31515]"coredll.dll"[/color][/size][size=2])]
[/size][size=2][color=#0000ff]private [/color][/size][size=2][color=#0000ff]static [/color][/size][size=2][color=#0000ff]extern [/color][/size][size=2][color=#0000ff]bool[/color][/size][size=2] CloseHandle([/size][size=2][color=#2b91af]IntPtr[/color][/size][size=2] hMutex);
 
[[/size][size=2][color=#2b91af]DllImport[/color][/size][size=2]([/size][size=2][color=#a31515]"coredll.dll"[/color][/size][size=2], EntryPoint = [/size][size=2][color=#a31515]"GetLastError"[/color][/size][size=2])]
[/size][size=2][color=#0000ff]private [/color][/size][size=2][color=#0000ff]static [/color][/size][size=2][color=#0000ff]extern [/color][/size][size=2][color=#0000ff]int[/color][/size][size=2] GetLastError();
[/size]

In all cases, the call to GetLastError returned ERROR_INVALID_HANDLE (6).

Changing the GetLastError call to Marshal.GetLastWin32Error() fixed the problem.

I see that the OP mentioned both GetLastError and Marshal.GetLastWin32Error so I'm confused. But this worked for me. YMMV.

Regards,
David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top