We are developing one windows application where we are reading Hardware via Serial Port. Now we dont have Hardware so we are using simulator for testing. This simulator working fine with VB Test Application where VB application use one VC++ dll which read data from Simulator.
We are using same dll in C# windows application. This dll returns actually array of integer. While reading data from we are getting Syste.AccessViolation Exception.
This is the code which I used. I have decalred this in class.
To Open TCP Port:
[
static extern long MT_Open_TcpPort(ref long socketHandle, string remoteIP, long remotePort);
[
static extern long MT_Read_Data(long socketHandle, byte devID, int address, int qty, ref int[] hold_reg);
While Reading data:
To Open Port :
retval = MT_Open_TcpPort(
To Read Data:
retval = MT_Read_Input_Reg(sockethandle, 1, 100, 3,
For this statement, We are getting System.AccessViolation Exception:
Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
Any Help Appreciated.
Regards,
Vikram Patil
VulpesPosted Mar 4, 2012, 8:39 AM
Also, in the second function, the Integer parameters will all map to 'short' in C# as Integer in VB6 is a 2 byte integer.
Leaving aside the problem of how to get at the array which you say is returned by the second function, I'd see if you still get the AccessViolation exception if you use these revised signatures:
VikramPosted Mar 4, 2012, 8:23 AM
Actually we have one VB 6 Application in which I found only dll without header file. So I didn't get parameter of the functions.
This is code in VB 6 application :
Declare Function MT_Open_TcpPort Lib " MT_TCP_Data_ RTU.dll " (ByRef llSocketHandle As Long, ByVal lsRemoteIP As String, ByVal lnTcpPort As Long) As Long
Declare Function MT_Read_Data Lib " MT_TCP_Data_ RTU.dll " (ByVal llSocketHandle As Long, ByVal lucDevID As Byte, ByVal lunAddress As Integer, ByVal lunQuantity As Integer, ByRef lunHLData As Integer) As Long
We have to convert this code into C# compliant code.
[DllImport("MT_TCP_Data_RTU.dll", EntryPoint = "MT_Open_TcpPort")]
static extern long MT_Open_TcpPort(ref int socketHandle, string remoteIP, int remotePort);
[DllImport("MT_TCP_Data_RTU.dll", EntryPoint = "MT_Read_Data")]
static extern long MT_Read_Holding_Reg(int socketHandle, byte devID, int address, int qty, ref int[] hold_reg);
Please suggest if anything wrong in above code.
Regards,
Vikram
VulpesPosted Mar 3, 2012, 8:32 AM