使用MonoMac将自定义本机MacOS X库转换为dll [英] Converting custom native MacOS X library to dll using MonoMac

查看:115
本文介绍了使用MonoMac将自定义本机MacOS X库转换为dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用本机界面(.xibs)将Windows Forms应用程序移植到MacOSX.该应用程序需要访问具有MacOS X驱动程序的硬件设备,但我想保留已为Windows版本编写的C#逻辑,以便将来更轻松地更新应用程序.我创建了一个小的Xcode项目,以测试如何访问此设备,并使它工作,然后将其滚动到.framework中,并尝试在MonoMac项目中使用它.事实证明,这有点困难.

I am trying to port a Windows Forms application to MacOS X, using a native interface (.xibs). This app needs to access a hardware device that has MacOS X drivers but I wanted to keep the C# logic already written for the Windows version so it would be easier to update the application in the future. I've created a small Xcode project to test how to access this device and I got it to work, then I rolled it into a .framework and tried to use this in a MonoMac Project. This proved a bit more difficult.

我主要按照以下教程来弄清楚应采取的步骤,我还复制了示例代码以确保不会遇到各种依赖关系问题,因为我创建的.framework是一个稍微复杂一点,并按照所有步骤创建一个名为iSimple的i386框架(巧合的是,我也需要用这种方法来编译我的驱动程序,因为它已经很旧了...).

I've mainly followed the following tutorial to figure out which steps to take when, I also copied the example code to be sure I don't run into all kinds of dependency problems because the .framework I've created is a bit more complicated and followed all steps to create a i386 framework (coincidentally I need to compile my driver this way too because it's quite old...) called Simple:

http://brendanzagaeski.appspot.com/xamarin/0002.html

这是一个非常简单的类,看起来像这样:

It's a very simple class that looks like this:

@implementation Simple
- (NSString *)run:(BOOL *)successful
{
    *successful = YES;
    return @"OK";
}
@end

我一直遵循所有步骤,直到详细说明如何使用Sharpie的步骤为止,因为已更改为具有不同参数的仅命令行工具.我最终使用以下命令生成了.cs文件:

I've followed all steps without any problem up til the step detailing how to use sharpie, because this has changed to a command-line only tool with different parameters. I ended up generating the .cs files using the following command instead:

sharpie bind -sdk macosx10.10 -framework Simple.framework -classic -v -c -arch i386

ApiDefinitions.cs看起来像这样:

ApiDefinitions.cs looks like this:

using MonoMac.Foundation;

//[Verify (ConstantsInterfaceAssociation)]
partial interface Constants
{
    // extern double SimpleVersionNumber;
    [Field ("SimpleVersionNumber")]
    double SimpleVersionNumber { get; }

    // extern const unsigned char [] SimpleVersionString;
    [Field ("SimpleVersionString")]
    byte[] SimpleVersionString { get; }
}

// @interface Simple : NSObject
[BaseType (typeof(NSObject))]
interface Simple
{
    // -(NSString *)run:(BOOL *)successful;
    [Export ("run:")]
    unsafe string Run (bool* successful);
}

-classic开关立即为我提供了正确的使用(MonoMac.Foundation)的权限,因此我相信了更多,但是我也尝试过不这样做.除使用外,其他输出相同.

The -classic switch gave me the correct using (MonoMac.Foundation) immediately so I trusted that more, but I've tried also without. The output except for using was identical otherwise.

我已注释掉验证"部分,因为该部分无法编译,这是对使用Sharpie的人的警告,它可能需要验证某些内容.根据教程,我还更改了bool *到ref bool的成功.

I've commented out the Verify part since this doesn't compile and is meant as a warning to people using sharpie that something might need to be verified. I've also changed bool* successful to ref bool successful according to the tutorial.

然后我需要bmac.该工具仅存在于Xamarin.Mac中,如果要在MacMono上使用它,则首先需要手动编译MacMono并使用生成的bmac.exe代替.本教程使用以下命令:

Then I needed bmac. This tool only exists in Xamarin.Mac and if you want to use it on MacMono you first need to compile MacMono manually and use the generated bmac.exe instead. The tutorial used the following command:

/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/usr/bin/bmac -oSimple.dll --tmpdir=/tmp -baselib=/Library/Frameworks/Xamarin.Mac.framework/Versions/Current/usr/lib/mono/XamMac.dll -r=System.Drawing SimpleBindingDefinition.cs

我已将其更改为以下命令:

I've changed this to the following command:

mono /Users/Name/Code/monomac/monomac/src/bmac.exe -o Simple.dll --tmpdir=/tmp -baselib=/Users/Name/Code/monomac/monomac/src/MonoMac.dll -r=System.Drawing ApiDefinitions.cs

这产生了以下错误:

   in Method: System.String Run(Boolean*)
error BI0000: Unexpected error - Please file a bug report at http://bugzilla.xamarin.com
// and more

足够公平,本教程已经警告了该布尔引用,所以我将其更改为:

Fair enough, the tutorial already warned about that boolean ref so I changed it to:

unsafe string Run (ref bool successful);

现在我留下了与布尔值完全相同的错误栏:

Now I was left with exactly the same error bar the part about the boolean:

error BI0000: Unexpected error - Please file a bug report at http://bugzilla.xamarin.com
System.NullReferenceException: Object reference not set to an instance of an object
  at Generator.Generate (System.Type type) [0x00000] in <filename unknown>:0 
  at Generator.Go () [0x00000] in <filename unknown>:0 
  at BindingTouch.Main2 (System.String[] args) [0x00000] in <filename unknown>:0 
  at BindingTouch.Main (System.String[] args) [0x00000] in <filename unknown>:0 

因此,可以确定它会尝试处理run:方法,并在某个地方绊倒,但是我不确定实际问题从哪里开始. Sharpie转换是否已经错误或者是我更改ApiDefinitions或调用bmac.exe的方式?

So it's sure that it tries to process the method run: and stumbles somewhere, but I am not sure where the actual problem starts. Is the sharpie conversion wrong already or is it the way I changed ApiDefinitions or invoke bmac.exe?

还尝试创建没有bool ref参数的新框架版本,并删除了这两个版本方法.那什么都没改变.

Also tried to create a new framework version without bool ref parameter and removed those two version methods. That didn't change anything.

我正在尝试找出在适当位置编译时是否是编译错误. 经过测试:此框架可以在常规Xcode项目中顺利运行

I am trying to figure out if perhaps it's a compile error when it's compiled in place. Tested: This framework works without a hitch in a regular Xcode project

推荐答案

bmac 现在可以构建良好的DLL.在初始化此库的任何实例之前,只是缺少了MonoMac应用程序中的加载框架.

bmac builds good DLL now. Just missing loading framework in the MonoMac application before init any instance of this library.

if (Dlfcn.dlopen("PathToFrameworksFolder/Simple.framework/Simple", 0) == IntPtr.Zero)
{
Console.Error.WriteLine("Unable to load the dynamic library.");
}

只需返回至: http://brendanzagaeski.appspot.com/xamarin/0002.html 并查看此博客的底部, MainClass类

Just go back to: http://brendanzagaeski.appspot.com/xamarin/0002.html and see the bottom of this blog, code of class MainClass

这篇关于使用MonoMac将自定义本机MacOS X库转换为dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆