获取错误:尝试读取或写入受保护的内存。这通常表明其他内存已损坏 [英] Getting Error: attempted to read or write protected memory. this is often an indication that other memory is corrupt

查看:82
本文介绍了获取错误:尝试读取或写入受保护的内存。这通常表明其他内存已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好



我正在尝试通过我的应用程序扫描条形码。



以下是代码



Hi all

I am trying to scan barcode through my application.

Below is the code

using TestClass;

namespace scanDemo
{
    public partial class Form1 : Form
    {
      TestClass.TestClass test = new TestClass.TestClass();
            
try
            {

                string reply = "";
                textBox1.Text = "";
                textBox1.Refresh();
                reply = test.Scan();
                textBox1.Text = reply;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }





扫描功能低于





scan function is below

[DllImport(@"C:\Documents and Settings\Administrator\My Documents\visual studio 2010\Projects\TestClass\TestClass\PLQ20W32.dll")]
       private static extern UInt16 PrtBarRead(StringBuilder data, out UInt16 len, out UInt16 direction);

       [DllImport(@"C:\Documents and Settings\Administrator\My Documents\visual studio 2010\Projects\TestClass\TestClass\PLQ20W32.dll")]
       private static extern string PortOpen();

       [DllImport(@"C:\Documents and Settings\Administrator\My Documents\visual studio 2010\Projects\TestClass\TestClass\PLQ20W32.dll")]
       private static extern string PrinterStatus();

       [DllImport(@"C:\Documents and Settings\Administrator\My Documents\visual studio 2010\Projects\TestClass\TestClass\PLQ20W32.dll")]
       private static extern string Auto_Start();

       [DllImport(@"C:\Documents and Settings\Administrator\My Documents\visual studio 2010\Projects\TestClass\TestClass\PLQ20W32.dll")]
       private static extern ushort BarcodeLength();

       [DllImport(@"C:\Documents and Settings\Administrator\My Documents\visual studio 2010\Projects\TestClass\TestClass\PLQ20W32.dll")]
       private static extern string PaperStatus();

       [DllImport(@"C:\Documents and Settings\Administrator\My Documents\visual studio 2010\Projects\TestClass\TestClass\PLQ20W32.dll")]
       private static extern string WriteToTrace();



       public  string Scan()
       {

           UInt16 status = 100;
           string reply = "";
           UInt16 length = 255;
           UInt16 direction = 0;

           try
           {
               //CODE TO CHECK CONFIG:
               //readxml();

               if (Auto_Start() == "Do Not Start")
               {
                   reply = "DEVICE NOT CONFIGURED";
               }
               else
               {
                   //Open Port

                   reply = PortOpen();

                   if (reply == "PORT IS ALREADY OPENED" || reply == "PORT IS OPENED")
                   {

                       //PRINTER STATUS

                       reply = PrinterStatus();

                       if (reply == "ONLINE")
                       {


                           //PAPER STATUS
                           //UInt16 r = 0;
                           reply = PaperStatus();


                           //SCAN CMD
                           if (reply == "PAPER ALIGNED")
                           {

                               StringBuilder buffer = new StringBuilder(new String(' ', 256));

                               status = PrtBarRead(buffer, out length, out direction);


                               switch (status)
                               {
                                   case 0:
                                       reply = buffer.ToString();
                                       break;
                                   case 10:
                                       reply = "PORT IS NOT OPENED YET";
                                       break;
                                   case 20:
                                       reply = "INVALID PARAMETERS";
                                       break;
                                   case 32:
                                       reply = "COVER OPEN";
                                       break;
                                   case 52:
                                       reply = "NO BARCODE DATA FOUND";
                                       break;
                                   default:
                                       reply = "UNKNOWN ERROR";
                                       break;
                               }

                               //CODE FOR VALIDATION OF DIRECTION AND LENGTH

                               if (!(buffer.ToString()).Contains("' '"))
                               {
                                   if (reply == "NO BARCODE DATA FOUND" || reply == "INVALID PARAMETERS" || reply == "PORT IS NOT OPENED YET")
                                   {
                                      // Trace.WriteToTrace("EXIT from Scan(): " + reply, 1);
                                       return reply;
                                   }
                                   if (BarcodeLength() != length)
                                   {
                                       reply = "BARCODE LENGTH DOES NOT MATCH";
                                   }
                                   switch (direction)
                                   {
                                       case 1:
                                           reply = reply + "|" + "0";
                                           break;
                                       case 2:
                                           reply = reply + "|" + "90";
                                           break;
                                       case 3:
                                           reply = reply + "|" + "180";
                                           break;
                                       case 4:
                                           reply = reply + "|" + "270";
                                           break;
                                       default:
                                           reply = "UNKNOWN ERROR";
                                           break;

                                   }
                                   //if (ReadingPosition == "Horizontal")
                                   //{
                                   //    if (direction == 2 || direction == 4)
                                   //        reply = "BARCODE DIRECTION DOES NOT MATCH";
                                   //}
                                   //if (ReadingPosition == "Vertical")
                                   //{
                                   //    if (direction == 1 || direction == 3)
                                   //        reply = "BARCODE DIRECTION DOES NOT MATCH";
                                   //}

                               }
                           }
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               reply = ex.Message;
         //      trace.WriteToTrace("EXCEPTION in Scan():  " + reply + "status: " + status, 1);
               return reply;

           }
         //  trace.WriteToTrace("EXIT from Scan(): " + reply + "status: " + status, 1);
           return reply;
       }



扫描方法是用dll编写的。我指的是。

请告诉我我做错了什么..


The scan method is written in a dll..i am referring that.
Please tell me what i am doing wrong..

推荐答案

谁编写了 DllImport 语句?原始C / C ++ dll中的相应函数如何?

我怀疑使用

Who wrote the DllImport statements? How do the corresponding functions in the original C/C++ dll look like?
I'd suspect that with
private static extern UInt16 PrtBarRead(StringBuilder data, out UInt16 len, out UInt16 direction)



需要更多项目,例如:

- CharSet

- CallingConvention

- MarshalAs


some more items are required, e.g.:
- CharSet
- CallingConvention
- MarshalAs


这篇关于获取错误:尝试读取或写入受保护的内存。这通常表明其他内存已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆