gpt4 book ai didi

c# - MonoTouch 绑定(bind)不能使用字段

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:23:48 24 4
gpt4 key购买 nike

你好,我正在尝试将 google admboads 更新到 V6,但我在绑定(bind)以下内容并将其暴露给托管世界时遇到了一些麻烦

我有以下结构

typedef struct GADAdSize {
CGSize size;
NSUInteger flags;
} GADAdSize;

我在单点触控方面做了这个

[StructLayout(LayoutKind.Sequential)]
public struct GADAdSize
{
public SizeF size;
public uint flags;
}

我有以下代码

extern GADAdSize const kGADAdSizeBanner;
extern GADAdSize const kGADAdSizeMediumRectangle;

我无法使用 [Field] 属性绑定(bind)它,因为文档指定 Field 属性只能用于

  • NSString 引用
  • NSArray 引用
  • 32 位整数 (System.Int32)
  • 32 位 float (System.Single)
  • 64 位 float (System.Double)

所以我尝试了以下2种我能想到的方法

[DllImport ("__Internal")]
extern static IntPtr kGADAdSizeMediumRectangle ();

public static GADAdSize MediumRectangle
{
get
{
object obj = Marshal.PtrToStructure(kGADAdSizeMediumRectangle(), typeof(GADAdSize));
return (GADAdSize) obj;
}
}

public static GADAdSize Banner 
{
get
{
var handle = Dlfcn.dlopen ("libGoogleAdMobAds", 0);
IntPtr ptr = Dlfcn.GetIntPtr(handle, "kGADAdSizeBanner");
Dlfcn.dlclose (handle);
object obj = Marshal.PtrToStructure(ptr, typeof(GADAdSize));
return (GADAdSize) obj;
}
}

并且在这两个方面它都崩溃了

使用 DLLImport 我在调用 Marshal.PtrToStructure() 时得到一个 nullargument 异常,第二个异常引发 DLLNotFoundException System.ArgumentNullException

在此先感谢您的帮助

亚历克斯


编辑:

对不起@Poupou 我的错,它抛出 System.ArgumentNullException 句柄值为 0,ptr 值为 0

堆栈跟踪是:

System.ArgumentNullException: Argument cannot be null.
Parameter name: src
at (wrapper managed-to-native) System.Runtime.InteropServices.Marshal:PtrToStructure (intptr,System.Type)
at AlexTouch.GoogleAdMobAds.GADAdSizeConstants.get_Banner () [0x00000] in <filename unknown>:0
at MobclixText.MobclixTextViewController.ViewDidLoad () [0x00006] in /Users/Alex/Projects/MobclixText/MobclixText/MobclixTextViewController.cs:33
at (wrapper managed-to-native) MonoTouch.ObjCRuntime.Messaging:void_objc_msgSend (intptr,intptr)
at MonoTouch.UIKit.UIWindow.MakeKeyAndVisible () [0x00010] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIWindow.g.cs:98
at MobclixText.AppDelegate.FinishedLaunching (MonoTouch.UIKit.UIApplication app, MonoTouch.Foundation.NSDictionary options) [0x00031] in /Users/Alex/Projects/MobclixText/MobclixText/AppDelegate.cs:44
at (wrapper managed-to-native) MonoTouch.UIKit.UIApplication:UIApplicationMain (int,string[],intptr,intptr)
at MonoTouch.UIKit.UIApplication.Main (System.String[] args, System.String principalClassName, System.String delegateClassName) [0x0004c] in /Developer/MonoTouch/Source/monotouch/src/UIKit/UIApplication.cs:38
at MobclixText.Application.Main (System.String[] args) [0x00000] in /Users/Alex/Projects/MobclixText/MobclixText/Main.cs:17

我猜这是因为句柄为 0。

关于您的评论

Using dlsym with one of the special flags (man dlsym) might help.

你能给我一个如何使用它的例子吗=) ??

亚历克斯


编辑 2:

你好@Poupou,感谢你的回答,但是

IntPtr RTLD_MAIN_ONLY = (IntPtr) (-5);
IntPtr ptr = Dlfcn.GetIntPtr (RTLD_MAIN_ONLY, "kGADAdSizeBanner");

我仍然得到 ptr 等于 0 还有其他想法吗?

亚历克斯


编辑 3:

好的,我现在正在尝试以下操作

IntPtr RTLD_MAIN_ONLY = Dlfcn.dlopen (null, 0);
IntPtr ptr = Dlfcn.dlsym (RTLD_MAIN_ONLY, "kGADAdSizeBanner");
Console.WriteLine("RTLD_MAIN_ONLY: " + RTLD_MAIN_ONLY);
Console.WriteLine("ptr: " + ptr);

Dlfcn.dlopen (null, 0); 完成 here现在我得到一个句柄值 -2 但我猜 native 链接器现在正在删除该符号,我怎样才能防止这种情况发生?

非常感谢你的时间@Poupou

亚历克斯

最佳答案

所以我找到了一种获取值的方法。第一部分是说服链接器导出符号(不隐藏任何现有符号)。这可以通过创建(本地)符号的别名(默认情况下是全局的)来完成。例如

-gcc_flags="-Xlinker -alias -Xlinker _kGADAdSizeBanner -Xlinker _MTAdSizeBanner"

之后的代码:

IntPtr RTLD_MAIN_ONLY = Dlfcn.dlopen (null, 0);
IntPtr ptr = Dlfcn.dlsym (RTLD_MAIN_ONLY, "MTAdSizeBanner");
object obj2 = Marshal.PtrToStructure(ptr, typeof(GADAdSize));

将为您提供一个值为 320x50 的 GADAdSize 实例。

你需要重新导出你需要的每个符号(糟糕)但是应该可以将它包含在 [LinkWith] 属性中(iirc)所以它不是必需的对于最终用户。

关于c# - MonoTouch 绑定(bind)不能使用字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10525043/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com