简体   繁体   English

Unity 3D 中无法调用动态库

[英]Unable to call dynamic library in Unity 3D

Wrote a library for displaying system messages in C++编写了一个库,用于在 C++ 中显示系统消息

.h 。H

#pragma once

#ifndef WINDOW_DEFINDE_DEBUG
#define WINDOW_DEFINDE_DEBUG

#include <Windows.h>
#include <string>

#ifdef __cplusplus
extern "C" {
#endif

#ifndef BUILD_LIBRARY
#define SHARED_LIBRARY_DECLSPEC __declspec(dllexport)
#else
#define SHARED_LIBRARY_DECLSPEC __declspec(dllimport)
#endif

    int SHARED_LIBRARY_DECLSPEC DebugWindowInformation(std::string message, std::string caption);
    int SHARED_LIBRARY_DECLSPEC DebugWindowQuestion(std::string message, std::string caption);
    int SHARED_LIBRARY_DECLSPEC DebugWindowWarning(std::string message, std::string caption);
    int SHARED_LIBRARY_DECLSPEC DebugWindowError(std::string message, std::string caption);

#ifdef __cplusplus
}
#endif

#endif

.cpp .cpp

#include "window_debug.h"

#define BUILD_LIBRARY

int SHARED_LIBRARY_DECLSPEC DebugWindowInformation(std::string message, std::string caption)
{
    std::wstring _message = std::wstring(message.begin(), message.end());
    std::wstring _caption = std::wstring(caption.begin(), caption.end());

    return MessageBox(NULL, (LPCWSTR)_message.c_str(), (LPCWSTR)_caption.c_str(),
        MB_ICONINFORMATION | MB_OK | MB_DEFBUTTON1);
}

int SHARED_LIBRARY_DECLSPEC DebugWindowQuestion(std::string message, std::string caption)
{
    std::wstring _message = std::wstring(message.begin(), message.end());
    std::wstring _caption = std::wstring(caption.begin(), caption.end());

    return MessageBox(NULL, (LPCWSTR)_message.c_str(), (LPCWSTR)_caption.c_str(),
        MB_ICONQUESTION | MB_YESNOCANCEL | MB_DEFBUTTON3);
}

int SHARED_LIBRARY_DECLSPEC DebugWindowWarning(std::string message, std::string caption)
{
    std::wstring _message = std::wstring(message.begin(), message.end());
    std::wstring _caption = std::wstring(caption.begin(), caption.end());

    return MessageBox(NULL, (LPCWSTR)_message.c_str(), (LPCWSTR)_caption.c_str(),
        MB_ICONWARNING | MB_OK | MB_DEFBUTTON1);
}

int SHARED_LIBRARY_DECLSPEC DebugWindowError(std::string message, std::string caption)
{
    std::wstring _message = std::wstring(message.begin(), message.end());
    std::wstring _caption = std::wstring(caption.begin(), caption.end());

    return MessageBox(NULL, (LPCWSTR)_message.c_str(), (LPCWSTR)_caption.c_str(),
        MB_ICONERROR | MB_OK | MB_DEFBUTTON1);
}

I'm trying to call the library functions through Unity, nothing happens.我试图通过 Unity 调用库函数,没有任何反应。 No errors, nothing at all.没有错误,什么都没有。 Maybe I'm doing something wrong?也许我做错了什么?

C# C#

namespace Assets.Scripts.InvokeCallFunc
{
    public static class CallFunc
    {
        [DllImport(@"C:\Users\mrxit\Downloads\King Of Kings 3\Project\eh_handler32_64.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        public static extern int DebugWindowInformation([MarshalAs(UnmanagedType.BStr)] string message, [MarshalAs(UnmanagedType.BStr)] string caption);

        [DllImport(@"C:\Users\mrxit\Downloads\King Of Kings 3\Project\eh_handler32_64.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        public static extern int DebugWindowQuestion([MarshalAs(UnmanagedType.BStr)] string message, [MarshalAs(UnmanagedType.BStr)] string caption);

        [DllImport(@"C:\Users\mrxit\Downloads\King Of Kings 3\Project\eh_handler32_64.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        public static extern int DebugWindowWarning([MarshalAs(UnmanagedType.BStr)] string message, [MarshalAs(UnmanagedType.BStr)] string caption);

        [DllImport(@"C:\Users\mrxit\Downloads\King Of Kings 3\Project\eh_handler32_64.dll", CallingConvention = CallingConvention.Cdecl, SetLastError = true)]
        public static extern int DebugWindowError([MarshalAs(UnmanagedType.BStr)] string message, [MarshalAs(UnmanagedType.BStr)] string caption);
    }
}

Later, I try to call these functions in my code.后来,我尝试在我的代码中调用这些函数。 For a test.为了测试。 Nothing comes out in the messages on Unity. Unity 上的消息中没有任何内容。 I threw the library into Assets and into the root project and into Plugins.我将库放入 Assets 并放入根项目和插件中。 All the same.全部都一样。

C# cannot marshal System.String as std::string because there different classes. C# 无法将System.String编组为std::string因为有不同的类。

UnmanagedType.BStr in C# is BSTR in Windows SDK. C# 中的UnmanagedType.BStr是 Windows SDK 中的BSTR

And you've set SetLastError = true , so when nothing comes out, you should call Marshal.GetLastWin32Error to get error.而且你已经设置了SetLastError = true ,所以当什么都没有出现时,你应该调用Marshal.GetLastWin32Error来获取错误。

In fact you can use this method to call MessageBox .实际上,您可以使用此方法调用MessageBox

[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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