using System; using System.Text; using System.Runtime.InteropServices; using PersistentSecurity; //************************************************************************ //* //* Check out a "monitor" license, version "6.0" from the license server //* //* The server will have to be running and serving this license. //* //************************************************************************ class Floating { // Prototypes for callbacks for lost connection to license server public delegate int DelmyRecon(StringBuilder server,int retrycount, int retries); public delegate int DelmyReconCheckoutFail(StringBuilder server,StringBuilder component,StringBuilder version); public delegate int DelmyReconComplete(StringBuilder server,int retrycount); public delegate int DelmyReconExit(StringBuilder server,int retrycount); static void Main() { StringBuilder szMessage = new StringBuilder(1025); // make this application use a floating license server PSProlibProxy.sgSetAttrInt(PSProlibProxy.SG_ATTRTYPE.SG_ATTR_LICENSETYPE,(int)PSProlibProxy.SG_LICENSETYPE.SG_LICENSETYPE_FLOATING); // Set callbacks just in case we loose connection with the license server DelmyRecon Recon = myRecon; DelmyReconCheckoutFail ReconCheckoutFail = myReconCheckoutFail; DelmyReconComplete ReconComplete = myReconComplete; DelmyReconExit ReconExit = myReconExit; PSProlibProxy.sgSetAttrFnC(PSProlibProxy.SG_ATTRTYPE.SG_ATTR_VENDOR_RECONNECT,Recon); PSProlibProxy.sgSetAttrFnC(PSProlibProxy.SG_ATTRTYPE.SG_ATTR_VENDOR_RECONNECT_COMPLETE,ReconComplete); PSProlibProxy.sgSetAttrFnC(PSProlibProxy.SG_ATTRTYPE.SG_ATTR_VENDOR_RECONNECT_CHECKOUT_FAIL,ReconCheckoutFail); PSProlibProxy.sgSetAttrFnC(PSProlibProxy.SG_ATTRTYPE.SG_ATTR_VENDOR_RECONNECT_EXIT,ReconExit); if (PSProlibProxy.sgCheckout("monitor","6.0") != PSProlibProxy.SG_SUCCESS) { // tell the user they are not licensed and exit gracefully PSProlibProxy.sgGetLastErrorString(szMessage); Console.WriteLine("{0}",szMessage); Environment.Exit(1); } Console.WriteLine("Running the monitor application...."); Console.WriteLine("Press any key to release the license...."); Console.ReadKey(); // app is finished, check in the license. NOTE: it is not necessary to do this // as the license server will detect the process terminating and do it for you. PSProlibProxy.sgCheckin("monitor","6.0"); Environment.Exit(0); } // These are the callback functions for when the client (this program) looses connection // with the license server. See the "ProgrammerGuide.pdf" for more information. // This give you control over what to do when a connection is lost with the license server // They are not necessary. However, if you don't implement them, the default handlers behave // almost the same as these in this example. If you had a GUI program, you would want to // do something more than just calling fprintf() //********************************************************* //* attempt to reconnect //********************************************************* static int myRecon(StringBuilder server,int retrycount, int retries) { Console.WriteLine("myRecon: Lost connection with license server \"{0}\". Attempting to reconnect {1} of {2} ....", server,retrycount,retries); return(0); } //********************************************************* //* reconnected, but one or more of the licenses cannot be re-checked out //********************************************************* static int myReconCheckoutFail(StringBuilder server,StringBuilder component,StringBuilder version) { StringBuilder errstr = new StringBuilder(1024); Console.WriteLine("myRecon: While reconnecting to license server \"{0}\" unable to re-checkout the license for \"{1}\" version \"{2}\".", server,component,version); PSProlibProxy.sgGetLastErrorString(errstr); Console.WriteLine("for the following reason: {0}",errstr); Console.WriteLine("Disabling \"{0}\" functionality.",component); // TODO: Disable this license functionality in your application... return(0); } //********************************************************* //* called when successfully reconnected to the license server //********************************************************* static int myReconComplete(StringBuilder server,int retrycount) { Console.WriteLine("myRecon: Reconnected to license server \"{0}\" after {1} attempts ....",server,retrycount); return(0); } //********************************************************* //* bad news. could not reconnect to the license server //********************************************************* static int myReconExit(StringBuilder server,int retrycount) { Console.WriteLine("myRecon: Failed to reconnect to license server \"{0}\" after {1} attempts .... Exiting.\n",server,retrycount); // Do what you want here, but right now, there is no license server to talk to and no licenses are checked out // if you just return(0), the app will continue to run. return(-1); //Environment.Exit(-1); } }