I have a simple application with 1 button to know if Notepadd++ is already open. I have review some topics but I cannot find the right one. Inside the button method I have:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
mutex = CreateMutex( NULL, TRUE, "Local\\$notepad++$");
if (GetLastError() == ERROR_ALREADY_EXISTS) {
//MessageBox::Show(..[not open]..);
}
//MessageBox::Show(..[open]..);
}
};
I have a problem with "Local\\$notepad++$", I get this errors:
argument of type "const char *" is incompatible with parameter of type "LPCWSTR"
and this other:
'HANDLE CreateMutexW(LPSECURITY_ATTRIBUTES,BOOL,LPCWSTR)': cannot convert argument 3 from 'const char [18]' to 'LPCWSTR'
If there is another easier way to do this please help me! I have also try changing the name to: notepad++. I am using visual studio 2015 c++
I have review and use as reference:
C/C++ How to tell if a program is already running?
Is using a Mutex to prevent multiple instances of the same program from running safe?
#include "Windows.h"
/*...*/
LPCSTR app_name = "Notepad++: a free (GNU) source code editor";
if (FindWindowA(0, app_name)) {
// it's open!
} else {
// it's not open!
}
for your method, put a * before the quotes
CreateMutex( NULL, TRUE, *"Local\\$notepad++$");
I have better solution:
int processExists(char *victim){
int res=0,ret;
PROCESSENTRY32 proc;
proc.dwSize=sizeof(proc);
HANDLE snap=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(!snap){abort();}
for(ret=Process32First(snap,&proc);ret;ret=Process32Next(snap,&proc)){
if(strstr(proc.szExeFile,victim)){res=1;break;}
}
CloseHandle(snap);
return(res);
}
And then:
private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
mutex = CreateMutex( NULL, TRUE, "Local\\$notepad++$");
if (!processExists("notepad++.exe") {
//MessageBox::Show(..[not open]..);
}
//MessageBox::Show(..[open]..);
}
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.