MonoTouch binding cannot use fields

Hi, I am trying to update google advertisements for V6, but they were not able to link the following and put it in a controlled world

I have the following structure

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

and I did it on the monotouch side

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

I have the following code

extern GADAdSize const kGADAdSizeBanner;
extern GADAdSize const kGADAdSizeMediumRectangle;

I cannot bind it using the [Field] attribute, since docs indicate that the Field attribute can only be used for

  • NSString Links
  • NSArray Links
  • 32-bit ints (System.Int32)
  • 32-bit floats (System.Single)
  • 64-bit floats (System.Double)

So, I tried the following two ways that I could come up with

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

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

and

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;
    }
}

And in both cases it causes a failure

Using DLLImport, I get a nullargument exception when I call Marshal.PtrToStructure (), and the second throws DLLNotFoundException System.ArgumentNullException


Edit:

@Poupou , System.ArgumentNullException 0 ptr 0

stacktrace:

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); -2, , , ?

@Poupou

+3
2

, . - ( ). , ( ) () . .

-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));

GADAdSize 320x50.

, (yuck), [LinkWith] (iirc), .

+4

- , . . OTOH iOS.

? . , [DllImport("__Internal") p/invoke.

, dldym ( ). man dlsym , . ( ) , :

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

1: Dlfcn.GetIntPtr - , dlsym;

2: :-) null ( IntPtr.Zero), SizeF.

+1

All Articles