locked
What is this error and how to solve it? RRS feed

  • Question

  • Hi Guys,
    I encounter this error while wrking with an API:
    "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
    the code i use is as below:

    Code Snippet

    using System;

    using System.Collections.Generic;

    using System.ComponentModel;

    using System.Data;

    using System.Drawing;

    using System.Text;

    using System.Windows.Forms;

    using System.Runtime.InteropServices;

    namespace WindowsApplication16

    {

    public partial class Form1 : Form

    {

    public Form1()

    {

    InitializeComponent();

    }

    private void button1_Click(object sender, EventArgs e)

    {

    IntPtr handle = (IntPtr)0;

    string apiStatus;

    apiStatus = RFID.ImportOfDll.OpenConnection(handle, "COM1");

    }

    }

    }

    namespace RFID

    {

    class ImportOfDll

    {


    [DllImport("C:\\Sense18KAPI.dll", EntryPoint = "Sense18K_Open")]

    public static extern string OpenConnection(IntPtr hCom,string comPort);

    }

    }



    What's wrong with the code above.
    let me give you the documentation of the API:

    Code Snippet

    Open COM Port: This function is used to open a communication connection between the host and the reader. The default communication baud rate is 9600bps.

    apiStatus WINAPI Sense18K_Open(HANDLE &hCom, char *com_port)

    Parameter: hCom: Reader handle Com_port: Com Port Number Return value: Status flags, See clause 6.1.


    .......................................

    6.1 Return Status Flags (apiStatus)


    #define ERR_NONE 0x00 // No errors encountered

    #define ERR_UNDEFIND 0xFF // Undefined errors

    #define ERR_CMD_UNDEF 0xFE // Command undefined

    #define ERR_CMD_SUB_UNDEF 0xFD // Subcommand undefined

    #define ERR_CMD_PARAM_INVAL 0xFC // Input an invalid parameter or over limit

    #define ERR_CMD_CRC_ERR 0xFB // CRC error in a command

    #define ERR_CMD_RES_CRC_ERR 0xFA // CRC error in a response from the


    #define ERR_S_BUF_EMPTY 0x40 // No data in the buffer

    #define ERR_R_ANT_FAIL 0xEF // Antenna failure or unconnected

    #define ERR_R_PLL_FAIL 0xEE // PLL unlocked

    #define ERR_R_UART_FAIL 0xED // Serial port connection error or over time

    #define ERR_R_SET_PARAM_FAIL 0xEC // Reader parameter setting failure

    #define ERR_R_GET_PARAM_FAIL 0xEB // Query reader parameter failure

    #define ERR_R_WR_FLASH_FAIL 0xEA // Write or program reader FLASH failure

    #define ERR_R_MOD_FAIL 0xE9 // Modulation setting failure

    #define ERR_R_RFPWR_FAIL 0xE8 // RF power setting failure

    #define ERR_R_ENCRYPT_FAIL 0xE0 // Reader encrpytion code failure


    #define ERR_TAG_NO_LABEL 0x8F // No tag detected

    #define ERR_TAG_WP 0x8E // Tag have been write-protected

    #define ERR_TAG_CRC_ERR 0x8D // Tag data CRC error

    #define ERR_TAG_PROG_ID_FAIL 0x8C // Program tag ID failure

    #define ERR_TAG_READ_FAIL 0x8B // Read tag failure

    #define ERR_TAG_ERASE_ID_FAIL 0x8A // Erase tag failure

    #define ERR_TAG_LOCK_ID_FAIL 0x89 // Lock tag failure

    #define ERR_TAG_KILL_ID_FAIL 0x88 // Kill tag failure

    #define ERR_RS232_ERROR 0x7F // RS232 initialize failure

    #define ERR_RS232_READ 0x7E // RS232 reading failure

    #define ERR_RS232_WRITE 0x7D // RS232 writing failure

    #define ERR_CRC_ERROR 0x7C // CRC error


    .............................................................

    7.Application Example: Following programs are common part in all samples of API application programs. It need add at the beginning of following programs.

    #include "Sense18KAPI.h" //Include header file

    HANDLE g_hCom; // serial port handle variable

    apiStatus status; // status variation returned by the command

    unsigned char g_RAddr=0xFF// reader address variation




    Wednesday, April 9, 2008 6:44 AM

Answers

  •  

    OpenConnection method returns integer, but not string, as you declared.

    I believe you should change function declaration to:

     

    public static extern int OpenConnection(IntPtr hCom,string comPort);
    Wednesday, April 9, 2008 9:22 AM

All replies

  • I'm sorry for prev. post. I've mistaken. Pretty elementary mistake... I've got confused while playing with reflector

    Wednesday, April 9, 2008 7:38 AM
  •  Vitaliy Liptchinsky wrote:

    Why do you use this line of code?

    IntPtr handle = (IntPtr)0;

    If you need to pass emtpy handle to external function, do not initialize it with 0. When you initialize it with 0, then handle is not considered to be null.

    Probably OpenConnection checks if passed handle is not null and then performs some operations with it.

    Use default IntPtr constructor instead.

     

    you are wrong, there is no difference between:

    IntPtr ptr = new IntPtr();
    IntPtr ptr = new IntPtr(0);
    IntPtr ptr = (IntPtr)0;
    Wednesday, April 9, 2008 8:21 AM
  • So guys,What is the solution,what should i do to not get this erorr.Thanks
    Wednesday, April 9, 2008 9:04 AM
  •  

    OpenConnection method returns integer, but not string, as you declared.

    I believe you should change function declaration to:

     

    public static extern int OpenConnection(IntPtr hCom,string comPort);
    Wednesday, April 9, 2008 9:22 AM