1

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

我猜这个在.net项目中提取文件的缓冲区无法分配那么多内存..我需要关于它的建议....

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

return 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();



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

0 回答 0