scratchfoundation / scratch-link

Device interoperability layer for Windows and MacOS
BSD 3-Clause "New" or "Revised" License
102 stars 83 forks source link

Windows: display which program is causing port conflict #170

Closed cwillisf closed 4 years ago

cwillisf commented 4 years ago

Proposed Changes

On Windows only (for now): if Scratch Link fails to bind its communication port, try to display the process ID and name for any process using that port.

Before: image

After: image

Note that FiddlerWinsock.cs was written by another group and should be treated as an external dependency. Also, AddressInUse.Designer.cs and AddressInUse.resx are auto-generated code created by the dialog editor tool in Visual Studio.

Reason for Changes

Under normal circumstances a particular port can only be used by one application at a time. If Scratch Link fails to bind its port, the most likely reason is that another application has already bound that port. Previously, Scratch Link would report this but figuring out which application is causing the conflict requires running commands at an administrator-level command prompt, which is at best difficult to explain and at worst impossible for users with restricted permissions.

This feature should help users figure out which application(s) are conflicting with Scratch Link without needing to use a command prompt.

Based on help desk tickets, this seems to affect Windows users much more often than macOS users. I still intend to implement this on macOS in a separate change.

cwillisf commented 4 years ago

I renamed "label2" to "labelNoResults" to be a little more descriptive, and now I set its visibility based on whether or not MapLocalPortToProcessId returned a good PID. If so, the new code displays more details than it used to including the title of the main window (if any) and the file name.

Here's what it looks like now (note that Scratch Link has no main window): image

...and here's the corresponding "Copy" text:

Scratch Link was unable to start because port 20110 is already in use.

This means Scratch Link is already running or another application is using that port.

This application will now exit.

Details:
Process Name:   ScratchLink
Window Title:   
File Name:  C:\Program Files (x86)\Scratch Link\ScratchLink.exe
Process ID: 34692
TCP Port:   20110
cwillisf commented 4 years ago

Oh, and here's what it looks like if there's a failure in trying to determine which program is using the port: image

Corresponding "Copy" text:

Scratch Link was unable to start because port 20110 is already in use.

This means Scratch Link is already running or another application is using that port.

This application will now exit.

Details:
Could not collect details. Running as Administrator might help.
cwillisf commented 4 years ago

Ooh, good point about the confusion. Hmmm...