![](/img/trans.png)
[英]How to properly create an entry point on an external .dll resource written in C++ consumed in C#
[英]how to reference entry point in an external dll (C# / unity 3D)
我正在为需要使用串行端口的应用程序使用Unity 3D,但是在Unity中,缺少事件DataReceived
和其他链接函数的实现。 因此,我认为解决方案是使自己的DLL可以管理串行端口数据,并作为一个外部DLL统一调用它。
我写了下面的代码。 我的类库项目中的代码(只是为测试冻结了一个串行端口)
码:
using System;
using System.IO.Ports;
namespace serialPortManager2
{
public class laserManager
{
private SerialPort laserComPort;
public bool open()
{
bool laserSerialPortDetected = false;
// ouverture du port série pour pilotage du laser
foreach (string portName in SerialPort.GetPortNames())
{
if (portName == "COM4") laserSerialPortDetected = true;
}
if (laserSerialPortDetected)
{
laserComPort = new SerialPort("COM4", 9600, Parity.None, 8, StopBits.One);
laserComPort.Open();
if (laserComPort.IsOpen)
{
// timeout de lecture d'une mesure laser
laserComPort.ReadTimeout = 5000;
return true;
}
else return false;
}
else return false;
}
}
}
在Unity中引用DLL的代码:
using System.Collections;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using UnityEngine;
public static class SPNativePlugIn {
// The name of the external library containing the native functions
private const string LIBRARY_NAME = "serialPortManager2";
[DllImport(LIBRARY_NAME)] public static extern bool open();
}
我的问题是出现此错误: EntryPointNotFoundException: open
我想我必须在某个地方集成laserManager
类,但是我不知道语法,我尝试了很多事情,但找不到。
在“ Assets”文件夹中创建一个名为“ Plugins”的文件夹,然后将您的.dll文件放入“ Plugins”文件夹中,并using dllFileName;
以此方式将其导入脚本中using dllFileName;
适用于未包含在统一默认库中的每个.dll
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.