[Date Index]
[Thread Index]
[Author Index]
Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
*To*: mathgroup at smc.vnet.net
*Subject*: [mg86877] Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
*From*: David Bailey <dave at Remove_Thisdbailey.co.uk>
*Date*: Tue, 25 Mar 2008 01:14:28 -0500 (EST)
*References*: <fs4rmb$p3s$1@smc.vnet.net>
Karen Bindash wrote:
> I am trying to write some code to interface Mathematica to a closed-
> source library written in C. I'm using Mathlink for this, which is
> typically used to interface Mathematica to external functions - see
> for example
>
> http://reference.wolfram.com/mathematica/tutorial/SettingUpExternalFunctionsToBeCalledFromMathematica.html
>
> For example, if the library has a function 'f' with the following
> prototype:
>
> int f(int x, int y);
>
> the following in a Mathlink template (.tm file) will allow me to call
> this function from Mathematica by the name Foo, if I link to the
> library., create an
> executable, then install that executable in Mathematica with
> Install[].
>
> :Begin:
> :Function: f
> :Pattern: Foo[x_Integer, y_Integer]
> :Arguments: {x, y}
> :ArgumentTypes: {Integer, Integer}
> :ReturnType: Integer
> :End:
>
> (There is no need for me to write the function "f", as that has been
> done. I just need to link to the library containing the function f, in
> much the same way you use the 'pow' function in C without writing it -
> you would just link to the maths library.).
>
> I have no problem with the above - it all works as expected.
>
> The problem occurs with a function 'g', which instead of having only
> integer arguments, takes a pointer to some data. The format of the
> data is not specified - the C function just needs to know where in
> memory the data is, and how many bytes there are. The C prototype is
>
> int g(const void *data, long nbytes);
>
> Does anyone know how I can write a Mathematica template so when I link
> with the function 'g', I can pass the data properly? Note, that since
> the library is closed-source, I am not able to change the calling
> method in any way. But I expect it should be possible to write an
> interface in C, such that data is passed from Mathematica in a form
> compatible with Mathematica, and then converts it to a from the C
> library accepts. But I am stuck as how to do this.
>
> If its not possible to do this with a template, can it be done by
> writing it all in C? If so, how?
>
> I don't want Mathematica to try to interpret the data in any way -
> just to pass an address of where the data is in memory, and also the
> number of bytes of data. Than the library function g will return an
> integer, which I want to pass back to Mathematica.
>
> Someone suggested that I might look at the C code generated by mcc to
> work out how to do this, (perhaps making use of MLPutInteger), but I
> can't work out how to do this.
>
> Does that make sense? Any ideas how to pass arbitrary data to a C
> program?
>
> I have a similar issue in trying to get arbitrary data from C to
> Mathematica, but that is another story.
>
When you pass data over MathLink, you are communicating between two
processes - so no pointers can be passed. It certainly sounds as though
you need to write an interface routine in C - as you suggest. Presumably
you know the structure of this data, so perhaps you could transmit it
from Mathematica in pieces and assemble it in the required format in the
interface routine.
Perhaps you should tell us the structure of this data, and how big it is.
David Bailey
http://www.dbaileyconsultancy.co.uk
Prev by Date:
**Re: ListPlot Joined/Filling bug?**
Next by Date:
**Re: finding positions of elements in a list**
Previous by thread:
**Re: Mathlink: How do I pass arbitrary data from Mathematica to C?**
Next by thread:
**Re: Mathlink: How do I pass arbitrary data from Mathematica to C?**
| |