Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.clipper.visual-objects > #11579

Re: Concurrent user count

From "Karl-Heinz" <invalid@invalid.invalid>
Newsgroups comp.lang.clipper.visual-objects
Subject Re: Concurrent user count
Date 2018-05-06 22:27 +0200
Message-ID <fl96u2Fdbu0U1@mid.individual.net> (permalink)
References <8e056dac-ba36-432f-aa2e-8014c2ee0a75@googlegroups.com> <fkisldFe172U1@mid.individual.net>

Show all headers | View raw


FWIW,

From Sysinternal there are the commandline tools handle.exe and handle64.exe

https://docs.microsoft.com/en-us/sysinternals/downloads/handle

combined with the RunPiped() function, posted many years ago to the german
newsgroup,  it´s easy to detect if a specific file is opened or not.

But, according the EULA the usage of this  tool is restricted:

EULA.txt

[...]
* transfer the software or this agreement to any third party; or
* use the software for commercial software hosting services.
[...]

-----------------------------

METHOD PshPipe() CLASS DtaDlgTest
LOCAL cBuffer  AS STRING
LOCA dwBufferLen, dwError AS DWORD


    dwBufferLen := 1000


   IF ( dwError := RunPiped ( "C:\bat\handle64.exe
vveigsta.dbf -nobanner -u" , @cBuffer , dwBufferLen ) )  == 0

/*
       if nothing is found cBuffer contains:   "No matching handles found."

        or if the file is opened something like this:

         "DBMax.EXE       pid:1296  type: File      pcwin8\khrauscher  1FC:
D:\Haus28\dbf\VVEIGSTA.DBF"
         "wwwHaus28.DBG   pid:1381  type: File      pcwin8\khrauscher  2E4:
D:\Haus28\dbf\VVEIGSTA.DBF"

*/
            odcmle:Value := cBuffer

     ELSE

                MsgInfo ( "Errorcode: " + NTrim (dwError ) )
     ENDIF

     RETURN SELF

-------------------------------------

FUNCTION RunPiped(cCmdLine AS STRING, cRet REF STRING, nBuffer AS DWORD) AS
DWORD PASCAL
*Return Value:
* 0= Ok
* 1= Error MemAlloc()
* 2= Error CreatePipe()
* 3= SetStdHandle()
* 4= Timeout
* 5= Error CreateProcess()
* 6= Error von Anwendung gemeldet

/*

    Per cCmdLine übergibst du den Befehl inklusive Parameter aber ohne die
    Pipe, die Pipe erhälts du als String in cRet den du per Referenz
    übergibst. Der Parameter nBuffer muss groß genug sein für die Rückgabe
    der Pipe.

*/

LOCAL si          IS _WINSTARTUPINFO
LOCAL pi          IS _WINPROCESS_INFORMATION
LOCAL nRet        AS DWORD
LOCAL nRead       AS DWORD
LOCAL nWait := 0  AS DWORD
LOCAL nTotal AS DWORD
LOCAL nRest AS DWORD
LOCAL nSize AS DWORD
LOCAL nTimeout AS DWORD
LOCAL hWriteOut   AS PTR
LOCAL hWriteErr   AS PTR
LOCAL hReadOut    AS PTR
LOCAL hReadErr    AS PTR
LOCAL dwFlags     AS DWORD
LOCAL dwExit      AS DWORD
LOCAL sa          IS _WINSECURITY_ATTRIBUTES
LOCAL hOldStdOut  AS PTR
LOCAL hOldErr     AS PTR
LOCAL pszBuff     AS BYTE PTR

nSize :=nBuffer
nTimeout :=1500
hOldStdOut := GetStdHandle(STD_OUTPUT_HANDLE)
hOldErr    := GetStdHandle(STD_ERROR_HANDLE)

sa.nLength              := _SIZEOF(_WINSECURITY_ATTRIBUTES)
sa.lpSecurityDescriptor := NULL_PTR
sa.bInheritHandle       := TRUE

    pszBuff := MemAlloc(nSize)
    IF pszBuff==NULL_PTR
        nRet:=1
    ENDIF

    //  Create a pipe between two processes for Output device
    IF CreatePipe( @hReadOut, @hWriteOut, @sa,nSize)
        IF !SetStdHandle(STD_OUTPUT_HANDLE, hWriteOut)
            nRet:=3
        ENDIF
    ELSE
        nRet:=2
    ENDIF


    IF nRet=0
        //  Create a pipe between two processes for Error device
        IF CreatePipe( @hReadErr, @hWriteErr, @sa,0)
           SetStdHandle(STD_ERROR_HANDLE, hWriteErr)
        ELSE
           nRet:=2
           CloseHandle( hWriteOut)
           CloseHandle( hReadOut)
        ENDIF
    ENDIF

    IF nRet=0
        //  Attempt to spawn process with piped handles for StdError
        //  and StdOutput
        MemSet(@si, 0, _SIZEOF(_WINSTARTUPINFO))

        si.cb           := _SIZEOF(_WINSTARTUPINFO)
        si.lpReserved   := NULL_PTR
        si.lpDesktop    := NULL_PTR
        si.lpTitle      := NULL_PTR
        si.dwFlags      := STARTF_USESTDHANDLES
        si.dwX          := 0
        si.dwY          := 0
        si.wShowWindow  := SW_HIDE       // Hide output
        si.cbReserved2  := 0
        si.lpReserved2  := NULL_PTR
        si.hStdOutput   := hWriteOut     // Piped StdOut
        si.hStdError    := hWriteErr     // Piped StdErr

        dwFlags := CREATE_NO_WINDOW

        IF CreateProcess( NULL,  ;
                       PTR(_CAST, cCmdLine), ;
                       NULL,;
                       NULL,;
                       TRUE,;
                       dwFlags,;
                       NULL,;
                       NULL,;
                       @si,;
                       @PI)

            IF GetExitCodeProcess( pi.hProcess, @dwExit)

                DO WHILE dwExit == STILL_ACTIVE
                    nWait++
                    IF (nWait > nTimeout)
                        TerminateProcess(pi.hProcess, 5333)
                        nRet:=4
                        EXIT
                    ENDIF
                    Sleep(1)
                    GetExitCodeProcess( pi.hProcess, @dwExit )
                ENDDO
            ENDIF

            //  Read from piped device (StdOutput of child process)
            IF PeekNamedPipe(hReadOut, pszBuff, nSize, @nRead, @nTotal,
@nRest)
                IF nRead=0     // keine Ausgabe in StdOut
                    //  Read from piped device (StdError of child process)
                    IF PeekNamedPipe(hReadErr, pszBuff, nSize, @nRead,
@nTotal, @nRest)
                        IF nRead>0
                            nRet:=6
                        ENDIF
                    ENDIF
                ENDIF
            ENDIF

            // Close handles and reset old standard handles
            SetStdHandle(STD_INPUT_HANDLE, hOldStdOut)
            SetStdHandle(STD_ERROR_HANDLE, hOldErr)
            CloseHandle(pi.hProcess )
            CloseHandle(pi.hThread )
        ELSE
            nRet:=5

        ENDIF

        // Close handles
        CloseHandle(hReadOut)
        CloseHandle(hReadErr)
        CloseHandle(hWriteOut)
        CloseHandle(hWriteErr)

    ENDIF

    IF nSize > 0
        cRet+= Psz2String(pszBuff)
    ENDIF

    IF pszBuff != NULL_PTR
        MemFree(pszBuff)
    ENDIF

RETURN nRet

--------------
regards
Karl-Heinz 

Back to comp.lang.clipper.visual-objects | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Concurrent user count Kuno Egger <kuno.egger@gmail.com> - 2018-04-26 11:31 -0700
  Re: Concurrent user count dlzc <dlzc1@cox.net> - 2018-04-26 12:00 -0700
  Re: Concurrent user count Martin <Martin@spam.spam> - 2018-04-27 09:12 +0100
  Re: Concurrent user count "Karl-Heinz" <invalid@invalid.invalid> - 2018-04-28 11:17 +0200
    Re: Concurrent user count "Karl-Heinz" <invalid@invalid.invalid> - 2018-05-06 22:27 +0200
  Re: Concurrent user count Kuno Egger <kuno.egger@gmail.com> - 2018-05-08 09:41 -0700

csiph-web