内存冲突异常 [英] Memory Violation Exception

查看:83
本文介绍了内存冲突异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在mfc(这是一个dll类型的项目)中有一个函数,该函数提取一个zip文件并创建最终显示这些文件的文件夹,现在在C#.Net项目中使用了该函数,因此当我从C#中的dll调用此方法时. NET项目中,并非所有文件都被提取并显示AccessViolationExceptions发生的错误.

我猜想这个缓冲区将.net项目中提取的文件带到那里,无法分配那么多的内存..
我需要建议....

BYTE *缓冲区;
缓冲区=新的BYTE [oFileData.lActualSize];
if(缓冲区== 0)
{
_tcscpy((** errorText),szFilesFolder.GetBuffer());
szFilesFolder.ReleaseBuffer();
CoTaskMemFree((** errorText));

返回FAILTOALLMEM;
}

mfc中功能的签名:

extern"C" _declspec(dllexport)int extractCompressedFile(TCHAR * _szFilePath,TCHAR * _extractPath,TCHAR ** _errorText)


调用C#.Net

IntPtr BackupDLL = NativeMethods.LoadLibrary(somepath);
IntPtr procaddr = NativeMethods.GetProcAddress(BackupDLL,"extractCompressedFile");
C_ExtractFiles func =(C_ExtractFiles)Marshal.GetDelegateForFunctionPointer(procaddr,typeof(C_ExtractFiles));
res = func(Program.ptc_SourFilePath,ptc_DestinationFilePath,ref errtext);
Program.strResult = res.ToString();



静态类ExtNativeMethods
{
[DllImport("kernel32.dll",CharSet = CharSet.Ansi)]
公共静态外部IntPtr GetProcAddress(IntPtr hModule,[In] [MarshalAsAttribute(UnmanagedType.LPStr)]字符串procedureName);
}

Hi all ,

I have a function in mfc(which is a dll type project) which extracts a zip file and creates folder which ultimately show up the files ,now this is used in C#.Net project so when I call this method from dll in the C#.net project not all files are extracted and shows up the error an AccessViolationExceptions occurs .

I guess this buffer which takes the extracted files there at the .net project is not able to allocate that much amount of memory..
I need advice on it ....

BYTE *buffer;
buffer = new BYTE[oFileData.lActualSize];
if( buffer == 0 )
{
_tcscpy((*_errorText), szFilesFolder.GetBuffer() );
szFilesFolder.ReleaseBuffer();
CoTaskMemFree((*_errorText));

return FAILTOALLMEM;
}

SIGNATURE of the function in mfc :

extern "C" _declspec(dllexport) int extractCompressedFile( TCHAR* _szFilePath, TCHAR* _extractPath, TCHAR** _errorText )


Calling in C#.Net

IntPtr BackupDLL = NativeMethods.LoadLibrary(somepath);
IntPtr procaddr = NativeMethods.GetProcAddress(BackupDLL, "extractCompressedFile");
C_ExtractFiles func = (C_ExtractFiles)Marshal.GetDelegateForFunctionPointer(procaddr,typeof(C_ExtractFiles));
res = func(Program.ptc_SourFilePath, ptc_DestinationFilePath, ref errtext);
Program.strResult = res.ToString();



static class ExtNativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Ansi)]
public static extern IntPtr GetProcAddress(IntPtr hModule, [In][MarshalAsAttribute(UnmanagedType.LPStr)] string procedureName);
}

推荐答案

这是编组的问题:.Net数据类型需要某种转换才能在C ++ dll中使用.因此,请向我们展示C ++函数的签名和您的DllImport语句,然后我们将能够提供更多的提示.
That is a problem of Marshalling: the .Net data types need some kind of conversion for use in the C++ dll. Hence, show us the signature of the C++ function and your DllImport statement, then we will be able to provide further hints.


这篇关于内存冲突异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆