title: Windows semaphores
body: <p>I need to implement a program which uses a semaphore to limit the number of opened notepad windows to 10. I created a semaphore in  WM_INITDIALOG </p>
  
  <pre><code>    semafor = CreateSemaphore(0, 1, 10, "semaphore");
  </code></pre>
  
  <p>and every time I click on button to open a new window, I open that semaphore. However it doesn't stop me from opening more than 10 windows.</p>
  
  <p>Here is my code when I click on button in a dialog box to open a new window:</p>
  
  <pre><code>case WM_COMMAND:
      switch (LOWORD(wParam)) {
          case ID_OK: 
  
              semafor = OpenSemaphore(SEMAPHORE_ALL_ACCESS, TRUE, "semaphore");
              if (semafor == NULL) {
                  printf("Eroare deschidere semafor empty: %d \n", GetLastError());
                  ExitProcess(1);
              }
  
              BOOL b = CreateProcess("C:\\Windows\\System32\\notepad.exe",
                  NULL, NULL, NULL, TRUE, 0, NULL, NULL,
                  &amp;si, &amp;pi);
  
              process[++i] = GetCurrentProcess();
  
              if (b) {
                  dwWaitForChild = WaitForInputIdle(pi.hProcess, 2000);
                  switch (dwWaitForChild) {
                  case 0:
                      printf("Procesul fiu este ready!\n");
                      break;
                  case WAIT_TIMEOUT:
                      printf("Au trecut 2 sec. si procesul fiu nu este ready!\n");
                      break;
                  case 0xFFFFFFFF:
                      printf("Eroare!\n");
                      break;
                  }
  
                  WaitForMultipleObjects(i, process, TRUE, INFINITE);
  
                  iRasp = MessageBox(NULL, "Terminam procesul fiu?", "Atentie!", MB_YESNO);
                  if (iRasp == IDYES) {
                      if (TerminateProcess(pi.hProcess, 2)) {
  
                          DWORD dwP;
  
                          GetExitCodeProcess(pi.hProcess, &amp;dwP);
                          printf("Codul de terminare al procesului fiu: %d\n", dwP);
                          ReleaseSemaphore(semafor, 1, NULL);
                          CloseHandle(pi.hProcess);
                          printf("\nProcesul fiu a fost terminat cu succes\n");
                      }
                      else {
                          //tiparim mesajul de eroare 
                          TCHAR buffer[80];
                          LPVOID lpMsgBuf;
                          DWORD dw = GetLastError();
  
                          FormatMessage(
                              FORMAT_MESSAGE_ALLOCATE_BUFFER |
                              FORMAT_MESSAGE_FROM_SYSTEM,
                              NULL,
                              dw,
                              MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
                              (LPTSTR)&amp;lpMsgBuf,
                              0, NULL);
  
                          wsprintf(buffer, "TerminateProcess() a esuat cu eroarea %d: %s",
                              dw, lpMsgBuf);
  
                          MessageBox(NULL, buffer, "Eroare!", MB_OK);
  
                          LocalFree(lpMsgBuf);
                      }
                  } // rasp YES
  
              }
              else
                  printf("Eroare la crearea procesului fiu!\n");
  
              return TRUE;
          }
          break;
      }
      return FALSE;
  }
  </code></pre>