简体   繁体   中英

Import in C# bundle written in c++ (Unity3D project)

I try to use the functions from a .bundle file inside Unity3D but every time when I call the functions I get the error:

DllNotFoundException: libant
connectANT.Start () (at Assets/connectANT.cs:13)

This is the script that I use to call the library antlib.bundle which is in the Assets/Plugin folder:

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

public class connectANT : MonoBehaviour {
[DllImport("libant")] static extern bool ANT_Init(int ucUSBDeviceNum_, int ulBaudrate_); 
void Start () {
    ANT_Init (1, 50000);
}
}

in the bundle this function is declared like this:

#ifdef __cplusplus
extern "C" {
#endif
EXPORT BOOL ANT_Init(UCHAR ucUSBDeviceNum_, ULONG ulBaudrate_);  //Initializes and opens USB connection to the module
#ifdef __cplusplus
}
#endif

This is just an example with only one function from the bundle. If I just import the functions in my script I don't get any error in Unity. Can someone help me to figure it out?

try this:

[DllImport("antlib.bundle")]
static extern int ANT_Init(byte ucUSBDeviceNum_, uint ulBaudrate_); 

there're two problems in your code:

  1. wrong file name
  2. wrong argument type and return type

if you want to use bool as return type, another attribute is required:

[return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
[DllImport("antlib.bundle")]
static extern bool ANT_Init(byte ucUSBDeviceNum_, uint ulBaudrate_); 

Sam is right, your return value and arguments have to match. Also, make sure you bundle includes info.plist with following:

<key>CFBundleExecutable</key>
<string>antlib.dylib</string>

your bundle name and your library name do not have to match, but your plist needs to fix this by pointing to the right lib.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM