Importing NSPerformService

IvanIcinIvanIcin USMember ✭✭
edited December 6 in Xamarin.Mac

I've tried to import NSPerformService and got this so far:
[System.Runtime.InteropServices.DllImport("System/Library/Frameworks/AppKit.framework/Versions/C/AppKit")]<br /> static extern bool NSPerformService(string item, NSPasteboard pboard);

Unfortunately, when I call it I get System.Runtime.InteropServices.MarshalDirectiveException:

Type AppKit.NSPasteboard which is passed to unmanaged code must have a StructLayout attribute.

I realize that it is something about marshaling, so that probably NSPasteboard should be sent like IntPtr or something else, but I have no idea on how to do that (this thread contains the solution when this happens on the return value, but it can't be applied when the problem is in the argument: https://forums.xamarin.com/discussion/57248/type-foundation-nsdictionary-which-is-passed-to-unmanaged-code-must-have-a-structlayout-attribute).

I guess the answer would help to other people that might have similar problem in the future as there aren't too many threads regarding this and definitely not about this specific issue.

Best Answer

  • IvanIcinIvanIcin USMember ✭✭
    Accepted Answer

    OK, got this, both arguments should be IntPtr when importing and then there should be one wrapper method that passes handle of NSString and NSPasteboard.

Answers

  • IvanIcinIvanIcin USMember ✭✭
    Accepted Answer

    OK, got this, both arguments should be IntPtr when importing and then there should be one wrapper method that passes handle of NSString and NSPasteboard.

  • ChrisHamonsChrisHamons USXamarin Team Xamurai

    Yep, you have to be careful when invoking into native code to get the signature correct, else things can go rather poorly.

    Glad you figured it out!

  • IvanIcinIvanIcin USMember ✭✭

    @ChrisHamons , thanks, I'm glad you would help if I haven't figured it out!

Sign In or Register to comment.