none
DllImport c# "Attempted to read or write protected memory" error RRS feed

  • Question

  • Hi all,

    I am using DllImport to invoke an external C dll's function.

    C header file has the following definition

    _EASY_IMPORT_OR_EXPORT_ eint EASYCALL setSessionElement(eint SessionId, PCSTR lpszElementName, PCSTR lpszElementValue)

    C# import statement

       [DllImport("EasyclaimC.dll", CharSet = CharSet.Auto, EntryPoint = "setSessionElement", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
       public static extern long setSessionElement(
           long SessionId,
           [MarshalAs(UnmanagedType.LPWStr)]
           string ElementName,
           [MarshalAs(UnmanagedType.LPWStr)]
            string ElementValue);

    Invoke:

    ECAPI.setSessionElement(aSessionId, "LocationID", minorCustId);

    which fails with the "attempted to read or write protected memory error"

    Is the dll import signature correct? Please throw some light on this. Thanks

    Maithili

    
    
    
    
    
    
    
    
    
    
    
    

    statusCode =

    

    Tuesday, March 25, 2014 12:41 AM

Answers

  • Hi, I think eint is 32bit int, so you can try to change it to:

     [DllImport("EasyclaimC.dll", CharSet = CharSet.Auto, EntryPoint = "setSessionElement", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
       public static extern int setSessionElement(
           int SessionId,
           [MarshalAs(UnmanagedType.LPWStr)]
           string ElementName,
           [MarshalAs(UnmanagedType.LPWStr)]
            string ElementValue);

    And if it still does not work, you can try to change it to:

     [DllImport("EasyclaimC.dll", CharSet = CharSet.Auto, EntryPoint = "setSessionElement", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
       public static extern int setSessionElement(
           int SessionId,
           [MarshalAs(UnmanagedType.LPStr)]
           string ElementName,
           [MarshalAs(UnmanagedType.LPStr)]
            string ElementValue);

    • Marked as answer by mythilireddy Thursday, May 1, 2014 10:47 PM
    Tuesday, March 25, 2014 2:35 AM
  • PCSTR is a const char* but you're trying to treat it as a Unicode string.  That won't work.  And as lapheal mentioned a C int (32 bits) maps to a C# int (32 bits).  A C# long (64 bits) does not map to a C int or long (both 32 bits).

    You didn't provide the definition of the export macro so it is unclear what the calling convention is.  If the convention is wrong the import will crash so verify it as well.

    _EASY_IMPORT_OR_EXPORT_ eint EASYCALL setSessionElement(eint SessionId, PCSTR lpszElementName, PCSTR lpszElementValue)

    //May need to add CallingConvention
    //EntryPoint and ExactSpelling are not necessary but I find them useful to include
    [DllImport("dll.dll", EntryPoint="setSessionElement", ExactSpelling=true, CharSet=CharSet.Ansi, PreserveSig=true)]
    private static extern int setSessionElement ( int sessionId, string lpszElementName, string lpszElementValue );

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, March 27, 2014 7:49 PM
    Moderator

All replies

  • Hi, I think eint is 32bit int, so you can try to change it to:

     [DllImport("EasyclaimC.dll", CharSet = CharSet.Auto, EntryPoint = "setSessionElement", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
       public static extern int setSessionElement(
           int SessionId,
           [MarshalAs(UnmanagedType.LPWStr)]
           string ElementName,
           [MarshalAs(UnmanagedType.LPWStr)]
            string ElementValue);

    And if it still does not work, you can try to change it to:

     [DllImport("EasyclaimC.dll", CharSet = CharSet.Auto, EntryPoint = "setSessionElement", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
       public static extern int setSessionElement(
           int SessionId,
           [MarshalAs(UnmanagedType.LPStr)]
           string ElementName,
           [MarshalAs(UnmanagedType.LPStr)]
            string ElementValue);

    • Marked as answer by mythilireddy Thursday, May 1, 2014 10:47 PM
    Tuesday, March 25, 2014 2:35 AM
  • Thanks for your reply. I have tried these but no luck.

    Another function from the same dll seems to work without any issues..

       [DllImport("EasyclaimC.dll", CharSet = CharSet.Auto, EntryPoint = "setSessionElement", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
       public static extern long createSessionEasyclaim(
            ref long sessionid,
            string lpszFilePath,
            string lpszPassPhrase);

    I am able to retrieve the SessionId from the above function. Just the below one I have problems with.

       [DllImport("EasyclaimC.dll", CharSet = CharSet.Auto, EntryPoint = "setSessionElement", CallingConvention = CallingConvention.StdCall, SetLastError = true)]
       public static extern long setSessionElement(
           long SessionId,
           string lpszElementName,
           string lpszElementValue);

    
    
    
    
    
    
    
    
    Tuesday, March 25, 2014 4:14 AM
  • PCSTR is a const char* but you're trying to treat it as a Unicode string.  That won't work.  And as lapheal mentioned a C int (32 bits) maps to a C# int (32 bits).  A C# long (64 bits) does not map to a C int or long (both 32 bits).

    You didn't provide the definition of the export macro so it is unclear what the calling convention is.  If the convention is wrong the import will crash so verify it as well.

    _EASY_IMPORT_OR_EXPORT_ eint EASYCALL setSessionElement(eint SessionId, PCSTR lpszElementName, PCSTR lpszElementValue)

    //May need to add CallingConvention
    //EntryPoint and ExactSpelling are not necessary but I find them useful to include
    [DllImport("dll.dll", EntryPoint="setSessionElement", ExactSpelling=true, CharSet=CharSet.Ansi, PreserveSig=true)]
    private static extern int setSessionElement ( int sessionId, string lpszElementName, string lpszElementValue );

    Michael Taylor
    http://msmvps.com/blogs/p3net

    Thursday, March 27, 2014 7:49 PM
    Moderator