C# 试图读取受写保护的内存 我有一个C++ DLL,导出这样的方法: extern "C" __declspec (dllexport) void ConvE(int type, const char* path, int b1, int b2, int b3, int b4, int b5) { FFileList file_list; char temp_path[1024]; if(type == 1) { sprintf(temp_path,"%s*",path); GetFindFileListWin(temp_path,".mrs",file_list); file_list.RecoveryZipE(b1, b2, b3, b4, b5); file_list.ConvertNameMRes2Zip(); } else if(type == 2) { sprintf(temp_path,"%s*",path); GetFindFileListWin(temp_path,".zip",file_list); file_list.ConvertZipE(b5, b4, b3, b2, b1); file_list.ConvertNameZip2MRes(); } } [DllImport("Mrs.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern void ConvE(int type, string path, int b1, int b2, int b3, int b4, int b5);
我将它调用到我的C#应用程序中,如下所示:C# 试图读取受写保护的内存 我有一个C++ DLL,导出这样的方法: extern "C" __declspec (dllexport) void ConvE(int type, const char* path, int b1, int b2, int b3, int b4, int b5) { FFileList file_list; char temp_path[1024]; if(type == 1) { sprintf(temp_path,"%s*",path); GetFindFileListWin(temp_path,".mrs",file_list); file_list.RecoveryZipE(b1, b2, b3, b4, b5); file_list.ConvertNameMRes2Zip(); } else if(type == 2) { sprintf(temp_path,"%s*",path); GetFindFileListWin(temp_path,".zip",file_list); file_list.ConvertZipE(b5, b4, b3, b2, b1); file_list.ConvertNameZip2MRes(); } } [DllImport("Mrs.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern void ConvE(int type, string path, int b1, int b2, int b3, int b4, int b5);,c#,c++,memory,unmanaged,C#,C++,Memory,Unmanaged,我将它调用到我的C#应用程序中,如下所示: extern "C" __declspec (dllexport) void ConvE(int type, const char* path, int b1, int b2, int b3, int b4, int b5) { FFileList file_list; char temp_path[1024]; if(type == 1) { sprintf(temp_path,"%s*",pat
extern "C" __declspec (dllexport) void ConvE(int type, const char* path, int b1, int b2, int b3, int b4, int b5)
{
FFileList file_list;
char temp_path[1024];
if(type == 1)
{
sprintf(temp_path,"%s*",path);
GetFindFileListWin(temp_path,".mrs",file_list);
file_list.RecoveryZipE(b1, b2, b3, b4, b5);
file_list.ConvertNameMRes2Zip();
}
else if(type == 2)
{
sprintf(temp_path,"%s*",path);
GetFindFileListWin(temp_path,".zip",file_list);
file_list.ConvertZipE(b5, b4, b3, b2, b1);
file_list.ConvertNameZip2MRes();
}
}
[DllImport("Mrs.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern void ConvE(int type, string path, int b1, int b2, int b3, int b4, int b5);
但每次我运行它时,它都会抛出错误“尝试读取或写入受保护的内存这通常表示其他内存已损坏”
据我所知,我在C#中导入函数的方式有问题,但我真的不知道如何解决它
编辑:
默认情况下,有一个Conv()与我的Conv()相同,但没有5个整数,而且这个可以正常工作。
我让Conv使用文件_list.RecoveryZipE(),以前在调试时,它将我堆叠在一个名为RecvoeryCharE的方法中(我将它添加到我的代码中,您可以查看一下),而且,该方法基于在Conv()上工作的RecoveryChar()
RecoveryChar:
由于某种原因,b1值总是取0。
似乎正是因为这个原因,我得到了受保护的内存错误
我所做的只是复制存在recoveryChar()和convertChar()的所有方法,并将值作为参数传递。我将探讨这些问题: 你的C++平台和C语言平台是什么?它们都是32位吗?都是64位吗?如果混合使用整数长度,则两种语言之间的参数传输可能存在错误。无论如何,最安全的方法是使用例如“int32\u t”而不是仅使用“int”来指定确切的整数宽度 有些人建议在用C++“char *”转换为C’字符串时指定精确的封送器,例如“MarshalAs(unMaundType,LPSRT)”字符串路径“,请尝试这个”
inline void log_(const char *format, ...)
{
va_list args;
va_start(args, format);
char buffer[1000];
vsprintf(buffer, format, args);
OutputDebugStringA(buffer);
}
如果将函数体更改为空,是否仍会发生错误?如果是-函数内部有问题。如果没有-导入时出现问题。当您试图访问数组中不存在的索引时,通常会发生此错误,可能是您的文件列表为空,或者您请求的是仅包含2个文件的列表中的第4个文件files@EduardoWada我用更多与我的错误相关的信息更新了我的问题,请看一看,谢谢你的回复。这个异常只说发生在C++代码中,你必须调试C++ DLL来找到它发生的行和变量的值。再次感谢你试图帮助我。我应该发布原始代码和我的编辑吗?
inline void log_(const char *format, ...)
{
va_list args;
va_start(args, format);
char buffer[1000];
vsprintf(buffer, format, args);
OutputDebugStringA(buffer);
}