Click here to Skip to main content
15,880,503 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Coding environment: Labwindows CVI 2012, something like language C.

My last application (*.exe) can run smoothly, but it has no function to prevent it run more than one instance at the same time on same IPC.
Can somebody show me how to use language C to commit this?

Thank you.
Posted

I used this code with CreateMutex similar in earlier times.
C++
//in the header
HANDLE m_hMutex;
//implemetn
CString Mutex = CString("Global\\")+AppTitel;
m_hMutex = CreateMutex(NULL, false, Mutex);

if (m_hMutex == NULL || GetLastError() == ERROR_ALREADY_EXISTS) {
  return false;
}


//Cleanup if existing the program!!!
if (m_hMutex) {CloseHandle(m_hMutex); m_hMutex = NULL;}
 
Share this answer
 
Comments
CPallini 3-Sep-14 8:54am    
Yes, that's the usual way. 5.
yuzaihuan 3-Sep-14 20:47pm    
It is OK in C++, or C#, but can I transform this code into C, if yes how to do ?

Thank you.
KarstenK 4-Sep-14 2:02am    
you must only change the CString to a char array and you're done...
yuzaihuan 4-Sep-14 21:35pm    
got it, I think I need add windows API to use "CreateMutex()" in my code.
yuzaihuan 15-Sep-14 3:15am    
Thank u, KarstenK.
I have tested the code, it is OK.
Need add #include <windows.h> first.
Neat link that demonstrates many 'single-instance' solutions - http://rosettacode.org/wiki/Determine_if_only_one_instance_is_running[^].
 
Share this answer
 
Comments
KarstenK 4-Sep-14 2:04am    
It isnt a proper solution on Windows. See me my solution and the link.
yuzaihuan 4-Sep-14 21:42pm    
POSIX is from Unix, but environment is Windows.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900