am calling win32 dll in the program I wrote in c# project but System.AccessViolationException: 'An attempt was made to read or write to protected memory. This is usually an indication that other memory is corrupt.' error . Error Screen DLL Calling Screen What could be the cause of this error? Although I made adjustments in the debug section as follows, it did not get better.
How should I use a file with win32 dll for C# (I know the functions in this file)
can you help me?
The codes are as follows.
1- Function where DLL is called
public static class Ziton_Function_Library
{
[DllImport(".\\2010-2W32.dll",EntryPoint = "FP10_ConnectTCPIP",CharSet =CharSet.Auto)]
internal static extern int EEC_FP10_ConnectTCPIP(char strIP, int wPort, int wIdPanel, long dwPassword, long dwTimeout, long dwConnTimeout, out int nError);
}
2- Function encountered with error in the program
private void ConnectTcpIp_Click(object sender, EventArgs e)
{
String strPassword, strPanelId, strTimeOut, strConnTimeOut;
String strIP, strPort;
int nPort, m_nIdPanelTCPIP,nError;
long nPassword, nTimeOut, nConnTimeOut;
Char nStrIP;
IntPtr m_nIdSessionTCPIP;
Tb_Password.Text = "1234";
Tb_PanelID.Text = "1";
Tb_ConnectionTimedOut.Text = "3000";
Tb_IPadress.Text = "192.168.104.140";
Tb_PanelPort.Text = "2505";
strPassword = Tb_Password.Text;
strPanelId = Tb_PanelID.Text;
strTimeOut = "0";
strConnTimeOut = Tb_ConnectionTimedOut.Text;
strIP = Tb_IPadress.Text;
strPort = Tb_PanelPort.Text;
nPort = Convert.ToInt16(strPort);
m_nIdPanelTCPIP = Convert.ToInt16(strPanelId);
nPassword = Convert.ToInt32(strPassword);
nTimeOut = Convert.ToInt32(strTimeOut);
nConnTimeOut = Convert.ToInt32(strConnTimeOut);
if (!string.IsNullOrEmpty(strIP) && strIP.Trim().Length == 1)
nStrIP = System.Convert.ToChar(strIP);
else
{
nStrIP = strIP.FirstOrDefault(); // if thats what your logic
}
//nStrIP = Convert.ToChar(strIP);
nError = 0;
Cursor.Current = Cursors.WaitCursor;
m_nIdSessionTCPIP = (IntPtr)Ziton_Function_Library.EEC_FP10_ConnectTCPIP(nStrIP, nPort, m_nIdPanelTCPIP, nPassword, nTimeOut, nConnTimeOut, out nError);
Cursor.Current = Cursors.Default;
Tb_SessionID.Text = Convert.ToString(m_nIdSessionTCPIP);
switch (nError)
{
case 0:
Tb_ConnectionStatus.Text = "Connected";
Tb_ConnectionStatus.BackColor = Color.Lime;
Tb_ConnectionStatus.ForeColor = Color.Black;
return;
case 2:
Tb_ConnectionStatus.Text = "Socket Error";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 16:
Tb_ConnectionStatus.Text = "Protocol Error";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 17:
Tb_ConnectionStatus.Text = "Gateway Session Already Established";
Tb_ConnectionStatus.BackColor = Color.Yellow;
Tb_ConnectionStatus.ForeColor = Color.Black;
return;
case 32:
Tb_ConnectionStatus.Text = "Operation Execution Failed";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 34:
Tb_ConnectionStatus.Text = "Operation Not Available";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 36:
Tb_ConnectionStatus.Text = "Unable to New Connection";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 38:
Tb_ConnectionStatus.Text = "Panel ID Does Not Match";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 39:
Tb_ConnectionStatus.Text = "Login Timeout";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 40:
Tb_ConnectionStatus.Text = "Invalid Password";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
case 61:
Tb_ConnectionStatus.Text = "Firmware Versiyon Not Compatible";
Tb_ConnectionStatus.BackColor = Color.Red;
Tb_ConnectionStatus.ForeColor = Color.White;
return;
}
CodePudding user response:
I'm pretty convinced your problem is in the first parameter ...EEC_FP10_ConnectTCPIP(char strIP...
I didn't find this library so I am not sure what's the native descriptor of this function but I'm guessing it expects a pointer to a string, not a char value. Double check that and refer to https://learn.microsoft.com/en-us/dotnet/framework/interop/default-marshalling-for-strings