c# - 检测到缓冲区溢出

标签 c# c++ dllimport

我在使用 C# DllImport C++ dll 时有一个问题,我使用 visual studio 2010 并检查了“启用非托管代码调试”,当它运行时,总是显示消息“检测到缓冲区溢出!...缓冲区检测到超限已经破坏了程序的内部状态。程序无法安全地继续执行,现在必须终止。”

这个dll是第三方厂商提供的,他们说运行这个dll没有错误,我该如何修复?

我的 M++ dll 函数是,

int  avc_to_avi_convert(char* chPath_avc, char* chPath_avi, int pPrivate, PROGRESS_CALLBACK progress_callback);

void avc_to_avi_close(int* pFd_avi);

typedef void (*PROGRESS_CALLBACK)(int iPercent, int pPrivate);

我在我的 C# dllimport 中使用它是这样的:

public delegate void PROGRESS_CALLBACK(int _iPercent, int _pPrivate);

[DllImportAttribute(@"..\xxx.dll", EntryPoint = "avc_to_avi_convert", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]

public static extern unsafe int avc_to_avi_convert([MarshalAs(UnmanagedType.LPStr)] StringBuilder _inputAVC, [MarshalAs(UnmanagedType.LPStr)] StringBuilder _ouputAVI, int pPrivate, PROGRESS_CALLBACK _pg);

[DllImportAttribute(@"..\xxx.dll", EntryPoint = "avc_to_avi_close", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]

public static extern unsafe void avc_to_avi_close(int pFd_avi);

private void button1_Click(object sender, EventArgs e)
{
    int i = 0;
    StringBuilder inputFile = new StringBuilder(Application.StartupPath + @"\avc\abc.avc");//(@"C:\avc\abc.avc");
    StringBuilder outputFile = new StringBuilder(Application.StartupPath + @"\avi\abc.avi");//(@"C:\avi\abc.avi");

    if (avc_to_avi_convert(
        inputFile,
        outputFile,
        1,
        progress_func) > 0) { 

    }
}

public void progress_func(int iProgress, int pPrivate)
{
     if (iProgress == 100)
     {
         //success
     }
     else if (iProgress == -1)
     {
         //convert error
     }
     else if (iProgress == -2)
     {
         //Not enough disk space
     }
     else
     {
         //update progress
     }
}

我将我的代码更改为,

[DllImportAttribute(@"..\xxx.dll", EntryPoint = "avc_to_avi_convert", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]

public static extern unsafe int avc_to_avi_convert([MarshalAs(UnmanagedType.BStr)] String _inputAVC, [MarshalAs(UnmanagedType.BStr)] String _ouputAVI, int pPrivate, PROGRESS_CALLBACK _pg);

[DllImportAttribute(@"..\xxx.dll", EntryPoint = "avc_to_avi_close", CallingConvention = CallingConvention.StdCall, CharSet = CharSet.Unicode)]

public static extern void avc_to_avi_close(ref int avi);

然后运行它,我仍然得到同样的错误。

最佳答案

1.) 您确定调用约定吗?试试 CallingConvention.StdCall。读这个:

http://blogs.msdn.com/b/adam_nathan/archive/2003/05/21/56690.aspx

2.) 为 CharSet 属性尝试不同的值,并使用 String 而不是 StringBuilder 作为路径参数。这是一个很好的引用:

http://msdn.microsoft.com/en-us/library/s9ts558h.aspx

3.) 此外,avc_to_avi_close 方法应如下所示:

[DllImportAttribute("xxx.dll", EntryPoint="avc_to_avi_close")]
public static extern void avc_to_avi_close(ref int avi);

4.) 我想尝试的其他事情:

  • 使用 CharSetMarshalAs 的不寻常组合。例如,您从上面的链接知道 Unicode 应该与 LPWStr 一起使用,而 Ansi 应该与 LPStr 一起使用,但您可以当然可以尝试其他组合。
  • 您的外部方法不需要标记为不安全

如果您已经用尽所有其他选项,为什么不尝试编写一个类似于您的 C# 代码的 C++ 程序:

  • 如果它崩溃了,你证明错误出在库中。
  • 如果它没有崩溃,你就知道错误是在你使用 PNovke 时出现的,你可以一个一个地检查我的答案中的所有 12 种以上的组合。

关于c# - 检测到缓冲区溢出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12766275/

相关文章:

c# - 如何防止图钉通过弹出窗口显示?

c++ - 使用 boost 序列化将 C++ 中的对象序列化为二进制格式以便通过套接字使用是否安全?

c++ - DUMPBIN 实用程序损坏的名称输出

c++ - 弱链接的实际应用是什么?

c++ - 在 C+ +'s Eigen library, how do I solve: invalid use of incomplete type ‘const class Eigen::MatrixSquareRootReturnValue<Eigen::Matrix<float, -1, -1>>’

c# - 通过引用传递到非托管 C DLL 函数后,结构保持不变

c# - 我是否在 C# 中正确编码了这个 C 函数?

c# - "&#64;"这样的字符是什么类型,如何转换成ASCII

C# Interfaces——只在其他接口(interface)中实现一个接口(interface)

c# - MVC 4 - 脚本/CSS 渲染问题