-1

Does anyone have any experience querying FlexLM? (At a minimum) I need to be able to tell if a license is available for a particular application. Previously this was done by checking what processes were running, but if I can somehow query FlexLM, that would be more elegant!

Brian
  • 3,850
  • 3
  • 21
  • 37
Tim
  • 1,621
  • 4
  • 19
  • 35

1 Answers1

4

I've done this recently. I needed to query FlexLM license servers, and discover what licenses were outstanding/available. I didn't find a reasonable API for this, so I instead just launched lmutil, asked it to query the server, and parsed laboriously through the textual results. A pain, but it worked, and really didn't take that long to put together.

Find your copy of lmutil.exe, and run it with either the -a or -i switch, depending on the data you want to gather. Pass it the server and port you wish you query, with the -c switch. Yes, you will need to know the port the FlexLM daemon's running on. There's a standard port, but there's nothing forcing it to run on that port only.

Since I needed to run this regularly, and I needed to query thousands of daemons, I drove lmutil from an application - something like:

string portAtHost = "1708@my.server.com";
string args = String.Format("lmstat -c {0} -a -i", portAtHost);
ProcessStartInfo info = new ProcessStartInfo(@"lmutil.exe", args);
info.WindowStyle = ProcessWindowStyle.Hidden;
info.UseShellExecute = false;
info.RedirectStandardOutput = true;

using (Process p = Process.Start(info))
{
    string output = p.StandardOutput.ReadToEnd();

    // standard output must be read first; wait max 5 minutes
    if (p.WaitForExit(300000))
    {
        p.WaitForExit(); // per MSDN guidance: Process.WaitForExit Method 
    }
    else
    {
        // kill the lmstat instance and move on
        log.Warn("lmstat did not exit within timeout period; killing");
        p.Kill();
        p.WaitForExit(); // Process.Kill() is asynchronous; wait for forced quit
    }   
    File.WriteAllText("c:\file.lmout", output);
}

...then you need to parse through the results. Depending what you're looking for, this could be as simple as splitting the result lines over space characters.

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
  • Works as advertised. Thanks! – Tim Jul 15 '09 at 11:34
  • The `WaitForExit` code is not doing what you think it is. `p.StandardOutput.ReadToEnd()` is synchronous. Execution will not proceed the next line until the process has already terminated. Give it a try by making your own executable that sleeps for 10 seconds and you will see that it waits at `ReadToEnd` for 10 seconds. Also, check `p.HasExited` after `ReadToEnd` and you will see that it's always true. "The redirected StandardOutput stream can be read synchronously or asynchronously. Methods such as Read, ReadLine, and ReadToEnd perform synchronous read operations..." – Glazed Apr 30 '12 at 19:47