【问题标题】:Unity3D call external dllunity3D调用外部dll
【发布时间】:2015-03-25 14:46:29
【问题描述】:

我正在尝试为 Unity3D Pro (5.0) 构建本机插件。到目前为止,我已经在 VS express 2013 for Windows 中构建了一个 DLL 文件,我为此创建了一个示例 Unity 项目并链接了库,但我仍然收到错误,我似乎无法移动。谷歌在这件事上没有太大帮助......

我正在尝试为 Windows Store 目标添加一个包含我自己的低级内容的 DLL。

我试图以这种方式访问​​的东西并不重要,我被困在hello world 示例应用程序中。

Visual Studio 项目

Dll3.cpp

#include "pch.h"
#include "Dll3.h"

extern "C" __declspec(dllexport) int testFunc(int a, int b) {
    return a + b;
}

DLL3.h

#pragma once

using namespace std;

extern "C" __declspec(dllexport) int testFunc(int a, int b);

dllmain.cpp

#include "pch.h"

BOOL APIENTRY DllMain(HMODULE /* hModule */, DWORD ul_reason_for_call, LPVOID /* lpReserved */)
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
     return TRUE;
}

pch.h

#pragma once

#include "targetver.h"

#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
#endif

// Windows Header Files:
#include <windows.h>

pch.cpp

#include "pch.h"

我将构建目标设置为 x64,DLL 文件成功导出,没有任何错误或警告。

团结项目

我将Dll3.dll 文件放入Assets/ 文件夹。起初,我在Assets/Plugins/ 有它,但我发现它根本不重要。

test.cs

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

public class test : MonoBehaviour {

    // Dll import according to Unity Manual
    #if UNITY_IPHONE || UNITY_XBOX360
    [DllImport ("__Internal")]
    #else
    [DllImport ("Dll3")]
    #endif
    private static extern int testFunc (int a, int b);

    // Use this for initialization
    void Start () {
        int a = 1;
        int b = 2;
        int c = testFunc (a, b);
        Debug.Log (c.ToString ());
    }

    // Update is called once per frame
    void Update () {

    }
}

然后我创建了一个空的游戏对象,将这个脚本分配给它,编译并运行。它编译时没有任何错误或警告,但是在运行时(在编辑器中),我得到了这个:

Failed to load 'Assets/Dll3.dll' with error 'This operation is only valid in the context of an app container.', GetDllDirectory returned ''. If GetDllDirectory returned non empty path, check that you're using SetDirectoryDll correctly.
Failed to load 'Assets/Dll3.dll' with error 'This operation is only valid in the context of an app container.', GetDllDirectory returned ''. If GetDllDirectory returned non empty path, check that you're using SetDirectoryDll correctly.
DllNotFoundException: Assets/Dll3.dll
    test.Start () (at Assets/test.cs:18)

谁能指出我在哪里做错了?我习惯了 Unix (OSX/Linux) 环境和 Windows 对我来说非常不标准。我不完全理解 VS DLL 项目的概念。如果有任何帮助,我将不胜感激。谢谢

【问题讨论】:

  • 我怀疑 VisualStudio 部分......这个最让我困惑。我不确定我是否正确导出了函数,我不确定我是否可以像这样在 VC++ 中导出纯 C 函数……我当然会欢迎客观方法,但结构化方法也可以……
  • 最好将 Plugins 文件夹用于本地 DLL,尽管听起来这不是问题。
  • 您为什么使用 Windows 应用商店目标?我觉得Win32比较合适。
  • 我需要win32目标没有的touch SDK,模块应该弥补win store平台少库的损失
  • 我在 Plugins 文件夹中遇到了完全相同的错误...如果缺少库,我会收到不同的“丢失”错误...所以这不是问题

标签: c# c++ dll visual-studio-2013 unity3d


【解决方案1】:

是的,从一开始就是正确的。我只需要在 Visual Studio Simulator 中运行它,作为我桌面上的商店应用程序或将其部署到我的开发平板电脑上。它不在 Unity 编辑器中运行 - 我真是个傻瓜 :-)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-01-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多