【发布时间】:2021-05-25 06:48:18
【问题描述】:
我正在尝试通过我在 C# 应用程序中访问的现有 C++ DLL 连接到“Train Simulator 2021”。除了我知道的函数名称外,我不知道 DLL 的内容。我执行以下操作来访问这些功能:
namespace RailSimulatorLibrary
{
public class RailSimulatorInterface
{
[DllImport("C:\\Program Files (x86)\\Steam\\steamapps\\common\\RailWorks\\plugins\\RailDriver.dll", EntryPoint = "GetLocoName", ExactSpelling = false, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr GetLocoName();
}
}
然后在我的 Windows Form App 中调用这个函数:
private void Form1_Load(object sender, EventArgs e)
{
string currentLocoName = Marshal.PtrToStringAnsi(RailSimulatorInterface.GetLocoName());
Console.WriteLine(currentLocoName);
}
当我运行 32 位版本的 Train Sim 时,我得到了正确的返回值。但是,当以 64 位运行时,指针为零。我想知道 IntPtr 是否是 32 位返回类型,我需要为 64 位返回不同的类型?
我使用 RailDriver64.dll 进行 64 位测试,但在这种情况下,我得到一个空指针,即使 trainsim 已作为 64 位启动。
【问题讨论】:
-
您不能在 32 位和 640 位应用程序中使用相同的本机 dll
-
检查是否有
"RailDriver64.dll"文件。 -
在依赖查看器中打开 DLL
-
对不起。我必须编辑那个帖子。我实际上使用了 RailDriver64.dll。我测试了一下,指针是空的。将其设置为 32 位并仅使用 Raildriver.dll,显示正确的值(其余代码相同)