【问题标题】:DLLImport error: System.AccessViolationException with Manifest file and c#DLLImport 错误:带有清单文件和 c# 的 System.AccessViolationException
【发布时间】:2010-03-28 18:19:32
【问题描述】:

当尝试从具有带有 requireAdministrator 清单文件的 .net 应用程序调用 (DLLImport) 外部 c++ dll 时,在启用 UAC 的 Windows 7 中尝试从 C++ dll 调用函数时遇到此错误。

我调用的方法:EnCrypts

例外:

System.AccessViolationException:试图读或写保护 记忆。这通常表明其他内存已损坏。

代码:

public class BlowFish
{
    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String EnCrypt(String strData, String strPassword);

    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String EnCrypt(String strData, String strPassword, bool doNotUsePassChecking);

    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String DeCrypt(String strData, String strPassword, bool doNotUsePassChecking);

    [DllImport("BlowfishTool.dll", CharSet = CharSet.Auto)]
    public static extern String DeCrypt(String strData, String strPassword);


    public static String EnCrypts(String strData, String strPassword)
    {
        return EnCrypt(strData, strPassword, true);
    }
}

【问题讨论】:

  • Manifest 和 UAC 在这里无关紧要。该错误提示声明(参数、返回值、数据类型等)有问题,或者 dll 本身存在某种内存损坏。你能发布这些函数的 C++ 声明吗?

标签: c#


【解决方案1】:

这是因为 - 创建时的字符串为 4 个元素分配内存。如果返回参数的大小大于 4 就出错了。

必须使用 IntPtr 和 Marshal.PtrToStringAuto :-)

【讨论】:

    猜你喜欢
    • 2013-08-16
    • 1970-01-01
    • 1970-01-01
    • 2013-12-22
    • 1970-01-01
    • 2019-09-08
    • 1970-01-01
    • 2012-02-11
    • 2013-06-02
    相关资源
    最近更新 更多