【问题标题】:Use a C++ dll in c#在 C# 中使用 C++ dll
【发布时间】:2020-10-14 12:18:40
【问题描述】:

我想在 c# 代码中使用 C++ dll 示例(使用 c++ dll):

    class Program
    {
        [DllImport(@"netcoreapp3.1\savedecrypter.dll", CallingConvention = CallingConvention.Cdecl)]
        public static extern string decryptSave(string o);

        static void Main(string[] args)
        {
            string ja = "dhsabdasji";
            string show = decryptSave(ja);
            Console.WriteLine(show);
            Console.ReadLine();
        }

    }
**and this is the EXAMPLE C++ dll code(im just a newbie in c++ btw)**
 extern "C"
 {
      __declspec(dllexport) string decryptSave(string path21)
      {
          
          return path21;

      }


 }

尝试调用它时出错 “System.DllNotFoundException:'无法加载 DLL'netcoreapp3.1\savedecrypter.dll' 或其依赖项之一:找不到指定的模块。(0x8007007E)'” 谁能帮我解决这个问题?如果有人可以这样做,将不胜感激。

编辑:现在我启用了本机代码调试,它显示此错误:

未处理的异常:System.AccessViolationException:试图读取或写入受保护的内存。这通常表明其他内存已损坏。

【问题讨论】:

  • 也许只是@"savedecrypter.dll" ?
  • 您是否尝试将 savedecrypter.dll 的位置添加到系统路径中然后只是 @"savedecrypter.dll"?
  • 尝试使用绝对路径或使用两个 '\\'。此外,我不确定您的 dll 是否会执行预期的操作。请看stackoverflow.com/questions/32991274/…

标签: c# c++ string dll call


【解决方案1】:

您的“savedecrypter”DLL 可能调用了您机器中缺少的另一个 DLL。检查您是否拥有所有必需的驱动程序,这是使用第三方“黑盒”DLL 时的常见错误。

【讨论】:

  • 现在我收到此错误 Unhandled Exception: System.AccessViolationException: Attempted to read or write protected memory。这通常表明其他内存已损坏。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-10-09
  • 2011-04-03
  • 2017-03-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多