从 Unity3d 调用本机 DLL 的最佳方式是什么?

What is the best way to call a native DLL, from Unity3d?

大家好,我在 unity3d pro 上遇到了一些问题,也许使用过 DLL 本机插件的人可以阐明我做错了什么。

首先,我将解释我的目标是什么。我有一个来自西门子 PLCSim 软件的 COM 对象,我已经得到它可以很好地与 Visual Studio 一起工作。下面是测试代码。

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public S7PROSIMLib.S7ProSim PLCSimConn = new S7PROSIMLib.S7ProSim();

        public Form1()
        {
            InitializeComponent();
        }

        private void button_Connect_Click(object sender, EventArgs e)
        {
            PLCSimConn.Connect();
            label_CPUState.Text = PLCSimConn.GetState();
            label_ScanMode.Text = PLCSimConn.GetScanMode().ToString();
        }
    }
}

我创建了一个 unity3d 项目来测试它。我将 dll 导入到我的资产中,并且能够从我的 c# 脚本中调用 S7PROSIMLib 的方法和实例。 COM Api 这里,https://cache.industry.siemens.com/dl/files/855/1139855/att_29424/v1/S7WSPSCB.pdf

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
using S7PROSIMLib;

public class TestNative : MonoBehaviour {

    public S7ProSimClass ps; 

    // Use this for initialization
    void Start () {

        ps = new S7ProSimClass ();
        ps.Connect ();
        Debug.Log (ps.GetState());

    }

}

现在在 运行 时我得到以下信息:

COMException

System.Runtime.InteropServices.Marshal.ThrowExceptionForHR (Int32 errorCode) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.InteropServices/Marshal.cs:1031)
System.__ComObject.Initialize (System.Type t) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System/__ComObject.cs:103)
(wrapper remoting-invoke-with-check) System.__ComObject:Initialize (System.Type)
Mono.Interop.ComInteropProxy.CreateProxy (System.Type t) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/Mono.Interop/ComInteropProxy.cs:108)
System.Runtime.Remoting.RemotingServices.CreateClientProxyForComInterop (System.Type type) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Remoting/RemotingServices.cs:588)
System.Runtime.Remoting.Activation.ActivationServices.CreateProxyForType (System.Type type) (at /Users/builduser/buildslave/mono-runtime-and-classlibs/build/mcs/class/corlib/System.Runtime.Remoting.Activation/ActivationServices.cs:234)
TestNative.Start () (at Assets/Scripts/TestNative.cs:13)

看过unity3d插件教程https://www.youtube.com/watch?v=DfRYLwG1Bug不调用第三方DLL

我已阅读以下内容:https://msdn.microsoft.com/en-us/library/ms973872.aspx 其中有很多关于非托管和托管 DLL 的有用信息。

基于上面 link 中的以下片段:

调用 COM API 有两种方法可以从托管代码调用 COM 组件:通过 COM 互操作(在所有托管语言中可用)或通过 C++ 互操作(在 C++ 中可用)。 对于调用 OLE Automation 兼容的 COM 组件,建议使用 COM interop。 CLR 将负责 COM 组件激活和参数编组。

对于基于接口定义语言(IDL)调用COM组件,建议使用C++互操作。 C++ 层可以非常薄,其余的托管代码可以用任何托管语言编写。 COM 互操作依赖于类型库中的信息来进行正确的互操作调用,但类型库通常不包含 IDL 文件中存在的所有信息。使用 C++ 互操作通过允许直接访问这些 COM API 解决了这个问题。

我相信我已经完成了在上面的 unity c# 脚本中显示的 COM 互操作。那没有用。我的另一个选择是通过 C++ 互操作,但我没有在网上找到任何示例。大多数示例都很简单,无需调用 COM 对象或任何其他第三方 DLL。如果有人能以正确的方式指导我,我将不胜感激。基本上它归结为解决这个问题的最佳方法是什么,只需最少的方法重写?谢谢!

Edit1:我在 youtube 上观看了这个视频,有人让它开始工作,但我还没有得到他的回应。至少我知道它应该适用于 unity3d。 https://www.youtube.com/watch?v=EGFMjUJN7ZU

Edit2: 将按照 post 建议尝试使用 32 位编辑器。

对于可能需要这方面帮助的任何人。 Siemens PLCSim COM 类型库仅适用于 32 位 Unity Editor。

经过大量试验和错误,以及通过论坛阅读更多内容后,解决方案实际上非常简单。我使用 tlbimp.exe 生成一个类型库 DLL,然后我将其放入 assets/plugins 文件夹中,同时注意 unity3d 编辑器属性,新的 DLL 被视为托管代码。请注意,这个特定的 DLL 不能在 64 位 Unity Editor 上运行,它会抛出 COMException。然而,新类型库 DLL 在 32 位 Unity 编辑器中运行良好,我想在 DLL 的某处有一条指令指定仅在 32 位中工作。

我在此处的存储库中记录了所有内容 https://github.com/fredz0003/myS7ProSimLib 我还放置了托管 DLL 以供将来使用,这样您就不会遇到我遇到的麻烦。

下面是示例代码:

using UnityEngine; using myS7ProSimLib;

public class TestNative : MonoBehaviour {

    /*
    * Used tlbimp.exe to generate library DLL
    * place new generated DLL on assets/plugins
    * also note that the DLL is treated as managed code
    */
    public S7ProSimClass ps;
    public bool input_0_0;


    void Start () {
        ps = new S7ProSimClass();

        ps.Connect();
        print("State " + ps.GetState());
        ps.SetScanMode(ScanModeConstants.ContinuousScan);

        // Here we pass the ref as an obj, since WriteInputPoint method
        // can take bit, word, dwords, as addresses ref obj can take the
        // for of bool, int, float, etc.
        object refInput0_0 = input_0_0;

        ps.WriteInputPoint(0, 0, ref refInput0_0);  }    }