9 messages in net.java.dev.jna.usersRe: [jna-users] Wrapping a C api to c...
FromSent OnAttachments
Richard OteroOct 12, 2007 3:59 pm 
Albert StrasheimOct 12, 2007 4:39 pm 
Albert StrasheimOct 12, 2007 5:51 pm 
Albert StrasheimOct 12, 2007 6:00 pm 
Timothy WallOct 13, 2007 6:05 am 
Timothy WallOct 13, 2007 7:05 am 
Timothy WallOct 13, 2007 7:27 am 
Albert StrasheimOct 13, 2007 10:55 am 
Richard OteroOct 14, 2007 2:50 pm 
Actions with this message:
Paste this link in email or IM:
Paste this link in email or IM:
Atom feed for this thread
Paste this URL into your reader:
Subject:Re: [jna-users] Wrapping a C api to control Matlab using Java (with example)Actions...
From:Albert Strasheim (full@gmail.com)
Date:Oct 12, 2007 4:39:14 pm
List:net.java.dev.jna.users

Hello

----- Original Message ----- From: "Richard Otero" <rich@gmail.com> To: <use@jna.dev.java.net> Sent: Saturday, October 13, 2007 12:59 AM Subject: [jna-users] Wrapping a C api to control Matlab using Java (with example)

Thank you for your work with simplifying the native interface!

I am trying to create a Java API for Matlab by calling the current C API using JNA.

Brilliant! I'm definitely going to borrow this idea of yours!

This is the lions share of the API that I will need to wrap:

Engine - A handle to a MATLAB engine object. int engClose(Engine *ep); int engEvalString(Engine *ep,const char *string); int engGetVisible(Engine *ep, bool *value); Engine *engOpen(const char *startcmd); Engine *engOpenSingleUse(const char *startcmd, void *dcom, int *retstatus); int engOutputBuffer(Engine *ep, char *p, int n); int engSetVisible(Engine *ep, bool value);

These functions should be easy to map. I'm guessing one doesn't care what goes on inside an Engine, so you can map the Engine pointers to JNA's Pointer.

retstatus maps to IntByreference. char pointers probably map to String. That's about it.

mxArray - The fundamental type underlying MATLAB data.

Looking at extern/include/matrix.h, I think you'll have to map that mxArray_tag structure as a JNA Structure. You'll have to do some thorough tests to make sure JNA lines up the fields in memory the way the compiler does. Hopefully it will just work, but mxArray_tag is a pretty scary struct as C structs go.

mxArray *engGetVariable(Engine *ep, const char *name); int engPutVariable(Engine *ep, const char *name, const mxArray *pm);

An example of using this would be: main() { Engine * ep; char * buffer;

ep = engOpen("matlab "); buffer = mxCalloc(101,sizeof(char)); engOutputBuffer(ep, buffer, 100); if (engEvalString(ep,"d=eye(3)")) fprintf(stderr,"Error evaluating d=eye(3)"); else printf("Result of engEvalString is \n%s\n",buffer); mxFree(buffer); }

Given the signatures for these functions, is there any advice on using JNA that comes to mind? This will be my first go at calling a native interface so a nudge in the right direction would be most appreciated.

I hope I could get you started in the right direction. If you get something working, it would be great if you could send it to the mailing list. I'll do the same from my side (hopefully I'll get around to attempting this in the next few days).

Regards,

Albert