Question

Is there a way to create a service using C# able to check if this client is Open?

I need to work a way to automate that checking.

Was it helpful?

Solution

If the window is open (does not need to be in focus) You could use FindWindow window call from the user32.dll to check on the window. Cut up the code below and place in the correct places (for the using, define of the dllimport, and the actual code). This will tell you if the window is open, there is a LOT more you can do with windows calls.

using System.Runtime.InteropServices;

[DllImport("user32.dll", EntryPoint="FindWindow", SetLastError = true)] static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);

IntPtr hWnd = FindWindowByCaption(IntPtr.Zero,"Cisco vpn title here");
if(hWnd.ToInt64() == 0){
    Console.WriteLine("ERROR Could not find cisco vpn.");
}else{
    Console.WriteLine("Handle found");
}

OTHER TIPS

The problem with this client is that its GUI component "vpngui.exe" can be terminated (not listed in the list of processes) but the networking interface that it had established will stay open.

Hence,your service cannot simply check to see whether a process named vpngui.exe is alive and kicking, first you have to detect whether the networking interface is up. You can achieve this by either checking for a registry entry that reveals whether the tunnel is active (see one of my other posts for details), or by checking for the existence of the said interface (again, see my other post).

So, what you need to do is to 1) check whether the tunnel is active 2) (optional) check whether GUI is active

You can then decide what to do next, for instance you could disable the networking interface by using cisco VPN clients CLI interface (or any other windows based option) and then kill the gui. After that you could use the CLI interface to start a new instance of the VPN client that will connect automatically to the account/login/pcf that you provide as an argument to the CLI command.

Heres a bit of code: this will check to see whether the GUI part of the story is alive:

 Process[] processlist = Process.GetProcesses();
 string theProcessThatISeekIs = "";
                foreach (Process theprocess in processlist)  
                {
                    if (theprocess.ProcessName.ToString().ToLower() == NameOfTheProcessYouSeek.ToLower())
                    {

                        theProcessThatISeekIs = theprocess.ProcessName.ToString();
                        break;
                    }
                }

The following code will return the name of the network interface that CISCO is using, if it returns "NotFound" then the interface is off.

string retVal = "";
        var nic = NetworkInterface
                  .GetAllNetworkInterfaces()
                  .Where(i => i.NetworkInterfaceType != NetworkInterfaceType.Loopback && i.NetworkInterfaceType != NetworkInterfaceType.Tunnel && i.Description == "Cisco Systems VPN Adapter");
        var name = nic.FirstOrDefault();
        if (name != null)
        {
            retVal = name.Name.ToString();

        }
        else
        {
            retVal = "NotFound";
        }
        return retVal;

With these two snippets you should be well able to construct a solution to your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top