简体   繁体   中英

Unity3D call external dll

I am trying to build a native plugin for Unity3D Pro (5.0). So far, I have built a DLL file in VS express 2013 for Windows, I have created a sample Unity project just for that and linked the library, but I am still getting an error and I cannot seem to move. Google wasn't very helpful in that matter ...

I am trying to add a DLL with my own low-level stuff for Windows Store target.

The stuff I am trying to access this way doesn't matter, I am stuck at hello world example app.

Visual Studio project

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"

and I set the build target to x64, the DLL file exported successfully without any errors or warnings.

Unity Project

I plopped the Dll3.dll file to the Assets/ folder. At first, I had it in Assets/Plugins/ but I figured out it didn't matter at all.

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 () {

    }
}

Then I created an empty game object, assigned this script to it, compiled and run. It compiled without any errors or warnings, but when running (in Editor), I got this:

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)

Can anyone, please point me to where I am doing the mistake? I am used to Unix (OSX/Linux) environment and Windows very non-standard for me. I do not understand completely the concept of the VS DLL project. I would be grateful for any help. Thank you

Yeah, it was correct from the start. I just needed to run it in Visual Studio Simulator, as a Store App on my desktop or deploy it to my development tablet PC. It does NOT run in the Unity Editor - I am such a fool :-)

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