Reputation: 941
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?
Upvotes: 0
Views: 600
Reputation: 1292
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.
Upvotes: 0
Reputation: 2407
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.
Upvotes: 0
Reputation: 75
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"
Upvotes: 0