.net - Get process ID of a client that connected to a named pipe server with C# -
i'm not sure if i'm not seeing it, or what? need know process id of client connected via named pipe server instance of namedpipeserverstream
. such possible?
in meantime came function:
[dllimport("kernel32.dll", setlasterror = true)] internal static extern bool getnamedpipeclientprocessid(intptr pipe, out uint32 clientprocessid); public static uint32 getnamedpipeclientprocid(namedpipeserverstream pipeserver) { //return: // = client process id connected via named pipe server, or // = 0 if error uint32 nprocid = 0; try { intptr hpipe = pipeserver.safepipehandle.dangerousgethandle(); getnamedpipeclientprocessid(hpipe, out nprocid); } catch { //error nprocid = 0; } return nprocid; }
i'm not strong in "dangerousgethandles" , "dllimports". i'm way better off win32, i'm using here.
the main problem code, not perform correct error handling. need check return value of getnamedpipeclientprocessid
detect error.
[dllimport("kernel32.dll", setlasterror = true)] internal static extern bool getnamedpipeclientprocessid(intptr pipe, out uint clientprocessid); public static uint getnamedpipeclientprocid(namedpipeserverstream pipeserver) { uint32 nprocid; intptr hpipe = pipeserver.safepipehandle.dangerousgethandle(); if (getnamedpipeclientprocessid(hpipe, out nprocid)) return nprocid; return 0; }
Comments
Post a Comment