3 Replies Latest reply on Nov 13, 2015 3:32 PM by Jacob Corder

    access violation exception

    James Mikel

      Hello,

      I am getting the following in the application events viewer.   the application accesses swCustomPropertyManager.GetNames() method without throwing an error...Not sure why the Get3-5 methods are causing the error below.  Please help.

       

      ...

      Exception Info: System.AccessViolationException

      Stack:

         at SldWorks.CustomPropertyManagerClass.Get3(System.String, Boolean, System.String ByRef, System.String ByRef)

      ...

       

      This is a standalone windows application that that uses the .interop.sldworks.dll and interop.swconst.dll to open selected files and extract the custom and config specific properties.

      I only get the error when running on the clients' system.

      Client's system is Windows 7.  Solidworks 2015.

      I have tried Get3, Get4 and Get5 all with the same results.  Any help would be greatly appreciated.

       

              public bool Export(String swFile)

              {

                  var success = true;

                  ErrorMsg = String.Empty;

                  swApp = new SldWorks();

       

       

                  //MessageBox.Show("before open doc");

                  swModel = swApp.OpenDoc6(swFile, p, s, "", ref nErrors, ref nWarnings);

                  //MessageBox.Show("after open doc");

                  if (swModel == null)

                  {

                      ErrorMsg = "Model did not load";

                      swApp.SendMsgToUser2("Assembly not loaded", stop, ok);

                      swModel = null;

                      swApp = null;

                      RecordError(ErrorMsg);

                      success = false;

                  }

                  if (swModel.GetType() != p)

                  {

                      ErrorMsg = "Selected SLDPRT file is not a part doc.";

                      swApp.SendMsgToUser2("Assembly not loaded", stop, ok);

                      swModel = null;

                      swApp = null;

                      RecordError(ErrorMsg);

                      success = false;

                  }

                  //MessageBox.Show("before rebuild");

                  swModel.Rebuild(rebuild);

                  //MessageBox.Show("after rebuild");

       

       

                  try

                  {

                      //MessageBox.Show("before save rebuild");

                      swModel.Save();

                      //MessageBox.Show("after save rebuild");

                  }

                  catch (Exception ex)

                  {

                      RecordError(ex.Message.ToString());

                      success = false;

                  }

                  cInfo = new Custom();

                  //MessageBox.Show("Model: " + swModel.GetTitle().Trim());

       

       

                  var title = swModel.GetTitle().Trim();

                  if (title.Length > 7)

                  {

                      if (title.EndsWith(".SLDPRT") || title.EndsWith(".SLDDRW"))

                      {

                          cInfo.DrawNo = title.Replace(".SLDDRW", "").Replace(".SLDPRT", "");

                      }

                      else

                      {

                          cInfo.DrawNo = title.Substring(1, 4);

                          //sDrawno = Mid(swModel.GetTitle, 1, InStr(Mid(swModel.GetTitle, 4, swModel.GetTitle.Length), "-") + 2)

                      }

                  }

                  else

                  {

                      cInfo.DrawNo = title;

                  }

       

       

                  try

                  {

                      //MessageBox.Show("before GetCustomInfoNames");

       

       

                      var cfm = (String[])swModel.ConfigurationManager.Document.GetCustomInfoNames();

                      //MessageBox.Show("cfm count: " + cfm.Count().ToString());

       

       

                      foreach (var ci in cfm)

                      {

                          switch (ci)

                          {

                              case "PROJECT NAME":

                                  cInfo.ProjectName = swModel.GetCustomInfoValue("", ci);

                                  break;

                              case "PROJECT NUMBER":

                                  cInfo.ProjectNumber = swModel.GetCustomInfoValue("", ci);

                                  break;

                              case "COLOR":

                                  cInfo.Color = swModel.GetCustomInfoValue("", ci);

                                  break;

                              case "PRODUCT NUMBER":

                                  cInfo.ProductNumber = swModel.GetCustomInfoValue("", ci);

                                  break;

                              default:

                                  break;

                          }

                      }

                  }

                  catch (Exception ex)

                  {

                      //swApp.CloseDoc(swFile);

                      ErrorMsg = swFile + " custom info caused: " + ex.Message.ToString();

                      RecordError(ErrorMsg);

                      success = false;

                  }

                  finally

                  {

                      swApp.CloseDoc(swFile);

                  }

       

       

                  lCS = new lConfigSpecific();

                  try

                  {

                      //MessageBox.Show("before ConfigSpecific Open Doc");

       

       

                      swModel = swApp.OpenDoc6(swFile, 1, 1, "", ref nErrors, ref nWarnings);

                      //MessageBox.Show("after Open doc");

       

       

                      swApp.SetCurrentWorkingDirectory(swModel.GetPathName().Substring(0, swModel.GetPathName().LastIndexOf("\\")));

                      //MessageBox.Show("after SetCurrentWorkingDirectory");

       

       

                      var names = (String[])swModel.GetConfigurationNames();

                      foreach (var name in names)

                      {

                          //MessageBox.Show("before Extension");

       

       

                          swModelDocExt = swModel.Extension;

                          //MessageBox.Show("after extension");

       

       

                          swCustomPropertyManager = swModelDocExt.CustomPropertyManager[name];

                          //MessageBox.Show("after CustomProperty");

                          //MessageBox.Show(name + " : " + swCustomPropertyManager.Count.ToString());

       

       

                          var ncs = new ConfigSpecific { Config = name };

                          var customProps = (String[])swCustomPropertyManager.GetNames();

                          //MessageBox.Show("after Custom Property GetNames");

       

       

                          foreach (string cp in customProps)

                          {

                              //MessageBox.Show("Inside Custom Props: " + cp);

       

       

                              String val;

                              String valout;

                              //bool resolved;

                              bool UpToDate;

                              switch (cp)

                              {

                                  case "CONFIG WEIGHT":

                                      UpToDate = swCustomPropertyManager.Get3(cp, false, out val, out valout);

                                      MessageBox.Show(valout);

                                      //status = swCustomPropertyManager.Get4(cp, false, out val, out valout);

                                      ncs.ConfigWeight = Decimal.Parse(valout);

                                      break;

                                  case "CONFIG VOLUME":

                                      UpToDate = swCustomPropertyManager.Get3(cp, false, out val, out valout);

                                      ncs.ConfigVolume = Double.Parse(valout);

                                      break;

                                  case "QTY":

                                      UpToDate = swCustomPropertyManager.Get3(cp, false, out val, out valout);

                                      ncs.Qty = valout.ToUpper() == "TBD" ? 0 : Int32.Parse(valout);

                                      break;

                                  case "LOCATION":

                                      UpToDate = swCustomPropertyManager.Get3(cp, false, out val, out valout);

                                      ncs.Location = valout;

                                      break;

                                  case "SAW CUT":

                                      UpToDate = swCustomPropertyManager.Get3(cp, false, out val, out valout);

                                      ncs.SawCut = valout.ToUpper() != "NO";

                                      break;

                                  default:

                                      var donothing = String.Empty;

                                      //RecordError("Custom Property not handled: " + cp + " Drawing: " + swFile);

                                      break;

                          }

                          }

                          lCS.Add(ncs);

                      }

                      swApp.CloseDoc(swFile);

       

       

                  }

                  catch (Exception ex)

                  {

                      ErrorMsg = swFile + " config specifics caused: " + ex.Message.ToString();

                      swApp.CloseDoc(swFile);

                      RecordError(ex.Message.ToString());

                      success = false;

                  }

                  return success;

              }

       

       

          }

        • Re: access violation exception
          Jacob Corder

          i think you need to set the solidworks session to visible. By declaring ISwapp as new Sldworks it is making the program NOT Visible.

           

          Set ISwapp.Visible = true and that should fix it.

           

          Some api calls cannot be done when solidworks is not visible.

           

          An alternate would be to use  the solidworks Document Manager API to read the custom properties.. its much faster and no issues like you are having

            • Re: access violation exception
              James Mikel

              Jacob,   do the API calls work or not work on various versions of OS?    I have had the application work fine on my development systems, Win 8,2.  On another laptop that has Win 10.  But not on the client's Win 7 64 bit system. 

               

              I will try the document manager API. Sounds promising.

                • Re: access violation exception
                  Jacob Corder

                  now that is a great question.

                  solidworks 2015 sp4 can be used in windows 10.

                  if you are deploying with dlls for Service Pack 3 or earlier, i would assume there would be some problems.

                   

                  the document manager api requires subscription i believe and a license. you get that from solidworks.

                   

                  i have used the Document Manager ApI to search thousands of files for properties and it executes VERY VERY FAST!!.