-1

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?

mejia_david
  • 317
  • 1
  • 3
  • 7
  • Does notepad already use a mutex you can use?? – user0042 Sep 10 '17 at 23:46
  • 1
    You could get it to compile by writing `L"Local\\$notepad++$"`; note the `L` prefix. Not sure it would actually work though - what makes you believe Notepad++ creates a mutex with this name? – Igor Tandetnik Sep 10 '17 at 23:48
  • To clarify what @IgorTandetnik is suggesting, `LPCWSTR` is a pointer to a string of 16-bit characters. `const char *` is for 8-bit characters. Adding `L` before a string constant will tell the compiler to treat that the characters in that string as 16-bit, which can be cast to `LPCWSTR`. – bindsniper001 Sep 10 '17 at 23:56
  • I do not konw if Notepadd++ creates mutex. Is there a way to find out? – mejia_david Sep 11 '17 at 00:47

2 Answers2

0
#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++$");
vidsac
  • 84
  • 12
0

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]..);
}
};
bukkojot
  • 1,526
  • 1
  • 11
  • 16
  • Thank you @bukkojot. I was able to find: https://msdn.microsoft.com/en-us/library/windows/desktop/ms686701(v=vs.85).aspx – mejia_david Sep 11 '17 at 02:58