0

I have a DLL file that is written in C. I am try to use in C DLL (ImportDLL) in my C# code. My method return out parameter. C method is called correctly but it crashed after process and gives error **"System.AccessViolationException: Attempted to read or write protected memory.

This is often an indication that other memory is corrupt"** after process completed.

My C declaration

int preProcessAndBestImagesC(
      char* ...,
      size_t* ...,
      char** ...,
      size_t* ...,
      (struct)* ...,
      size_t* ...,
      int** ...,
      (struct)** ...,
      int ...,
      int printStatus
    );

My C# Declaration

[DllImport(@"abc.dll", CallingConvention = CallingConvention.StdCall, SetLastError = true, BestFitMapping = true, EntryPoint = "xxx")]
    [return: MarshalAs(UnmanagedType.I4)]
    unsafe private static extern int xxx(
        String p_ ...,                                               
        [MarshalAs(UnmanagedType.I2)] out UInt16 p_numImageFilesOrDirs,

        String[] p_vecImageFilesOrDirs,             

        [MarshalAs(UnmanagedType.I2)] out UInt16 ...,
        [MarshalAs(UnmanagedType.LPArray)] out (struct)[] ...,
        [MarshalAs(UnmanagedType.I2)] out UInt16 ...,
        out Int16[] ...,
        [MarshalAs(UnmanagedType.LPArray)] out (struct)[] ...,
        [MarshalAs(UnmanagedType.I2)] Int16 ...,
        [MarshalAs(UnmanagedType.I2)] Int16 ...
    );

Does anyone know what the problem is?

mmpatel009
  • 921
  • 4
  • 11
  • 25
  • Did you try to google for this? I'm pretty sure you'll already find some useful answers, even here in stackoverflow. – Żubrówka Apr 11 '13 at 08:23

3 Answers3

0

Without code it is hard to answer your question but you can use following steps as suggested by msdn

Go to

Tools->Options

Debugging->General

uncheck option "Suppress JIT optimization on module load"

Abhishek
  • 75
  • 4
0

The declaration, e.g. marshalling of parameters, cdecl/stdcall, could be wrong.

It could also be a Data Execution Prevention (DEP) issue. In that case, use

editbin.exe /NXCOMPAT:NO "$(TargetPath)"

in the postbuild event.

Bernhard Hiller
  • 2,163
  • 2
  • 18
  • 33
  • Please provide more details, Thanks in advance. – mmpatel009 Apr 11 '13 at 13:23
  • Looking at the other question you posted here (http://stackoverflow.com/questions/15925884/an-unhandled-exception-of-type-system-executionengineexception-occurred-in-xxx), it is likely a marshalling problem. Edit your question and show us both the original signature of the function in C,and your DllImport statement in C#. – Bernhard Hiller Apr 12 '13 at 08:27
  • Please see my c declaration and c# DLLImport statement in c# and tell me which changes is required in my c# code. – mmpatel009 Apr 12 '13 at 10:39
  • Quite complex. There are some "struct"s - do you use the sequential layout attribute? int - are they really 16bit ints? string - character set issues? StdCall vs. CDecl? – Bernhard Hiller Apr 12 '13 at 10:56
  • Yes i have use sequential layout attribute. int is 16bit ints, and string - character set no any issues my c program accept my string parameter correctly. – mmpatel009 Apr 12 '13 at 11:16
0

I would suggest the following:

Considering the DLL returns a POINTER to the memory, please make sure to Marshaling your data/parameter. You can use INTPTR to point to the memory allocated by DLL.

Also, make sure the DLL doesn't implicitly deletes the allocated memory. If it does, please consider re-writing the DLL code (if possible)

Hope this helps.

Rahul Shukla
  • 1,292
  • 9
  • 25