473,324 Members | 2,179 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

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.AccessViolationException

Here is part of my code:

Expand|Select|Wrap|Line Numbers
  1. struct TPLU
  2.         {
  3.             public string Name;
  4.             public int LFCode;
  5.             public string Code;
  6.             public int BarCode;
  7.             public int UnitPrice;
  8.             public int WeightUnit;  //4 Kg, 9 PCS
  9.             public int Deptment;
  10.             public double Tare;
  11.             public int ShlefTime;
  12.             public int FixUnit;
  13.             public double FixWeight;
  14.             public int Tolerance;
  15.             public int Message1;
  16.             public int Message2;
  17.             public int MultiLabel;
  18.             public int Rebate;
  19.             public int Account;
  20.             public int IsLock;
  21.             public int Traceid;
  22.         }
  23.  
  24.         private TPLU[] PPLUCluster;
  25.  
  26.         private int[] predefinedHotkeys = new int[84]; //84 hotkeys on the scale. 0 to 83
  27.  
  28.         const string dllfile = @"PBusdrv.dll";
  29.  
  30.         [DllImport(dllfile)]
  31.         private static extern int PBusConnectEx(string RefLFZKFileName, string RefCFGFileName,string IPAddr);
  32.  
  33.         [DllImport(dllfile)]
  34.         private static extern int PBusDisConnectEx(string IPAddr);
  35.  
  36.         [DllImport(dllfile)]
  37.         private static extern int PBusTransferPLUCluster(TPLU[] PPC);
  38.  
  39.         [DllImport(dllfile)]
  40.         private static extern int PBusClearPLUData();
  41.  
  42.         [DllImport(dllfile)]
  43.         private static extern int PBusUpTr(string SerialNOStr, string AccountFile);
  44.  
  45.         [DllImport(dllfile)]
  46.         private static extern int PBusTransferHotkey(int[] HotkeyTable, int TableIndex);
  47.  
  48. and the part where the error comes out:
  49.   private void button12_Click(object sender, EventArgs e)
  50.         {
  51.             int d;
  52.             int index = 0;
  53.             string ipaddress = textBox1.Text.ToString();
  54.             PPLUCluster = new TPLU[2000];
  55.           //  IntPtr ptrcluster = Marshal.AllocHGlobal(Marshal.SizeOf(PPLUCluster));
  56.             d = PBusConnectEx("", "", ipaddress);
  57.            // Marshal.StructureToPtr(PPLUCluster, ptrcluster, true);
  58.             try
  59.             {
  60.                 if (d < 0)
  61.                 {
  62.                     MessageBox.Show("connection fail");
  63.                 }
  64.                 else
  65.                 {
  66.                     while (true)
  67.                     {
  68.                       //  int index = 0;
  69.                         d = PBusUpLoadPLU(index, PPLUCluster); //HERE IS WHERE FAILS
  70.                         if (d < 0)
  71.                         {
  72.                             MessageBox.Show("Upload plu fail");
  73.                             break;
  74.                         }
  75.                         if (d == 9) //Upload OK
  76.                         {
  77.                             MessageBox.Show("Upload OK");
  78.                             break;
  79.                         }
  80.                         index++;
  81.                     }
  82.                 }
  83.             }
  84.             finally
  85.             {
  86.                 PBusDisConnectEx(ipaddress);
  87.             }
  88.         }
  89.  
  90.     }
  91.  
  92. }
Aug 5 '15 #1
0 2072

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jeff Thur | last post by:
I am getting this error when trying to run a stored procedure: Microsoft.VisualBasic.CompilerServices.LateBinding.LateInd exGet(Object o, Object args, String paramnames) +1361...
1
by: Jenny C. | last post by:
Hi I am fairly new to .NET, and I am having a hard time to call a stored procedure from C# code in a windows application I have tested my connection (and login) several time and it is OK. If I...
0
by: banduraj | last post by:
I get an System.InvalidOperationException when trying to Serialize a struct that has a type refrenced from another library. The exception is: System.InvalidOperationException was unhandled...
1
by: =?Utf-8?B?SmFzb24gUm9kbWFu?= | last post by:
I have a wierd problem in my application where any call to Assembly.GetFullName of the currently running assembly fails with an AccessViolationException. This only happens on windows vista, all...
8
by: JonnyBlaze | last post by:
Hi Guys - This is my first C# app, but I have some knowledge of a bunch of languages like VB6 and PHP, so I'm picking it up rather quickly. I've been pulling my hair out on this one for about 4...
0
by: Dilip | last post by:
I have cross-posted this question to 3 newsgroups since I am not sure exactly where it belongs (and some NG's have very less activity). Apologies in advance for this. I have set the followup to...
5
by: SSekar | last post by:
Hi, I am getting this exceptions when I am trying to access the Frotran dll. Here is sample code for that. using System; using System.Collections.Generic; using System.Text; using...
3
by: maneshborase | last post by:
Hi friends, I am facing one serious problem in my application. I am trying to open dicom image file (.dcm) has size around 400 MB. But I am getting and unhandy exceptions, Some time, ...
8
by: Trupti Panchal | last post by:
Hi, My C# app throws the following exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. static...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.