MathGroup Archive 2008

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Mathlink: How do I pass arbitrary data from Mathematica to C?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86908] Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
  • From: Karen Bindash <KarenBindash at googlemail.com>
  • Date: Wed, 26 Mar 2008 04:48:58 -0500 (EST)
  • References: <fs4rmb$p3s$1@smc.vnet.net> <fsa5b1$a9u$1@smc.vnet.net>

On 25 Mar, 06:16, David Bailey <dave at Remove_Thisdbailey.co.uk> wrote:
> 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/SettingUpExternalFu...
>
> > 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 Baileyhttp://www.dbaileyconsultancy.co.uk

The data will be passed to some hardware via a library routine written
in C.

For example, the first bit of data might bea command like "RESET", the
next might be "LOADFILE" The next bit of data might be a binary file.
Hence I need to find a way of passing an arbitrary collection of
bytes. It might be one byte, two bytes, three bytes ... up to a couple
MB or so. I will know the exact length.

It it was a file I wanted to send, then I assume  something like


data=BinaryReadList["filenane.dat",Character8]

would be suitable to read it, then I would need to send the contents
of 'data'

But equally the data might just be a bit of text.






  • Prev by Date: Re: Intersection of 2D Surfaces in 3D
  • Next by Date: Re: Saving Packages
  • 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?