Reputation: 21
I am developing a plug-in for a third-party application which requires to use a System.LicenseProvider.
The License File itself is FlexLM generated.
so I have:
[LicenseProvider(typeof(LicFileLicenseProvider))]
public class MyLicensedModule
{
protected System.ComponentModel.License thisLicense;
protected ModuleFlexFeature thisfeature;
public bool LicenseCheck()
{
bool isLicensed = LicenseManager.IsLicensed(typeof(ModuleFlexFeature)); //returns TRUE
if(thisFeature==null) thisFeature = new ModuleFlexFeature();
thisLicense = LicenseManager.Validate(typeof(ModuleFlexFeature),thisFeature);
//no thrown exception
return (thisLicense != null); //thisLicense is always null
}
public void Dispose()
{
if (thisLicense!=null)
{
thisLicense.Dispose();
thisLicense = null;
}
}
}
(+ other irrelevant methods), using:
internal class ModuleFlexFeature
{
public ModuleFlexFeature() { }
public string FeatureName { get { return "myFlexLMFeature"; } }
public float FeatureVersion { get { return 2016.0f; } }
}
Using the LMTOOLS by Flexera, I can get the License Server Status (I am running on 7507@mypcname, 0 out of 1 license for myFlexLMFeature is used).
Then I can add 7507@mypcname in the extra servers to be used by the third party app, but:
I've tried to use
LicenseManager.IsValid(typeof(ModuleFlexFeature),new ModuleFlexFeature(), out thisLicense);
but both have similar results (code seems to work but thisLicense is null)
Am I doing anything wrong ? is LicenseManager compatible with FlexLM ? or is there a bug on the third-party app which runs my plug-in (somehow does not connect to the license server properly)? How to check?
Thanks
Upvotes: 0
Views: 1371
Reputation: 21
OK, After some more investigation:
You cannot use a LicFileProvider to read FlexLMFiles
You have to create your own LicenseProvider and implement getLicense. To do this, you have to know where the file is / may be, and use lmutil. So first, you need to check if a license is available.
Inspired from this previous question, I have been able to get the following code to check if a license is valid or not (and detect, when several lm servers are used, which one to use):
//get the potential file candidates
string file = Environment.GetEnvironmentVariable("LM_LICENSE_FILE");
List<string> candidates = new List<string>(file.Split(';'));
foreach (string filecan in candidates)
{
//read the lm stats for this
string args = "lmstat -f " + MyFeatureName + " -c " + file;
ProcessStartInfo info = new ProcessStartInfo(@"lmutil.exe", args);
Process lmutil = Process.Start(info);
string output = lmutil.StandardOutput.ReadToEnd();
//now get the line
string matchPattern = @"Users of (\w+):.*Total of (\d+) license.*Total of (\d+) license";
MatchCollection matches = Regex.Matches(output, matchPattern);
foreach(Match m in matches)
{
//are only returned: m.Succes = true and m.Groups[1].Value = MyFeatureName
int value;
int total = Int32.TryParse(m.Groups[2].Value, out value) ? value : 0;
int used = Int32.TryParse(m.Groups[3].Value, out value) ? value : 0;
if (total - used > 0) return true;
}
}
return false;
This works fine... BUT this does not generate a license (this only checks if I can reasonably hope for one or not).
I've investigated lmborrow but it does not seem to generate a token either. Do you have any idea?
Upvotes: 1