2013-01-24 67 views

回答

0

的Windows Vista和Windows 7提供了一個相當強大的防火牆API,可以用來例外添加到防火牆。下面的代碼將爲指定的應用程序的Windows防火牆添加一個例外,只要代碼以管理員權限運行即可。將%systemroot%\ system32 \ FirewallAPI.dll的引用添加到您的應用程序。

Imports NetFwTypeLib 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 

    ' Create the Application we want to add to the exception list 
    Dim appType As Type = Type.GetTypeFromProgID("HnetCfg.FwAuthorizedApplication") 
    Dim app As INetFwAuthorizedApplication 
    app = DirectCast(Activator.CreateInstance(appType), INetFwAuthorizedApplication) 

    ' Set the application properties 
    app.Name = "Negative0's Sandbox" 
    app.ProcessImageFileName = "C:\Users\Negative0\vbsandbox2.exe" 
    app.Enabled = True 


    ' Get the firewall manager, so we can get the list of authorized apps 
    Dim fwMgrType As Type = Type.GetTypeFromProgID("HnetCfg.FwMgr") 
    Dim fwMgr As INetFwMgr 
    fwMgr = DirectCast(Activator.CreateInstance(fwMgrType), INetFwMgr) 

    ' Get the list of authorized applications from the Firewall Manager, so we can add our app to that list 
    Dim apps As INetFwAuthorizedApplications 
    apps = fwMgr.LocalPolicy.CurrentProfile.AuthorizedApplications 
    apps.Add(app) 

End Sub 

Source