Announcement

Collapse

Looking for a User App or Add-On built by the NinjaTrader community?

Visit NinjaTrader EcoSystem and our free User App Share!

Have a question for the NinjaScript developer community? Open a new thread in our NinjaScript File Sharing Discussion Forum!
See more
See less

Partner 728x90

Collapse

Code snipet to check License Key

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Code snipet to check License Key

    I'm about to distribute an indicator I developed to be tested by few people.

    Of course, I don't want this indicator to be distributed beyond the selected testers.

    I would think many people have coded the "license key" check, but I could not find any examples in this forum.

    Does someone has such code sample?

    Thanks!

    PS: I did read through the distribution sections of the manual, and know how to export, etc.. I was just trying to get this piece of code instead of reinventing the wheel

    #2
    Originally posted by ds1111 View Post
    I'm about to distribute an indicator I developed to be tested by few people.

    Of course, I don't want this indicator to be distributed beyond the selected testers.

    I would think many people have coded the "license key" check, but I could not find any examples in this forum.

    Does someone has such code sample?

    Thanks!

    PS: I did read through the distribution sections of the manual, and know how to export, etc.. I was just trying to get this piece of code instead of reinventing the wheel
    ref: http://www.ninjatrader.com/support/f...ight=machineid

    Comment


      #3
      Thanks Koganam.

      This is the code I saw in the link:
      if ( Cbi.License.MachineId != <my machine id>) {
      return;
      }

      That would means that I would have a if statement for each person, and would have to 'recompile' and distribute again for every individual person... I thought that would have an easier way to accomplish this.... how about if I have 50 people?

      Any ideas to make this easier?

      Comment


        #4
        Originally posted by ds1111 View Post
        Thanks Koganam.

        This is the code I saw in the link:
        if ( Cbi.License.MachineId != <my machine id>) {
        return;
        }

        That would means that I would have a if statement for each person, and would have to 'recompile' and distribute again for every individual person... I thought that would have an easier way to accomplish this.... how about if I have 50 people?

        Any ideas to make this easier?
        Not really. You want to tie the code to specific machines. You can do it individually, or else have a file with the list of authorized machine ID's, and compare against the list. That means, of course, that anyone can edit the list to add their machine as authorized. At best it is "security by obscurity", and not very good even at that.

        The alternative is to hook up with any of those companies which run licensing servers, sign up, get their API code, and go from there. NT does have a license server, but it is only available to their official partners.

        Comment


          #5
          ds1111,

          Before becoming a partner, I handled this by adding a static class that had an ArrayList of hard coded machine IDs and a static bool method that takes a string argument to check the ArrayList.

          Code:
          internal static class Licenses
          {
          private static readonly ArrayList licArray = new ArrayList{"MachineID1", "MachineID2", "etc"}
          public static bool CheckLicense(string id)
          {
          if (licArray.Contains(id))
          return true;
          else return false;
          }
          }
          then in the indicator/strategy OnStartUp() method:
          Code:
          if (Licenses.CheckLicense(NinjaTrader.Cbi.License.MachineId) == false)
          {
          System.Windows.Forms.MessageBox.Show("Your machine is not licensed to use this software. \n Please remove it from the chart's indicator list");
          return;
          }
          Then you just include that class when you export the NinjaScript file. You have to add the machine IDs manually to the ArrayList, but it's relatively easy to maintain especially on future builds.

          Hope this helps.

          VT

          Comment


            #6
            Thanks a million! Exactly what I needed. Thanks for putting the code here also!

            Comment


              #7
              You're welcome BUT!,

              After second thought, I realized that it worked for my implementation, but perhaps not in all.

              For how I used it, a "return" from OnStartUp() worked fine, but under other circumstances it might not work. Instead of "return" you could have it set a bool variable and then check that variable in OnBarUpdate().

              My usage loaded stuff in OnStartUp() and OnBarUpdate() was essentially empty. The code I posted above will still allow OnBarUpdate() to be called and run. But if you set a flag/bool in OnStartUp() and check it in OnBarUpdate(), it should prevent/block the code in OBU from running.

              I hope that makes sense. Sorry for the confusion.

              VT
              Last edited by VTtrader; 03-04-2012, 10:12 PM.

              Comment


                #8
                This is my final code (seems to be working). I use the License.Id instead of the License.MachineID, so it will run for that user license and not for the user specific machine (seems to make more sense to me).
                I put the code below for others to use.

                Thanks again!

                // In the Initialize() , I use the Id (license key) and not the MachineID
                if (Licenses.CheckLicense(NinjaTrader.Cbi.License.Id) == false)
                {
                System.Windows.Forms.MessageBox.Show(
                "Your machine is not licensed to use this software. \n Please remove it from the chart's indicator list");
                return;
                }

                *******************
                After the OnBarUpdate code:

                internal static class Licenses
                {
                private static readonly ArrayList licArray = new ArrayList{"ID1", "ID2", "etc"};

                public static bool CheckLicense(string id)
                {
                if (licArray.Contains(id))
                return true;
                else return false;
                }
                }

                Comment


                  #9
                  Hi ds1111,

                  You would want to test it, see my follow-up post, "return" probably will not prevent the OnBarUpdate() code from running. Instead of "return", you'll probably want to use a bool variable that you check in OnBarUpdate().

                  Code:
                  if (Licenses.CheckLicense(NinjaTrader.Cbi.License.MachineId) == false)
                  {
                  System.Windows.Forms.MessageBox.Show("Your machine is not licensed to use this software. \n Please remove it from the chart's indicator list");
                  licenseFailed = true;
                  }
                  Then in OnBarUpdate():
                  if (licenseFailed) return;

                  Or something along those lines.

                  VT

                  Comment

                  Latest Posts

                  Collapse

                  Topics Statistics Last Post
                  Started by DJ888, 04-16-2024, 06:09 PM
                  4 responses
                  12 views
                  0 likes
                  Last Post DJ888
                  by DJ888
                   
                  Started by terofs, Today, 04:18 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post terofs
                  by terofs
                   
                  Started by nandhumca, Today, 03:41 PM
                  0 responses
                  5 views
                  0 likes
                  Last Post nandhumca  
                  Started by The_Sec, Today, 03:37 PM
                  0 responses
                  3 views
                  0 likes
                  Last Post The_Sec
                  by The_Sec
                   
                  Started by GwFutures1988, Today, 02:48 PM
                  1 response
                  9 views
                  0 likes
                  Last Post NinjaTrader_Clayton  
                  Working...
                  X