【问题标题】:Unable to execute extern c function in Unity 5无法在 Unity 5 中执行外部 c 函数
【发布时间】:2019-02-22 12:25:27
【问题描述】:

目前我正在尝试将CEF 嵌入到 Unity 中。我已经设法将 cefsimple 示例编译为 dll,并通过使用 DllImport 和以下代码在普通控制台应用程序中使用它:

[DllImport("cefsimple")]
public static extern ulong StartCustom();
static void Main(string[] args)
{
    StartCustom();
}

及对应的C函数:

extern "C" __declspec (dllexport) int StartCustom() {
    return wWinMain(nullptr, nullptr, nullptr, 0);
}

(我已将所有必要的文件移动到包含已创建 .exe 文件的文件夹中)

但如果我尝试在 Unity 中做同样的事情,我会得到 ​​p>

Failed to load 'Assets/Plugins/cefsimple.dll' with error 'The specified procedure could not be found.

DllNotFoundException: cefsimple
Launcher.Init () (at Assets/Plugins/Launcher.cs:13)

有什么办法可以解决这个问题吗?

【问题讨论】:

  • Assets/Plugins/cefsimple.dll 看起来像一个相对路径,DllNotFoundException 看起来像找不到 DLL 本身...你能设置一个绝对路径来检查吗?
  • 另一条线索:您的 dll 是否与未复制到同一目录中的其他 dll 有依赖关系?
  • 感谢您的快速回答。我尝试替换路径并收到以下错误:DllNotFoundException: C:\Users\rstoliar\Documents\CefExample\Assets\Plugins\cefsimple。但是第一个错误(Failed to load 'Assets/Plugins/cefsimple.dll' with error 'The specified procedure could not be found)消失了。
  • 我复制了与普通控制台应用程序相同的文件。
  • 绝对是依赖 DLL 问题。尝试使用依赖漫游器工具打开您的 DLL,看看缺少哪些。

标签: c# c++ c unity3d ffi


【解决方案1】:

首先,你最先在你的 c 应用程序中定义 dll 导入和导出指令,如下所示:

#ifndef YOUR_SOURCE_NAME_H //header guard
#define YOUR_SOURCE_NAME_H

    #define YOUR_SOURCE_NAME_EXPORT (1)

    #ifdef YOUR_SOURCE_NAME_EXPORT
        #define DLL_USAGE_MODE extern "C"  __declspec(dllexport)   // export DLL information
    #else
        #define DLL_USAGE_MODE  extern "C" __declspec(dllimport)   // import DLL information
    #endif


#endif // YOUR_SOURCE_NAME_H

您最常将 DLL_USAGE_MODE 放在您的函数名之前,以使其可从外部访问。 例如,您的 c DLL 中有以下函数:

DLL_USAGE_MODE int saySomthing(const char* pText) {
    printf("\n%s", pText);
    return 0;
}

现在在您的 c# 应用程序中:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

class Program {
    [DllImport("address_of_your_dll.dll", EntryPoint = "saySomthing", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
    internal static extern int saySomthing(string text);

    static void Main(string[] args) {
        saySomthing("HelloWorld !!");
    }
}

【讨论】:

    猜你喜欢
    • 2018-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-23
    • 2017-12-11
    • 2021-09-04
    • 2013-08-07
    相关资源
    最近更新 更多