System.AccessViolationException when trying to use a dll function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • alerico90
    New Member
    • Aug 2015
    • 1

    System.AccessViolationException when trying to use a dll function

    Hello Ihave a problem with the use of a single function of a dll, the rest of the functions works just fine but there is one that keeps giving me a System.AccessVi olationExceptio n

    Here is part of my code:

    Code:
    struct TPLU
            {
                public string Name;
                public int LFCode;
                public string Code;
                public int BarCode;
                public int UnitPrice;
                public int WeightUnit;  //4 Kg, 9 PCS
                public int Deptment;
                public double Tare;
                public int ShlefTime;
                public int FixUnit;
                public double FixWeight;
                public int Tolerance;
                public int Message1;
                public int Message2;
                public int MultiLabel;
                public int Rebate;
                public int Account;
                public int IsLock;
                public int Traceid;
            }
    
            private TPLU[] PPLUCluster;
    
            private int[] predefinedHotkeys = new int[84]; //84 hotkeys on the scale. 0 to 83
    
            const string dllfile = @"PBusdrv.dll";
    
            [DllImport(dllfile)]
            private static extern int PBusConnectEx(string RefLFZKFileName, string RefCFGFileName,string IPAddr);
    
            [DllImport(dllfile)]
            private static extern int PBusDisConnectEx(string IPAddr);
    
            [DllImport(dllfile)]
            private static extern int PBusTransferPLUCluster(TPLU[] PPC);
    
            [DllImport(dllfile)]
            private static extern int PBusClearPLUData();
    
            [DllImport(dllfile)]
            private static extern int PBusUpTr(string SerialNOStr, string AccountFile);
    
            [DllImport(dllfile)]
            private static extern int PBusTransferHotkey(int[] HotkeyTable, int TableIndex);
    
    and the part where the error comes out:
      private void button12_Click(object sender, EventArgs e)
            {
                int d;
                int index = 0;
                string ipaddress = textBox1.Text.ToString();
                PPLUCluster = new TPLU[2000];
              //  IntPtr ptrcluster = Marshal.AllocHGlobal(Marshal.SizeOf(PPLUCluster));
                d = PBusConnectEx("", "", ipaddress);
               // Marshal.StructureToPtr(PPLUCluster, ptrcluster, true);
                try
                {
                    if (d < 0)
                    {
                        MessageBox.Show("connection fail");
                    }
                    else
                    {
                        while (true)
                        {
                          //  int index = 0;
                            d = PBusUpLoadPLU(index, PPLUCluster); //HERE IS WHERE FAILS
                            if (d < 0)
                            {
                                MessageBox.Show("Upload plu fail");
                                break;
                            }
                            if (d == 9) //Upload OK
                            {
                                MessageBox.Show("Upload OK");
                                break;
                            }
                            index++;
                        }
                    }
                }
                finally
                {
                    PBusDisConnectEx(ipaddress);
                }
            }
    
        }
    
    }
    Last edited by Rabbit; Aug 5 '15, 04:26 PM. Reason: Please use [code] and [/code] tags when posting code or formatted data.
Working...