PID in einer WIN32/Console ausgeben ?!



  • Hallo Forum,

    ist es moeglich in einer Win32-Console die Aktuelle PID des Programmes
    herraus zufinden und anzeigen oder geht das nur in WINAPI ?!

    mfg Oliver Kern.



  • Das geht sicherlich in einer Win32-Konsole. Trotzdem brauchst du dafür eine WinApi-Funktion, die dir die pid zurückgibt, womit es mit ANSI-C nichts mehr zu tun hat.



  • @ DrGreenthumb hat recht
    Aber vielleicht hift das weiter, gibts auch für Threadid

    The GetCurrentProcessId function retrieves the process identifier of the calling process.

    DWORD GetCurrentProcessId(VOID);
    Parameters
    This function has no parameters.

    Return Values
    The return value is the process identifier of the calling process.

    Remarks
    Until the process terminates, the process identifier uniquely identifies the process throughout the system.

    MAPI: For more information, see Syntax and Limitations for Win32 Functions Useful in MAPI Development.



  • es funktioniert:

    pid = GetCurrentProcessId();
    	printf("PID:%d",pid);
    

    Wie kann man erreichen das ein Programm nicht mehrmals gestartet wird ?!

    mfg Oliver Kern



  • Erzeuge einen Mutex siehe unten

    HANDLE hMutex;
    
    hMutex = CreateMutex(
      NULL,                  // no security descriptor
      FALSE,                 // mutex not owned
      "progname" );             // Name des Programms das gestartet wurde
    
      if (hMutex == NULL)
        printf("CreateMutex error: %d\n", GetLastError() );
      else
        if ( GetLastError() == ERROR_ALREADY_EXISTS )
        {
          MessageBox(0,"CreateMutex opened existing mutex\n Applikation Starter is already started", "ERROR",MB_ICONERROR|MB_OK|MB_SETFOREGROUND|MB_APPLMODAL );
          exit(1);
        }
    

    Am ende des Programms gehört dann noch ein
    CloseHandle(hMutex);



  • Vielen Dank, genau das suchte ich !!!

    mfg Oliver Kern.


Anmelden zum Antworten