简体   繁体   中英

DLL Import corrupting memory

I am receiving this error while using a dllimport Attempted to read or write protected memory. This is often an indication that other memory is corrupt Attempted to read or write protected memory. This is often an indication that other memory is corrupt

private const string dir2 = @"C:\NBioBSP.dll";

[System.Runtime.InteropServices.DllImport(dir2, SetLastError = true, CallingConvention = System.Runtime.InteropServices.CallingConvention.Cdecl)]
public static extern uint NBioAPI_FreeFIRHandle(IntPtr hHandle, IntPtr hFIR); 

I call it like this

uint resultado = NBioAPI_FreeFIRHandle(handle, handle);

Anyone know what the issue can be

Two problems.

First, the calling convention is wrong. Per the header file that defines the function (as well as the supporting file that defines NBioAPI as __stdcall on the Win32 platform), you should be using CallingConvention.StdCall .

Second, in the header that defines the types used by the API, NBioAPI_HANDLE and NBioAPI_FIR_HANDLE are typedef 'd to UINT , which is always 32 bits (four bytes) long. You're using IntPtr , which has a platform-dependent size (it will be 64 bits in a 64-bit process.) Change the function parameters to uint .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM