interop >> Exception on call to unmanaged dll. Attempted to read or write protected memory. This is often an indication that other memory is corrupt

by John » Fri, 02 Jun 2006 03:40:13 GMT

Hello,

I'm having a problem with a call to an unmanaged dll. I've been
looking at for some time and hopefully one of you can help me. I'm
also new to C# so it very easily could be a newbie issue.

Header File of dll.

int pfproInit();
void pfproCleanup();
int pfproCreateContext(int * context, char *host_addr, int host_port,
int time_out, char *proxy_addr, int proxy_port, char *proxy_logon, char
*proxy_pass);
int pfproDestroyContext(int context);
int pfproSubmitTransaction(int context, char *strParm, long lParmLen,
char **strResp);
void pfproCompleteTransaction(char *strResp);
char *pfproVersion();

C# definitions

public class Trans
{

[DllImport("pfpro.dll", EntryPoint = "pfproInit", CallingConvention
= CallingConvention.Cdecl)]
public static extern int Init();

[DllImport("pfpro.dll", EntryPoint = "pfproCleanup",
CallingConvention = CallingConvention.Cdecl)]
public static extern void Cleanup();

[DllImport("pfpro.dll", EntryPoint = "pfproCreateContext",
CallingConvention = CallingConvention.Cdecl)]
public static extern int CreateContext(ref int context,
StringBuilder host_addr, int host_port, int time_out, StringBuilder
proxy_addr, int proxy_port, StringBuilder proxy_logon, StringBuilder
proxy_pass);

[DllImport("pfpro.dll", EntryPoint = "pfproDestroyContext",
CallingConvention = CallingConvention.Cdecl)]
public static extern int DestroyContext(int context);

[DllImport("pfpro.dll", EntryPoint = "pfproSubmitTransaction",
CallingConvention = CallingConvention.Cdecl)]
public static extern int SubmitTransaction(int context,
StringBuilder strParm, long lParmLen, StringBuilder strResp);

[DllImport("pfpro.dll", EntryPoint = "pfproCompleteTransaction",
CallingConvention = CallingConvention.Cdecl)]
public static extern void CompleteTransaction(StringBuilder
strResp);

[DllImport("pfpro.dll", EntryPoint = "pfproVersion",
CallingConvention = CallingConvention.Cdecl)]
public static extern StringBuilder Version();
}

Calling code


StringBuilder transactionCommand = new StringBuilder(4096);
StringBuilder test = new StringBuilder("test-payflow.verisign.com");
StringBuilder test2 = new StringBuilder(String.Empty);
StringBuilder test3 = new StringBuilder(String.Empty);
StringBuilder test4 = new StringBuilder(String.Empty);
StringBuilder resp = new StringBuilder(4096);

transactionCommand.Append("TRXTYPE=D&TENDER=C&COMMENT1=www.test.com
Capture");

con = Trans.Init();
if (Trans.CreateContext(ref con, test, 443, 30, test4, 0, test2, test3)
== 0)
{
//The following line is where I'm getting the exception
Trans.SubmitTransaction(con, transactionCommand,
transactionCommand.Length, resp);
Trans.CompleteTransaction(resp);
Trans.DestroyContext(con);
Trans.Cleanup();
}


Thanks for any help given.


interop >> Exception on call to unmanaged dll. Attempted to read or write protected memory. This is often an indication that other memory is corrupt

by Mattias Sjren » Fri, 02 Jun 2006 06:29:38 GMT


John,

[...]

long in C++ maps to int in C#. Change the lParmLen parameter type
accordingly.

The type of the last parameter type should probably be out IntPtr.


Mattias

--
Mattias Sjren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Similar Threads

1. "Attempted to read or write protected memory. This is often an indication that other memory is corrupt" - Asp.Net

2. Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Have just switch from 1.1 to framework 2.0 and i getting this error :

Attempted to read or write protected memory. This is often an indication 
that other memory is corrupt

anyone have a clu why ?

happens inside this :

public bool ExecSql(string sql)

{

bool ReturnValue = false;

OpenConnection();

OleDbCommand myCommand = new OleDbCommand(sql,oDatabaseConnection);


try

{

try

{

log.Debug("[ExecSql] SQL (ExecuteNonQuery): " + sql);


//OleDbTransaction myTrans = 
oDatabaseConnection.BeginTransaction(IsolationLevel.ReadCommitted);

if (myTrans!=null)

myCommand.Transaction = myTrans;

myCommand.ExecuteNonQuery();

//myTrans.Commit();

ReturnValue = true;

}

catch (Exception ex)

{

log.Error("[ExecSql] Fatal error : " + ex.Message + " - " + sql);

}

}

finally

{

GC.Collect();

GC.WaitForPendingFinalizers();


}

return ReturnValue;

}


3. System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt - Microsoft .NET Framework

4. Attempted to read or write protected memory. This is often an indication that other memory is corrupt

Hi,
I am getting the error message
Attempted to read or write protected memory. This is often an indication 
that other memory is corrupt.
It was not coming until yet, for around 2 months. Now, may be the records 
have increased and so i am getting this.

anyone has any idea why and when do we get this.

Its a windows based application with c# 2.0 and .Net Framework 2.0.

Regards,
Sugandh Jain


5. How can i handle "attempted to read or write protected memory. this is often an indication that other memory is corrupt" - CSharp/C#

6. native dll access error - Attempted to read or write protected memory

Hi,
I am using .NET 2.0 and trying to use a function from a native DLL
file.
Here is the syntax that I am using:

definition:
 [DllImport(@"old.dll", EntryPoint="#1")]
    public static extern String getPwd(String strServerName, String
strUserId);

Call:
 string pwd = getPwd("servername", "myusername");

I get the following error on the above line:
________________
Attempted to read or write protected memory. This is often an
indication that other memory is corrupt.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.

Exception Details: System.AccessViolationException: Attempted to read
or write protected memory. This is often an indication that other
memory is corrupt.
___________________

I have read the MSDN articles on these issues but those solutions are
not working.

Foll is the output of the 'link' command about the dll:

link -dump -exports old.dll
File Type: DLL

  Section contains the following exports for old.dll

    00000000 characteristics
    3A352AEB time date stamp Mon Dec 11 14:28:43 2000
        0.00 version
           1 ordinal base
           1 number of functions
           1 number of names

    ordinal hint RVA      name

          1    0 000012F0 _pwGet@12

  Summary

        5000 .data
        2000 .rdata
        1000 .reloc
        A000 .text


Any help , direction is greatly appreciated.

Thanks,
Hiral

7. Attempted to read or write protected memory. This is often an indication that other memory is corrup - .NET Windows Forms