AccessViolationExceptiond:试图读/写保护的内存
我有一个用C编写的DLL
文件。我尝试在我的C#代码中使用C DLL(ImportDLL)。我的方法返回参数。 C法是正确调用,但过程中坠毁,并给出错误**"System.AccessViolationException: Attempted to read or write protected memory.
AccessViolationExceptiond:试图读/写保护的内存
这通常是指示其他内存已损坏“**过程完成之后。
我的C声明
int preProcessAndBestImagesC(
char* ...,
size_t* ...,
char** ...,
size_t* ...,
(struct)* ...,
size_t* ...,
int** ...,
(struct)** ...,
int ...,
int printStatus
);
我C#声明
[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 ...
);
有谁知道问题是什么?
没有代码是很难回答你的问题,但你可以使用下面的步骤,通过MSDN
转到建议
工具 - >选项
Debugging->常规
不选择选项“抑制模块负载的JIT优化“
我尝试这个解决方案,但它不适合我。 – mmpatel009 2013-04-12 11:37:26
该声明,例如编组参数cdecl/stdcall可能是错误的。
它也可能是数据执行保护(DEP)问题。在这种情况下,请在postbuild事件中使用
editbin.exe /NXCOMPAT:NO "$(TargetPath)"
。
请提供更多详细信息,提前致谢。 – mmpatel009 2013-04-11 13:23:35
看看你在这里发布的其他问题(http://stackoverflow.com/questions/15925884/an-unhandled-exception-of-type-system-executionengineexception-occurred-in-xxx),它可能是一个编组问题。 编辑您的问题,并向我们展示C中函数的原始签名以及C#中的DllImport语句。 – 2013-04-12 08:27:17
请在c#中看到我的c声明和c#DLLImport语句,并告诉我在我的c#代码中需要哪些更改。 – mmpatel009 2013-04-12 10:39:46
我建议如下:
考虑到DLL返回一个指针到内存中,请确保您的编组数据/参数。您可以使用INTPTR指向由DLL分配的内存。
此外,请确保DLL不会隐式删除分配的内存。如果是这样,请考虑重新编写DLL代码(如果可能)
希望这有助于。
您是否尝试过Google?我很确定你已经找到了一些有用的答案,即使在这里在stackoverflow。 – 2013-04-11 08:23:34