我有一个静态库(* .a for iOS),其中包含一些我需要分配给C#回调的函数。该代码工作正常,没有回调,但是当我将委托添加到结构时,出现以下错误:如何设置DLL的代理/回调(C和C#之间的互操作)
ArgumentException: The specified structure must be blittable or have
layout information. Parameter name: structure at
FMOD_Listener.LoadPlugins() [0x00000] in <filename unknown>:0 at
FMOD_Listener.Initialize() [0x00000] in <filename unknown>:0
(Filename: currently not available on il2cpp Line: -1)
这里是原生代码(C):
extern "C" {
typedef void (F_CALLBACK *basic_callback) (int *value1);
typedef struct telephone
{
int area_code;
int number;
basic_callback basic_callbck;
} TELEPHONE;
F_DECLSPEC F_DLLEXPORT void F_STDCALL AigooRegisterPhone(TELEPHONE **telephone);
void F_CALLBACK aigoo_basic_callback(int *value1)
{
*value1 = *value1 * 10 ;
}
F_DECLSPEC F_DLLEXPORT void F_STDCALL AigooRegisterPhone(TELEPHONE **telephone)
{
TELEPHONE* myPhone = new TELEPHONE();
myPhone->area_code = 929;
myPhone->number = 823;
myPhone->basic_callbck = aigoo_basic_callback;
*telephone = myPhone;
}
}
这是管理侧C#:
public delegate void basic_callback (ref int value1);
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct TELEPHONE
{
public int area_code;
public int number;
public basic_callback basic_callbck;
}
public class FMODPlugInHandler {
[DllImport ("__Internal")]
public static extern void AigooRegisterPhone(out IntPtr TelephonePtr);
}
public class FMOD_Listener : MonoBehaviour
{
...
void LoadPlugins()
{
int plugin_result = 0;
if (Application.platform == RuntimePlatform.IPhonePlayer) {
IntPtr PhoneIntPtr;
FMODPlugInHandler.AigooRegisterPhone(out PhoneIntPtr);
plugin_result = 823823823;
myLog = "plugin_result = " + plugin_result + " PhoneIntPtr: " + PhoneIntPtr;
if (PhoneIntPtr != IntPtr.Zero){
TELEPHONE MyPhone = (TELEPHONE)Marshal.PtrToStructure(PhoneIntPtr, typeof(TELEPHONE));
plugin_result = 123456;
myLog = "result = " + plugin_result + " number: " + MyPhone.number ;
int int_cs = 2;
plugin_result = MyPhone.basic_callbck(ref int_cs);
myLog = "result = " + plugin_result + " number: " + MyPhone.number + " int_cs: " + int_cs;
}
}
}
...
}
嗨@LPs,感谢您的回复。我刚刚尝试了你的建议。有同样的错误:'ArgumentException:指定的结构必须是blittable或有布局信息。 参数名称:结构 在FMOD_Listener.LoadPlugins()[0x00000]在<文件名未知>:0 在FMOD_Listener.Initialize()[0x00000]在<文件名未知>:0 (文件名:当前不可用的il2cpp线: -1)' –