You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C++游戏引擎嵌入C#脚本调用Console.WriteLine遇System.DllNotFoundException

在C++游戏引擎中嵌入Mono调用C# Console.WriteLine时抛出DllNotFoundException

问题背景

我正在尝试将C#脚本嵌入到C++游戏引擎项目中,参考了Mono相关示例。目前代码能正常运行,也能访问C#类的方法,但调用Console.WriteLine()时会抛出异常,无法使用System相关函数。

相关代码

Dog.cs

public class Dog
{
    static public void Type()
    {
        Console.WriteLine("a Dog!");
    }

    // 其他代码...
}

错误日志

Running the static method: Dog:Type()

Unhandled Exception:
System.TypeInitializationException: The type initializer for 'System.Console' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.ConsoleDriver' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Sys' threw an exception. ---> System.DllNotFoundException: System.Native assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) Interop+Sys.LChflagsCanSetHiddenFlag()
  at Interop+Sys..cctor () [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
   --- End of inner exception stack trace ---
  at System.IO.FileSystem.FileExists (System.ReadOnlySpan`1[T] fullPath, System.Int32 fileType, Interop+ErrorInfo& errorInfo) [0x00007] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.IO.FileSystem.DirectoryExists (System.ReadOnlySpan`1[T] fullPath, Interop+ErrorInfo& errorInfo) [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.IO.FileSystem.DirectoryExists (System.ReadOnlySpan`1[T] fullPath) [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.IO.Directory.Exists (System.String path) [0x0001e] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.TermInfoDriver.SearchTerminfo (System.String term) [0x00044] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.TermInfoDriver..ctor (System.String term) [0x0004b] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term) [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.ConsoleDriver..cctor () [0x0004d] in <de882a77e7c14f8ba5d298093dde82b2>:0 
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding, System.Text.Encoding outputEncoding) [0x00007] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Console..cctor () [0x0007d] in <de882a77e7c14f8ba5d298093dde82b2>:0 
   --- End of inner exception stack trace ---
  at Dog.Type () [0x00000] in <f155b242589444d6a2f6baf230b1c326>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.TypeInitializationException: The type initializer for 'System.Console' threw an exception. ---> System.TypeInitializationException: The type initializer for 'System.ConsoleDriver' threw an exception. ---> System.TypeInitializationException: The type initializer for 'Sys' threw an exception. ---> System.DllNotFoundException: System.Native assembly:<unknown assembly> type:<unknown type> member:(null)
  at (wrapper managed-to-native) Interop+Sys.LChflagsCanSetHiddenFlag()
  at Interop+Sys..cctor () [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
   --- End of inner exception stack trace ---
  at System.IO.FileSystem.FileExists (System.ReadOnlySpan`1[T] fullPath, System.Int32 fileType, Interop+ErrorInfo& errorInfo) [0x00007] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.IO.FileSystem.DirectoryExists (System.ReadOnlySpan`1[T] fullPath, Interop+ErrorInfo& errorInfo) [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.IO.FileSystem.DirectoryExists (System.ReadOnlySpan`1[T] fullPath) [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.IO.Directory.Exists (System.String path) [0x0001e] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.TermInfoDriver.SearchTerminfo (System.String term) [0x00044] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.TermInfoDriver..ctor (System.String term) [0x0004b] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.ConsoleDriver.CreateTermInfoDriver (System.String term) [0x00000] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.ConsoleDriver..cctor () [0x0004d] in <de882a77e7c14f8ba5d298093dde82b2>:0 
   --- End of inner exception stack trace ---
  at System.Console.SetupStreams (System.Text.Encoding inputEncoding, System.Text.Encoding outputEncoding) [0x00007] in <de882a77e7c14f8ba5d298093dde82b2>:0 
  at System.Console..cctor () [0x0007d] in <de882a77e7c14f8ba5d298093dde82b2>:0 
   --- End of inner exception stack trace ---
  at Dog.Type () [0x00000] in <f155b242589444d6a2f6baf230b1c326>:0

已尝试操作

  1. 调用mono_set_dirs("/usr/lib/", "/etc/")设置Mono的DLL查找路径,/usr/lib/mono/4.5/下确实存在对应DLL。
  2. 将/usr/lib/mono/4.5/下的System.dll复制到项目目录并手动调用mono_domain_assembly_open(domain, "System.dll")加载,但问题未解决。

环境信息

  • 系统:Ubuntu 22.04 LTS
  • 已本地安装Mono,并通过.so文件链接到项目中

解决方案

原因分析

错误中的System.DllNotFoundException并非找不到托管的System.dll,而是找不到原生的System.Native.so库——这是Mono实现System类库底层功能的原生依赖,Console.WriteLine()的终端交互逻辑依赖这个库。

修复步骤

  1. 确认原生库路径
    在Ubuntu 22.04中,System.Native.so通常位于/usr/lib/mono-native/或/usr/lib/x86_64-linux-gnu/目录下。可以用命令定位:

    find /usr -name "System.Native.so"
    
  2. 确保程序能加载到该库

    • 方法一:将找到的System.Native.so复制到项目可执行文件所在目录,或者添加其所在路径到LD_LIBRARY_PATH环境变量:
      export LD_LIBRARY_PATH="/usr/lib/mono-native/:$LD_LIBRARY_PATH"
      
      然后运行游戏引擎程序。
    • 方法二:在编译C++项目时,通过链接器参数指定该库的路径,例如:
      -L/usr/lib/mono-native/ -lSystem.Native
      
  3. 替代方案:自定义控制台输出
    如果不想依赖原生库,可以在C#中实现自定义的输出方法,通过Mono的互操作回调到C++的日志系统:

    • C#端:
      public static class Logger
      {
          [System.Runtime.InteropServices.DllImport("YourGameEngine")]
          private static extern void LogMessage(string msg);
      
          public static void WriteLine(string msg)
          {
              LogMessage(msg);
          }
      }
      
    • C++端实现LogMessage函数:
      extern "C" void LogMessage(const char* msg)
      {
          // 调用引擎的日志输出逻辑,比如printf或自定义日志系统
          printf("%s\n", msg);
      }
      

    之后在C#代码中使用Logger.WriteLine()替代Console.WriteLine()。


内容的提问来源于stack exchange,提问作者Pierre Mauger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.09 09:00:57