【发布时间】:2012-05-09 22:32:16
【问题描述】:
您好,我正在尝试将 google admboads 更新到 V6,但在绑定以下内容并将其公开给托管世界时遇到了一些麻烦
我有以下结构
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] 属性绑定它,因为文档指定 Field 属性只能用于
- NSString 引用
- NSArray 引用
- 32 位整数 (System.Int32)
- 32 位浮点数(System.Single)
- 64 位浮点数 (System.Double)
所以我尝试了以下两种我能想到的方法
[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。
关于你的评论
将 dlsym 与特殊标志之一 (man dlsym) 一起使用可能会有所帮助。
你能给我一个如何使用它的例子吗=) ??
亚历克斯
编辑 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 但我猜本机链接器现在正在删除符号,我怎样才能防止这种情况发生??
非常感谢您抽出宝贵时间@Poupou
亚历克斯
【问题讨论】:
-
DLLNotFoundException ?我很惊讶,
handle的值是多少?和堆栈跟踪?由于该库已被静态链接,因此将找不到它。将dlsym与特殊标志之一 (man dlsym) 一起使用可能会有所帮助。 -
@poupou 请看我的更新,希望你能帮助我也感谢你的信息。
标签: c# ios xamarin.ios pinvoke