Call Objective C code from .NET / Mono(Mac) - mono

I'm porting a .NET-based test automation framework to Mac OS X.
I have significant .NET experience, however, I'm totally a rookie in the Mac world.
I need to call Objective C code from .NET / Mono. I started with the "MonoMac - Binding Objective-C Types" tutorial.
I created an API definition file. I couldn't compile it, because the type "BaseTypeAttribute" couldn't be found. Later I found that this is not a problem, because the btouch tool will compile it (basetypeattribute-not-found-in-a-binding-project).
Since I'm developing for OS X (not iOS), I was not using btouch, I was going to use the bmac tool. Findig that wasn't trivial, but I managed to compile&found it based on this article. Good.
I was not able to compile my binding file, because the bmac said "Type or namespace 'Tasks' does not exists in namespace 'System.Threading'".
Nevertheless, I specified an output folder for the generated .cs files, fixed the compile error (the Tasks wasn't really needed, the error was caused by an unnecessary using that was generated).
So I'm about at the point where I could have been if I followed the manual "Binding New Objective-C Types / Binding_Details" document.
The problem is that I still cannot use my Objective C library.
I can compile and run the C# code with MonoDevelop/Xamarin, but it fails with the message:
System.Exception: Could not create an native instance of the type 'My.Type': the native class hasn't been loaded.
It is possible to ignore this condition by setting MonoMac.ObjCRuntime.Class.ThrowOnInitFailure to false.
at MonoMac.Foundation.NSObject.InitializeObject (Boolean alloced)
at MonoMac.Foundation.NSObject..ctor (MonoMac.Foundation.NSObjectFlag x)
at My.Type..ctor ()
I found an article (coreplot-monomac-bindings-crashing) that says the library must be manually loaded before using it, but I had no luck with it. "Dlfcn.dlopen" always return IntPtr.Zero.
I'm tried to create my lib as "Cocoa Framework", "Cocoa Library" and "Bundle" in XCode. Which one should I use?
Are there any low-level debugging possibilites in Mono(Mac)?
Did anybody managed to do this? (calling objc from .net)
thanks in advance,
-g

Related

How to run the sha2.c of polarssl

I was trying to use the sha2.c file from polarssl at this link,
https://polarssl.org/sha-256-source-code
I am actually quite a newbie to this, but I was able to get this on Eclipse and when I tried to build it, it gives the error
c:/mingw/x86_64-w64-mingw32/lib/../lib/libmingw32.a(lib64_libmingw32_a-crt0_c.o):crt0_c.c:(.text+0x3d): undefined reference to `WinMain'
do I have to pass some kind of data in the arguments? how can I find out how to use it?
The problem is not in the source file you downloaded, but the fact that you need to make 'an application'. Eclipse cannot compile 'just some functionality' unless you instruct it to build a library. You will have to provide a WinMain / main function so that Windows knows what to do when you start the application.
That is what the compiler is complaining about, there is no main() functions it can compile into the application!
Easiest way to start is to start a Generic C Application in Eclipse and then add this sha2 source file and header to that project. The Generic C application project already has a main function you can work from..

Objective-C: How to make a Framework?

I'm new to Objective-C but has a lot experience with Java and .NET.
I'm trying to add EGOPhotoViewer to my iOS 5 project in Xcode 4.2.1. But I get a lot of release, dealloc, retain etc. issues while compiling their code, since I'm using Automatic Reference Counting (I think!).
How can I create a class library, framework or what it is called in Objective C for their code, that I can add to my project?
EDIT:
I've done the approach from JeremyP by inserting the code with a new target. I compiled in the beginning, but after a while I get this compile error:
Undefined symbols for architecture i386:
"_OBJC_METACLASS_$_EGOPhotoViewController", referenced from:
_OBJC_METACLASS_$_PhotoViewController in PhotoViewController.o
"_OBJC_CLASS_$_EGOPhotoViewController", referenced from:
_OBJC_CLASS_$_PhotoViewController in PhotoViewController.o
ld: symbol(s) not found for architecture i386
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Does anybody know why?
For your purpose, a regular static library as described by beryllium would do fine.
Just for unashamed plug purposes and for spreading the word, this document describes how to create versioned, documented frameworks using Xcode, GIT and DoxyGen.
Creating a Versioned Framework 1.23
The main purpose of creating such frameworks is to redistribute them. I personally find it extremely annoying to manually include libraries and headers I receive from third parties - especially if the libraries are delivered in separate versions for simulator and device. That guide is meant for classic middleware developers. I have written it to allow people like those folks from Google Analytics to finally provide something worth their brand.
This document gives you a step by step explanation, bundled with loads of screenshots.
Open Xcode -> File -> New -> New Project -> Framework & Library -> Next -> Type Name, Choose Folder -> Create
It will be a library called yourApp.a. You can find it in Derived Data folder
You can't create frameworks for iOS. You can however, create static libraries using beryllium's technique. You can also add a static library to your existing project using File / New / New Target... Obviously, once you create the target you can change the Objective-C automatic reference counting build setting to "no" for your new target.
I thought it was possible to turn ARC on and off at the source file level, but I can't figure out how.
I've created frameworks on multiple occasions using the following method:
http://db-in.com/blog/2011/07/universal-framework-iphone-ios-2-0/

how to import COM dll in D

I'm trying to create an D application which uses a (third party) COM .dll so I can scrape a text box of another application so I can sound an error when a certain string shows up.
However the third party doesn't provide .lib, .def or .h files that go with the dll (atleast with the free trial version). I can create the .lib file with the implib tool but I don't see any of the library's functions in the created .lib.
Their (visual c++) samples use the #import directive to link it in however that is of no use for me ...
On a side note how can I get the proper interfaces (in a .di with boilerplate that does the linking) of the dll automatically? I ask so the correctness of the linkage doesn't depend on my (likely to be incorrect) translation of the functions. They do have a webpage which gives all functions but the object model is a bit chaotic to say the least.
From what I know, COM libraries only expose a few functions, required to (un)register the library and to create objects.
You can however view the interfaces and functions in a COM .dll using the OLE/COM Object Viewer. It seems it might be able to output header files (.h). Afterwards, maybe you could use htod as a starting point to converting everything to D interfaces.
The DMD distribution seems to include a .COM sample (chello.d, dclient.d, dserver.d), and at first glance it doesn't look like it would require any LIBs explicitly.
Unfortunately, I've never actually used COM in D, so I can't advise any further. I hope this helps in some way.
While I have yet to actually do COM work myself, I am trying to revive Juno over on Github/he-the-great. Part of the project is tlbimpd which is what will output a D file from a DLL.
I've tested the examples and successfully run tlbimpd. Please do try things out for your use and submit any issues.

Calling MATLAB Engine from Cocoa Application

I'm writing a Cocoa application and I'm trying to link it with the MATLAB Engine to call MATLAB functions. So far I've added the .app/extern/include/ directory (the one that contains the engine.h header) to the header search paths (and subsequently #imported engine.h) and added the .app/extern/lib/maci64 directory to the library search paths (though that doesn't really do anything). I've been looking through the MATLAB documentation and it looks like MATLAB has its own compiler 'mex' for MATLAB engine applications… but clearly that doesn't work for a Cocoa app (and anyway, on my system, the 'mex' command starts PDFTex and has nothing to do with MATLAB). Also, the engine libraries in that directory are in an odd format (.map) which seems to be a debugging symbol file and not a normal Mac library (dylib, a, framework, etc.). Thoughts?

VB.Net Visual Studio Error When Showing Form

I get the following exception when showing a form:
InvalidOperationException was unhandled
Mixed mode assembly is build against version 'v2.0.50727' of the runtime and cannot be loaded in the 4.0 runtime without additional configuration information.
alt text http://img69.imageshack.us/img69/2599/captureya.png
Dont really know why this isnt working. Any help?
I haven't seen the code for LoginForm. But I think you need to set an app.config flag to fall back to .NET 2.0 era bindings...
<startup useLegacyV2RuntimeActivationPolicy="true">
<supportedRuntime version="v4.0"/>
</startup>
First, what's a mixed mode assembly? A
mixed mode assembly is an assembly
that contains both managed (CIL) and
unmanaged (machine language) code.
Consequently, they are not portable to
other CPU instruction sets, just like
normal C and C++ programs and
libraries.
Next, why use them? The primary
purpose for mixed mode assemblies is
as "glue", to e.g. use a C++ library
class as a base class of a managed
class. This allows the managed class
to extend unmanaged methods, allowing
the managed code to be polymorphic
with respect to existing unmanaged
functions. This is extremely useful in
many contexts. However, as something
like this involves extending a C++
class, it requires that the compiler
know all about the C++ compiler ABI
(name mangling, virtual function table
generation and placement, exception
behavior), and thus effectively
requires native code. If the base
class is within a separate .dll, this
will also require that the mixed mode
assembly list the native .dll as a
dependency, so that the native library
is also loaded when the assembly is
loaded.
The other thing that mixed mode
assemblies support is the ability to
export new C functions so that other
programs can LoadLibrary() the
assembly and GetProcAddress the
exported C function.
Both of these capabilities require
that the shared library loader for the
platform support Portable Executable
(PE) files, as assemblies are PE
files. If the shared library loader
supports PE files, then the loader can
ensure that when the assembly is
loaded, all listed dependent libraries
are also loaded (case 1), or that
native apps will be able to load the
assembly as if it were a native DLL
and resolve DLL entry points against
it.
Source
I had this issue, tried the answer above and it did not work.
After much reading and trial and error and not finding anything that worked I noticed that I had both imported the Mysql dlls and added them in properties. After I removed the import statements it worked.
I know it was removing the import statements because I tested between every change I tried.
Hope that helps somebody.