撰寫了一個用于在 C 中顯示系統訊息的庫
。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
#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);
}
我試圖通過 Unity 呼叫庫函式,沒有任何反應。沒有錯誤,什么都沒有。也許我做錯了什么?
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);
}
}
后來,我嘗試在我的代碼中呼叫這些函式。為了測驗。Unity 上的訊息中沒有任何內容。我將庫放入 Assets 并放入根專案和插件中。全部都一樣。
uj5u.com熱心網友回復:
C# 不能編組System.String,std::string因為有不同的類。
UnmanagedType.BStr在 C# 中是BSTR在 Windows SDK 中。
而且你已經設定SetLastError = true了,所以當什么都沒有出現時,你應該打電話Marshal.GetLastWin32Error來獲取錯誤。
其實你可以用這個方法來呼叫MessageBox。
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int MessageBox(IntPtr hWnd, String text, String caption, uint type);
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/508682.html
標籤:C#C unity3d
上一篇:字符卡在瓷磚上unity3d
