Open firewall from your application

Jan 28

Since I had added SSL support to the SMTP sending from my application using stunnel, some users don’t opened up the firewall when requested from the operating system (from Windows Vista up).

Therefore I needed to open up the firewall directly from the application.

Since I don’t like to have run the application with elevated rights, I decided to make this change from an external app to spawn from my own application.

It is very simply to add a firewall rule using the netsh command:

netsh advfirewall firewall add rule name=”stunnel” dir=in action=allow program=”c:\tools\stunnel.exe” enable=yes profile=any

(where the stunnel.exe path should be adapted to your own settings).

The problem now was that this command needed to be run with elevated rights, and there is no option to do so.
After a quick search on the internet, I found elevate.exe – a small .NET executable that does what I need: launch a program with elevated rights.

So it was easy: launch elevate.exe (downloadable with sources) and pass as command line the call to netsh.exe.

Comments are closed.