Documents.open() Causing Error: Attempted To Read Or Write Protected Memory.
I get this error when it tries to open the label file with Engine.Documents.Open(). However, on the same machine, I am able to open the label file in BarTender and print without any issues. Also, I had this program running on a dev machine and it worked there, the difference between the machines is one is a Windows Server 2003 and the machine that has the error is Windows Server 2008.
Error Message:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Stack Trace:
at BarTender.FormatClass.get_PrintPreview()
at Seagull.BarTender.Print.PrintPreview..ctor(Format format, VisibleWindows visibleWindows)
at Seagull.BarTender.Print.LabelFormat..ctor(Format document, VisibleWindows visibleWindows)
at Seagull.BarTender.Print.LabelFormatDocument..ctor(Format format, Engine engine)
at Seagull.BarTender.Print.Documents.Open(String fileName, String printerName)
at ArchestrA.Toolkit.PS2LabelPrintQueueBT.PS2LabelPrintQueueBTRuntime.BTPrint(String s_label_app_path, String s_printer, String s_print_qty, String[] sa_variable_value, String[] sa_variable_name, String[] sa_graphic_value, String[] sa_graphic_name)
BarTender:
Edition: Enterprise Automation
Version: 10.1 SR2
Build: 2945
Windows Server 2008 R2 Standard (x64) SP1
Print Driver: Zebra 105SL (203 dpi)
Code:
private void BTPrint(string s_label_app_path, string s_printer, string s_print_qty, string[] sa_variable_value, string[] sa_variable_name, string[] sa_graphic_value, string[] sa_graphic_name) { try { LabelFormatDocument btFormat = m_btEng.Documents.Open(s_label_app_path, s_printer); //Populate label vars from arrays if (sa_variable_name != null && btFormat.SubStrings.Count > 0) { for (int i = 0; i < sa_variable_name.Length; i++) { if (btFormat.SubStrings.XML.ToString().Contains(sa_variable_name[i]) && btFormat.SubStrings[sa_variable_name[i]].Type == SubstringType.ScreenData) { btFormat.SubStrings[sa_variable_name[i]].Value = sa_variable_value[i]; } else { LogWarning(Tagname + ".BTPrint: " + sa_variable_name[i] + " not found in " + s_label_app_path + " or it is the wrong type."); } } } btFormat.PrintSetup.IdenticalCopiesOfLabel = Convert.ToInt32(s_print_qty); Result res = btFormat.Print(); switch (res) { case Result.Failure: throw new Exception("The print failed."); case Result.Timeout: throw new Exception("The print timed out."); default: //Success break; } btFormat.Close(SaveOptions.DoNotSaveChanges); } catch (Exception ex) { ErrorFlag.Value = true; ErrorMessage.Value = Tagname + ".BTPrint: " + ex.Message; LogError(Tagname + ".BTPrint: " + ex.Message + " Stack Trace: " + ex.StackTrace); } }
-
Fernando Ramos Miracle
★ BarTender Hero ★
Hello Josh,
1. Could you let me know if your code is running as an application or as a service? If the latter, under what type of user account?
2. Could you let me know the exact path where your documents are stored? Are they saved locally or on a networked folder?
If you test with a locally stored document, do you get the same issue?
Regards.
0 -
Legacy Poster
★ BarTender Hero ★
Fernando,
Thank you for your response.
1. The code is running as a service. And the user it runs under is an Administrator.
2. The path is local its C:\Labels\. I've also made sure none of the files are read only.
Additionally, I hadn't thought about the user that the service is running under, so I logged in with that user to the machine and opened bartender. From there I opened the label file and I was able to successfully print.
0 -
Fernando Ramos Miracle
★ BarTender Hero ★
Hello Josh,
Could you verify that the path to the labels and the printer name are correctly passed to the BTPrint method? Could it be that BarTender is trying to open a document with an incorrect path or using a non-existing printer?
*I'm understanding from your error report that the code is stumbling in the first command of your code: LabelFormatDocument btFormat = m_btEng.Documents.Open(s_label_app_path, s_printer);
If that is not the case, please let me know.
Regards.
0 -
Legacy Poster
★ BarTender Hero ★
Fernando,
The path is correct, because when I pass in a path that doesn't exist I get this error: "The document could not be found. Try checking the spelling of the file name or try another file name." Also, I did set the printer to a name that didn't exist and it gave the "Attempted to read or write protected memory." So not much was changing. Also yes it is occurring at first command.
However, I have good and bad news. Good news is that it works now. The bad news is that I have no way to reproduce it, and I have a feeling it's a product of Windows failing to manage memory (which is surprising for Server 2008). Everything started working after I made a few User Account Settings for the user that was running the service. This required a restart. When the computer returned, I ran my code and it worked. So to confirm that the changes I made to security were the issue, I returned them to the original settings hoping to cause the error again. Yet, the code worked. So the only thing I can think of is that restarting the computer fixed it (i.e. cleaned up memory artifacts). This is troublesome, but I'm moving forward again.
Sorry for any confusion this may have caused. This was an unfortunate circumstance and doesn't yield an answer for either of us. I appreciate your quick responses greatly.
Take care.
0 -
Fernando Ramos Miracle
★ BarTender Hero ★
Hello Josh,
Many thanks for your feedback; I'm glad you were able to solve your issue (that is indeed the important part).
Regarding a possible memory leak please note that since v10.0 applications based on the BarTender Print Server SDK now benefit from a number of performance enhancements. This includes the same stability improvements that Commander received as part of BarTender 9.4 SR3. It's possible that you might be able to benefit of this enhancements to prevent this sort of issues.
New capabilities include:
- Restarting the BarTender process if it becomes unresponsive.- Automatically restarting the BarTender process on a schedule.- Document caching.The new properties and methods are summarized here. (For more detail, please consult the SDK documentation.)- IsResponsive property added to Engine and TaskEngine classes.- ResponsiveTimeout property added to Engine, TaskEngine, and TaskEngines classes.- RestartSchedule and StartupCommand properties added to TaskManager class.- Additional Start() method added to TaskManager class in support of document caching features.- CloseFormatAfterCompletion property added to all tasks that open BarTender documents.- Priority property added to Task class.Regards0
請登入寫評論。
評論
5 條評論