Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ .NET Wrapper: Attempted to read or write protected memory. This is often an indication that other memory is corrupt
    primarykey
    data
    text
    <p>I am having C++ .net wrapper for an unmanged MFC dll. This wrapper is used by a vb.net dll, called into my c# code. During runtime sometimes the wrapper throws an exception of Attempted to read or write protected memory. </p> <pre><code>System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt </code></pre> <p>It seems to occur randomly in my "While" loop. Sometimes it throws at the beginning, sometimes in the middle and sometimes nothing is thrown.</p> <p>HOW IT WORKS: my program need a MFC dll. I have a wrapper.dll(c++) and a myVbDll.dll(vb.net) referred into my program. i also added the MFC dll as content because it's not a valid COM component. So this is how it works: </p> <p>myProgramm.exe->myVbDll.dll->wrapper.dll->myMFC.dll->myMFCfunction</p> <p><strong>INFO :</strong> If I set <code>field = "WHATSOEVER";</code> just before to call MyWrappedFunction, the error is never thrown!!</p> <p><strong>UPDATE :</strong> After several changes, the problem still occurs. I look at this time converting unicode string to Ansi. There might be something to find ... Cause when you write text in a string as above, it works, but when the ToString function is used, it does not work.</p> <p>Can some body tell why this occurs.</p> <p>Part of my program in c#(reading 5000 lines from .csv file with TextFieldParser to get field) :</p> <pre><code>string[] fields; string field ; string temp = ""; TextFieldParser parser = new TextFieldParser(textbox_csv.Text, System.Text.Encoding.UTF8); parser.TextFieldType = FieldType.Delimited; parser.SetDelimiters(";"); while (!parser.EndOfData) { fields = parser.ReadFields(); field = fields[0]; temp = field.ToUpper(); field = myVbDll.MyWrappedFunction(ref temp, false); } </code></pre> <p>Part of VB.net Dll, called by my c# program :</p> <pre><code>Public Class myVbDll Public Declare Auto Function MyWrappedFunction Lib "myWrapper.dll" (ByVal name As String, ByVal opt As Boolean) As String End Class </code></pre> <p>part of MFC wrapper, called by VB.net Dll (error is not into MFC dll for sure):</p> <pre><code>typedef void (*MYFUNCTION)(CString&amp;, CString&amp;, BYTE); MYFUNCTION Myfunction; LPWSTR _stdcall MyWrappedFunction(LPWSTR ValInput, BYTE opt) { HINSTANCE gLibtestDLL=NULL; CString S_ValInput(ValInput); CString S_resultat; gLibtestDLL = AfxLoadLibrary(TEXT(".\\test.dll")); if(gLibtestDLL == NULL) { MessageBox(NULL, TEXT("unable to load test.DLL"), TEXT("Error"),MB_OK | MB_ICONINFORMATION); return NULL; } Myfunction = (MYFUNCTION)GetProcAddress(gLibtestDLL, "Myfunction"); if (Myfunction == NULL) { MessageBox(NULL, TEXT("Can't find Myfunction."), TEXT("Error"),MB_OK | MB_ICONINFORMATION); return NULL; } //****************************************************************** S_resultat.LockBuffer(); S_resultat.Format("%64c", ' '); Myfunction(S_ValInput , S_resultat , opt); S_resultat.ReleaseBuffer(); S_resultat.LockBuffer(); S_resultat.TrimRight(); S_resultat.ReleaseBuffer(); // CString To UNICODE USES_CONVERSION; S_resultat.LockBuffer(); LPWSTR C_tmp= A2OLE(S_resultat.GetBuffer(S_resultat.GetLength())); S_resultat.ReleaseBuffer(); AfxFreeLibrary(gLibtestDLL); LPWSTR C_resultat=C_tmp; //****************************************************************** return C_resultat; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload