C++ Get Handle of Open Sockets of a Program
As Remy said, its not trivial. You have to call OpenProcess
with PROCESS_DUP_HANDLE
for each process in the system. You might also need PROCESS_QUERY_INFORMATION
and PROCESS_VM_READ
, but I've never needed it (I've seen other code that uses it).
For each process, you access the donor process's handle table with NtQuerySystemInformation
(with an information class of SystemHandleInformation
). Finally, you call DuplicateHandle
to make the process's handle your handle, too.
You will have to filter the handle types when enumerating the donor process's handle table. For each handle you have duplicated, call NtQueryObject
with ObjectTypeInformation
. If the type is a socket, you keep it open and put it in your list. Otherwise, close it and go on.
To perform the compare, the code looks similar to below. The type is returned as a UNICODE_STRING
:
// info was returned from NtQueryObject, ObjectTypeInformation
POBJECT_TYPE_INFORMATION pObjectTypeInfo = (POBJECT_TYPE_INFORMATION)(LPVOID)info;
wstring type( pObjectTypeInfo->Name.Buffer, pObjectTypeInfo->Name.Length );
if( 0 != wcscmp( L"Socket", type.c_str() ) ) { /* Not a Socket */ }
If there is no Socket type (I don't recall), you should try to get the name associated with the handle (its still a UNICODE_STRING
), and look for \\Device\\Tcp
. This time, you would use the same handle, but call NtQueryObject
with ObjectNameInformation
:
// info was returned from NtQueryObject, ObjectNameInformation
POBJECT_NAME_INFORMATION pObjectNameInfo = (POBJECT_NAME_INFORMATION)(LPVOID)info;
wstring name( pObjectNameInfo->Name.Buffer, pObjectNameInfo->Name.Length );
if( name.substr(0, 11) == "\\Device\\Tcp" ) ) { /* It's a TCP Socket */ }
Myself an another fellow did similar a few years ago. Instead of Sockets, we used Mutexes and Events to crash privileged Antivirus components from their userland UI program (which was sharing handles with the privileged component for IPC). See Old Dogs and New Tricks: Do You Know Where Your Handles Are?.
Get a number of open sockets in C#?
Have a look at the TcpStatistics Class.
For example, the TcpStatistics.CurrentConnections Property returns "The number of TCP connections for which the current state is either ESTABLISHED or CLOSE-WAIT."
long result = IPGlobalProperties.GetIPGlobalProperties()
.GetTcpIPv4Statistics()
.CurrentConnections;
How to pass a SOCKET (HANDLE) from unmanaged C program to .NET child process?
"You need to call WSAGetLastError to get more information. My guess is that sockets are not initialized (see WSAStartup)."
Simple, but effective. Calling WSAStartup() did the trick.
Thank you very much.
How to get local port not knowing socket
Since you mention .so, I take it is some of the *Nix variant. And since it is *nix, it means it is Linux :)
It is rather easy to do this. Just take a snapshot of process file descriptors before calling create connection - for instance, by reading /proc//fd - and than after calling connection. The extra descriptor you see there would be a new socket opened (if there is more than one, it means, library opened more than one descriptor - you will have to guess which is the one you need.) Now you have your descriptor and you can inquiry it.
How to find a socket's local port number? (Windows C++)
Use getsockname
. For example:
struct sockaddr_in sin;
int addrlen = sizeof(sin);
if(getsockname(clientSock, (struct sockaddr *)&sin, &addrlen) == 0 &&
sin.sin_family == AF_INET &&
addrlen == sizeof(sin))
{
int local_port = ntohs(sin.sin_port);
}
else
; // handle error
This also works for *nix-based systems, but note that some systems define the third argument of getsockname
to be of type socklen_t*
instead of int*
, so you might get warnings about pointers differing in signedness if you're writing cross-platform code.
Related Topics
Long VS. Int C/C++ - What's the Point
How to Print the Value of Nullptr on Screen
Generate All Sequences of Bits Within Hamming Distance T
Is There a Case Where Including the Same Header Twice Is Actually Helpful
Why Does My Cout Output Not Appear Immediately
How to Compare Two Standard Conversion Sequences Use the Rank of Contained Conversions
C++ Copy-Construct Construct-And-Assign Question
Two Colours Text in Qpushbutton
How to Use 'Const_Cast' to Modify a Constant Variable
Using Sendmessage to Send Wm_Close to Another Process
What Is Vc++ Doing When Packing Bitfields
Throw and Ternary Operator in C++
How to Read/Write Std::String Values From/To Binary Files