13 Replies Latest reply on Mar 2, 2011 7:27 AM by mans.joling

    get automation license in c#

    andrew.french

      this is how I used to do it in vb.net:

       

      Private Function MGCPCB_licenseDoc(ByVal docOBJ As MGCPCB.Document) As Boolean

       

      Dim licenseServer As Object

       

      Dim key As Long

       

      Dim licenseToken As Long

       

      If Not docOBJ Is Nothing Then

      key = docOBJ.Validate(0)

      licenseServer = CreateObject("MGCPCBAutomationLicensing.Application")

       

      If Not (licenseServer Is Nothing) Then

       

      Try

      licenseToken = licenseServer.GetToken(key)

      docOBJ.Validate(licenseToken)

      MGCPCB_licenseDoc =True

       

      Catch

      MsgBox("Unable to obtain automation license")

      MGCPCB_licenseDoc =False

       

      End Try

       

      End If

      licenseServer =Nothing

       

      End If

       

      End Function

       

      This is what I've got in C# so far:

      public

       

       

      bool getAutomationLic()

      {

       

       

      object licenseServer;

       

       

      long key;

       

       

      long licenseToken;

      key = ExpDoc.Validate(0);

      licenseServer = Microsoft.VisualBasic.

       

      Interaction.CreateObject("MGCPCBAutomationLicensing.Application");

       

       

      if (licenseServer != null)

      {

       

       

      try

      {

      licenseToken = licenseServer.GetToken(key);

      ExpDoc.Validate((

       

      int)licenseToken);

      }

       

       

      catch { return false; }

       

       

      return true;

      }

      }

       

      The issue I am having is with the GetToken command, it doesn't seem to exist anymore or its not available in C#.  I think one way to solve this is if I used the correct type for the variable licenseServer.  What file do I need to reference to get access to the MGCPCBAutomationLicensing Data Model?  Anyway, I might not be asking the right question so if you know how to license automation in c# please share!

        • 1. Re: get automation license in c#
          fagrondin

          Hey Andrew,

           

          I'm using C# and  this is how I do it for the license server :

           

          key = docObj.Validate(0);

           

          MGCPCBAutomationLicensing.Application licenseServer = new MGCPCBAutomationLicensing.Application();

          licenseToken = licenseServer.GetToken(key);

          licenseServer =null;

           

          try

          {

          docObj.Validate(licenseToken);

          }

          catch

          { }

           

          It is not the complete function but if should make it works in your case.

           

          I hope it helps.Good Luck.

           

          Regards

          Fred

           

          • 2. Re: get automation license in c#
            andrew.french

            Hey Fred,

             

            What reference are you using to gain access to the MGCPCBAutomationLicensing name space?  For example, to use the MGCPCB name space you need to add a reference to the ExpeditionPCB.exe file. Thanks again!

             

            Andrew

            • 3. Re: get automation license in c#
              fagrondin

              Hey Andrew,

              no problem, I got through this too.

              I'm using visual Studio and my references are called through the Mentor Graphics COM objects.

               

              When you are in the project :

              References > Add reference > COM > "navigate to" MGCPCBAutomationLicensing > OK

              It should all show-up.

               

              Bonne Chance

              Fred

              1 of 1 people found this helpful
              • 4. Re: get automation license in c#
                andrew.french

                Well would you look at that, learn something new everyday!  Thank you for your assistance Fred!

                • 5. Re: get automation license in c#
                  fagrondin

                  No Problem.

                  As you can see it opens a lot of doors...

                  Fred

                  • 6. Re: get automation license in c#
                    mans.joling

                    Hello,

                     

                    I get this not running I am a newbie to C#

                    He gives a exception on : key = pcbDoc.Validate(0);

                    What I am doing wrong?

                     

                    Mans

                     

                        public class Automation_Module
                        {
                            // Expedition Automation Globals
                            public static MGCPCB.Application pcbApp;
                            public static MGCPCB.Document pcbDoc;

                            public static void MGCPCB_Connect()
                            {
                                // connects to MGCPCB
                                int key;
                                int licenseToken;

                                key = pcbDoc.Validate(0);
                                MGCPCBAutomationLicensing.Application licenseServer = new MGCPCBAutomationLicensing.Application();
                                licenseToken = licenseServer.GetToken(key);
                                licenseServer = null;
                                try
                                {
                                    pcbDoc.Validate(licenseToken);
                                }
                                catch
                                {
                                    MessageBox.Show("MGCPCB needs to be running!");
                                }
                            }
                    }
                    • 7. Re: get automation license in c#
                      fagrondin

                      Hey Mans,

                       

                      I think the problem is your pcbDoc declaration. Have you try adding break points to see if pcbDoc isn't NULL ?

                      What's the transcript of the error message ? How do you call the function ?

                      Regards

                      Fred

                      • 8. Re: get automation license in c#
                        andrew.french

                        Mans,

                         

                        I'm going to assume you have the correct references covered earlier in this thread.  Are you initializing pcbApp and pcbDoc prior to checking for the automation license?  I don't know if its the best way to do this but there is no createObject or getObject in C# so to initialize pcbApp try this:

                         

                        pcbApp = (MGCPCB.Application)System.Runtime.InteropServices.Marshal.GetActiveObject("MGCPCB.Application");

                         

                        and then to initialize pcbDoc:

                         

                        pcbDoc = pcbApp.ActiveDocument;

                         

                        I created a class that takes care of initialization and licensing, each item, getting the application, document, and license all have their own boolean method which reports back if it was able to get a handle on the item.

                         

                        Once you've initialized those variables try licensing automation, I think that should fix your issue.  If not post the exception that you are receiving and that should hopefully help find the root problem.

                         

                         

                         

                         

                         

                         

                        • 9. Re: get automation license in c#
                          mans.joling

                          Hi Andrew,

                           

                          I added the 2 lines as you discribe and get now a : "ComException was unhandeld" on this line :

                          MGCPCBAutomationLicensing.Application licenseServer = new MGCPCBAutomationLicensing.Application();

                           

                           

                          Retrieving the COM class factory for component with CLSID {87F05DE4-AABE-4C41-9B45-4BF08E47DEB4} failed due to the following error: 80040154 Class not registered (Exception from HRESULT: 0x80040154 (REGDB_E_CLASSNOTREG)).

                           

                          Any Idea?

                           

                          Mans

                          • 10. Re: get automation license in c#
                            andrew.french

                            Not terribly sure here, try verifying your platform target is x86 and not any cpu or x64. Its under the "Build" tab in the properties window in visual studio 2010.  I did a google search on your error and most topics seemed to be related to this.  I had to do this at a previous job, all my code would fail fantastically on the x64 machines, then I changed the target platform for my applications to x86 and they started working on the x64 machines just fine.

                             

                            If that doesn't work try uploading your project so we can look at it directly and maybe we can get this working for you.

                            • 11. Re: get automation license in c#
                              mans.joling

                              Hi Andrew,

                               

                              It works now thank you for your help

                               

                              Regards

                               

                              Mans

                              • 12. Re: get automation license in c#
                                andrew.french

                                Glad to hear it!  Out of curiosity was your issue with the target platform or something else?

                                • 13. Re: get automation license in c#
                                  mans.joling

                                  Hello Andrew,

                                   

                                  It was a target platform issue

                                   

                                  Mans