-3

Edit: I can't see anything wrong with my question. It is not a duplicate, even though people have accused it to be. If you decide to down-vote the question anyways, please tell me why, so I can understand. Thank you.

How can I force another process to close a port that it is currently using. For example if Skype is listening on port 80 locally. How can I programmatically force Skype to release that port again, so that I can run XAMPP on the same port properly? Without changing Skype settings or closing Skype.

Yes, I know this might lead to unexpected behavior.

Is it just a matter of closing a handle using DuplicateObject or how is it done?

I'm not interested in closing the process or finding out which process is blocking the port. I already knkw how that's done properly even without netstat.

Edit: I found WSADuplicateSocket which looked promising. But it has to be called from the process that already has the Descriptor identifying the local socket as Microsoft calls it. If I could somehow get that Descriptor identifying the local socket I might be able to call closesocket on that. This could probably be done with come code injecting, but I'm sure there is a better way.

Edit: A tool called CurrPorts is allegedly able to do what I'm trying to do. So it's definitely possible... but how?

Forivin
  • 14,780
  • 27
  • 106
  • 199
  • 2
    Why is this tagged c++? Force close Skype. See http://stackoverflow.com/questions/788348/how-do-i-free-my-port-80-on-localhost-windows?rq=1 – Captain Giraffe Apr 16 '16 at 22:01
  • You can turn off the option "Use port 80 and 443 for additional incoming connections" in skype. – melak47 Apr 16 '16 at 22:06
  • That is not what I asked for. Neither am I interested in force closing any process, nor am I looking to change other applications settings. I spare you the answer on "why this is tagged with C++". If you truly think this question deserves a down-vote please tell me why. You surely can't expect me to provide a code of "what I've tried" here.. – Forivin Apr 16 '16 at 22:16
  • You can't seriously expect that a modern operating system is going to provide you with a facility to break other processes. – user207421 Apr 17 '16 at 08:07
  • Oh, yes I can. You see DuplicateObject already provides functionality to to close other processes handles. And this is definitely not necessarily breaking the process. And even if. Closeing a process could cause just as much damage as this, potentially even more. – Forivin Apr 17 '16 at 08:10
  • [Forcing handles closed is not a good idea](https://technet.microsoft.com/en-us/magazine/2009.04.windowsconfidential.aspx). – Raymond Chen Apr 17 '16 at 15:47
  • I am well aware of that. Although I would say "not necessarily" instead. I know what you mean though. – Forivin Apr 17 '16 at 16:19

1 Answers1

0

The correct way to access a socket from another process is to inject code into that process that calls WSADuplicateSocket() to duplicate the desired socket handle for access by your process, then send the details to your process via IPC, and access the socket via WSASocket(). See:

C++ Get Handle of Open Sockets of a Program

However, a more brute-force approach would be to use DuplicateHandle() with the DUPLICATE_CLOSE_SOURCE flag instead. No need for code injection:

Community
  • 1
  • 1
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I enumerated through all the handles and logged what types of handles I got. Here is the full list: http://pastebin.com/DZDTgZ7K I couldn't identify anything as a "socket object". I'm also not sure if sockets really have handles, as Microsoft always talks about `Descriptors` rather than `Handles` when it comes to sockets. – Forivin Apr 20 '16 at 08:27
  • @Forivin sockets are kernel objects on Windows, and have real handles. See [Socket handles](http://forum.sysinternals.com/socket-handles_topic1193.html) for how to detect them during handle enumeration and query their port details. – Remy Lebeau Apr 20 '16 at 14:03