hxcpp - Create library for other applications

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

hxcpp - Create library for other applications

danielku15
Hi everybody.

I just added some new targets (neko and c++) to my project in order to reach a wider customership. In my project I generate a SVG file and generate binary Midi data into a Array<Int> object.

As a first step I created a CLI - Tool which takes some parameter and generates the desired output into a file. Everything works fine. But now I want to provide a library (dll/so) to use in my C# Desktop and Java Android Applications.

Now to the problems:
 
1. Building a DLL/Lib for Native Invokes, not an Exe.

I found this post but I didn't get how to easily compile a dynamic library. There seems to be a flag for static libraries but for dynamic there's a need for complex build files?


2. Clean API for Native Invokes

Is there a common way how to provide a clean API for pinvoke/JNI Mappings? For pinvokes I know there's a need for plain C functions wrapping the C++ operations. Has anyone encountered this problem and knows how to easily make haXe stuff callable from C# and Java. Especially the memory handling could become a problem. (I'm no C++ expert at all).

I'd like to create an API like that (C# sample):
string svg = MyLibraryWrapper.loadSvg("MyTestfile.bin");
byte[] midiData = MyLibraryWrapper.loadMidi("MyTestfile.bin");
FileMeta fileInfo = MyLibraryWrapper.loadMeta("MyTestfile.bin"); // struct FileMeta { string A; string B; .. }



It would be great if you can give me some hints how to create such a library from my haXe project.

[offtopic]

It seems that hxcpp has some problems with generating optional parameters. If you use optional parameters the code cannot be compiled because the parameters are declared twice.

And another problem appears when using inline methods.
public inline function getSubClass() : SubClass { return cast this.superClass; }
The C++ code will call this method but it does not get generated.

Are those problems known?


Cheers
Daniel
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp - Create library for other applications

Cauê W.
wow, just read your topic daniel !  :) 

I've just released yesterday a lib that might help you with native invokes (as I've just posted) - http://code.google.com/p/hxffi/

1. Building a DLL/Lib for Native Invokes, not an Exe.

You can do that on cpp Build.xml, by specifying at the <target > tag the attribute toolid="${ndll-tool}"

2. Clean API for Native Invokes

This is where libffi comes in ; )

Right now there isn't any docs (i just made the bindings yesterday), but you can look at some examples from the little test suite I made. But you'll run into problem with structs, but there is this lib http://code.google.com/p/structs/ we're working on to take care of that. The integration should be painless, but I haven't tried yet.  

--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp - Create library for other applications

Cauê W.
oh never mind, I understand now that you're talking about calling hxcpp functions from C#/Java.
It should be possible, but you'll have to code your own wrappers. Basically you'll need to declare the function as public for the dll (hand coded cpp), and declare it as extern "C"

2011/10/24 Cauê Waneck <[hidden email]>
wow, just read your topic daniel !  :) 

I've just released yesterday a lib that might help you with native invokes (as I've just posted) - http://code.google.com/p/hxffi/

1. Building a DLL/Lib for Native Invokes, not an Exe.

You can do that on cpp Build.xml, by specifying at the <target > tag the attribute toolid="${ndll-tool}"

2. Clean API for Native Invokes

This is where libffi comes in ; )

Right now there isn't any docs (i just made the bindings yesterday), but you can look at some examples from the little test suite I made. But you'll run into problem with structs, but there is this lib http://code.google.com/p/structs/ we're working on to take care of that. The integration should be painless, but I haven't tried yet.  


--
haXe - an open source web programming language
http://haxe.org
Reply | Threaded
Open this post in threaded view
|

Re: hxcpp - Create library for other applications

danielku15
Quite funny that you just published this lib and today I such a usecase.

Thanks for the lib, I'll take a look at all the stuff in order to get a clean and memory safe API.

I also thought of some other solutions till your answered. My plan: I wanted to implement some C functions on my own which use the generated hxcpp source and fill the required data into a datastructure at the heap. Then I return a pointer to the pinvoke/jni implementation where I marshal the data to the managed environment. Afterwards I call another function to release the memory.

In C# the wrapper code will look like like this:
IntPtr data = MyLibraryWrapper.readFileData("MyFile.bin");
// do some marshaling into C# structures
MyLibraryWrapper.freeFileData(data);

But mixing up the generated and own code is quite dangeours (in my opinion). I think the best solution is to compile into a static library and use this static library in a C/C++ project to compile a dynamic library:

1. haXe Source + haXe Compiler = C++ source & static Library
2. static Library + Custom C/C++ source + C/C++ compiler/linker = DLL/so
3. DLL/so + C#/Java Wrapper = Ready to use framework.

The file info I need to pass trough contains some small strings(meta data), a long string(svg data) and a byte array (midi data). On the first look it seems such dynamic stuff is not possible using "hxffi" and "structs".

I hope I get this running. It's already the 3rd way I try to create C#/Java applications using my haXe source:

1. Wait for C#/Java Target
2. Print C#/Java source using macros (waiting for Issue #519 to get closed)
3. Use native invokes (this one)