【问题标题】:Having an issue importing DLL into unity将 DLL 导入统一时遇到问题
【发布时间】:2019-11-08 17:04:37
【问题描述】:

我正在尝试将 user32.dll 放入我的统一免费版本。 (非专业)

我只是将 user32.dll 放到了我的 Assets/Plugins/ 文件夹中,它给我的错误是:

DLLNotFoundException: Assets/Plugins/user32.dll

这是我正在使用的代码:

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

public class DatabaseManager : MonoBehaviour
{



    //DLL imports
    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    static extern bool SetForegroundWindow(IntPtr hWnd);

    static Process proc = Process.GetProcessesByName("firefox")[0];
    IntPtr ptrFF = proc.Handle;
    //------------------------------------------------------------------------


    void Awake()
    {              
        SetForegroundWindow(ptrFF);
    }

   //Other code functions like Update etc.
}

我在某处听说非托管 dll (c++) 只能在 Unity Pro 中使用,但我真的需要这个 DLL,我是不是做的不准确???我不确定 user32.dll 是否真的属于非托管 c++ dll。

请帮忙。

【问题讨论】:

  • 什么 Unity 版本?什么构建平台?
  • Unity 2019.3.0a6 构建平台只是PC、Mac和Linux Standalone,目标平台是Windows Architecture x86_64
  • 最近几天经常这样说^^2019.3.0a6是一个Alpha版本..它充满了错误和错误并不少见..那是alpha 版本的整个想法。对于生产来说,它不稳定。也许首先尝试回到最新的稳定版本2019.1.8 并检查它是否在那里工作......然后也许看看NativePlugins Manual
  • @derHugo 可能与它是非托管 dll 有关吗?还是 C++ 代码?
  • @derHugo 我降级到 2019.1.8f1,我会告诉你发生了什么。

标签: unity3d user32


【解决方案1】:

将路径从Assets/Plugins/ 更改为Assets/Plugins/x86_64/

using UnityEngine;
using System.Runtime.InteropServices;

public class DatabaseManager : MonoBehaviour {

    [DllImport("user32.dll")] static extern int GetForegroundWindow();

    [DllImport("user32.dll", EntryPoint="MoveWindow")]  
    static extern int  MoveWindow (int hwnd, int x, int y,int nWidth,int nHeight,int bRepaint);

    void Awake()
    {
        int handle = GetForegroundWindow();
        Debug.Log(handle);

        int fWidth  = Screen.width;
        int fHeight = Screen.height;
        // Move the Unity windows.
        MoveWindow(handle,0,0,fWidth,fHeight,1); 

    }
}

【讨论】:

  • 无变化:DLLNotFoundException: Assets/PLugins/x86_64/user32.dll
  • 你的脚本后端是什么?
  • 什么意思?我怎么得到它?我只是在 C# 中的 Visual Studio IDE 中编码
  • 我已将 DLLImport 更改为您提到的内容,没有任何变化。我是否需要在我的 VS 中添加引用之类的操作?我已经尝试过这种方法,但不知道我在做什么。我应该可以将它拖放到 assets/plugins/x86_64/ 文件夹中然后引用它,不是吗?目标框架是 .net 4.6.1
  • 每个问题只问一个问题。创建一个新的。
猜你喜欢
  • 1970-01-01
  • 2014-08-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多