Search code examples
c#c++pinvokewrapper

P/Invoke "Protected Memory" exception on /MT or /MD


I've coded a wrapper for CryptoPP which would be used by c# app. My issue is that when calling to a specific function in my wrapper using PInvoke, throws an exception "Attempted to read or write protected memory...". Both of them are compiled as x64.

Now.. strange part is that if i compile my wrapper using /MTd or /MDd runtime, the call does not fail and everything works perfectly. But changing the runtime to /MT or /MD would throw the above exception.

I cannot use the /MTd or /MDd option for official use by my customers, as it requires lots of dlls resources to be installed or distributed into the user machine.

cpp code:

extern "C" __declspec(dllexport) int CryptBlock(bool mode, unsigned char type, unsigned char *inData, unsigned char *outData, int dataLen, unsigned char *key, int keyLen);

c# PInvoke:

[DllImport("mydll.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
    public static extern int CryptBlock(bool mode, byte type, IntPtr inData, IntPtr outData, int dataLen, IntPtr key, int keyLen);

I have tried modifying my P/Invoke code in various ways: [In, Out], [Out], ref, ref byte[], byte[] etc... still throwing the exception.

Waiting for my savior...

Thank you.


Solution

  • You are right that you cannot distribute the debug runtime, but in fact the issue is not quite what you think. The license does not permit redistribution of the debug runtime.

    The most likely explanation is in fact that your code has a defect. The fact the the defect does not manifest with the debug runtime is simply down to chance. So the correct way to proceed is to track down your defect and fix it.