Note that there are some explanatory texts on larger screens.

plurals
  1. POProtected Memory Violation calling FORTRAN DLL from C#
    primarykey
    data
    text
    <p>I am trying to call out to a legacy dll compiled from FORTRAN code. I am new to Interop, but I've read some articles on it and it seems like my case should be fairly straightforward. </p> <p>The method I really want to call has a complex method signature, but I can't even call this simple GetVersion method without getting a protected memory violation.</p> <p>Here's my DllImport code:</p> <pre><code>[DllImport("GeoConvert.dll", EntryPoint="_get_version@4", CallingConvention=CallingConvention.StdCall)] public static extern void GetGeoConvertVersion([MarshalAs(UnmanagedType.LPStr, SizeConst=8)] ref string version); </code></pre> <p>Here's the FORTRAN code:</p> <pre><code>SUBROUTINE GetVer( VRSION ) C !MS$DEFINE MSDLL !MS$IF DEFINED (MSDLL) ENTRY Get_Version (VRSION) !MS$ATTRIBUTES DLLEXPORT,STDCALL :: Get_Version !MS$ATTRIBUTES REFERENCE :: VRSION !MS$ENDIF !MS$UNDEFINE MSDLL C CHARACTER*8 VRSION C VRSION = '1.0a_FhC' C RETURN END </code></pre> <p>Here's my unit test that fails:</p> <pre><code>[Test] public void TestGetVersion() { string version = ""; LatLonUtils.GetGeoConvertVersion(ref version); StringAssert.IsNonEmpty(version); } </code></pre> <p>Here's the error message I get:</p> <pre><code>System.AccessViolationException Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt. </code></pre> <p>Other things I've tried:</p> <ul> <li>Using the default marshalling</li> <li>Passing a char[] instead of a string (get method signature errors instead)</li> </ul>
    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.
 

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