Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

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

  • To: mathgroup at smc.vnet.net
  • Subject: [mg86837] Mathlink: How do I pass arbitrary data from Mathematica to C?
  • From: Karen Bindash <KarenBindash at googlemail.com>
  • Date: Sun, 23 Mar 2008 01:00:49 -0500 (EST)

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.


  • Prev by Date: Re: Problem involving NDSolve
  • Next by Date: Strange behaviour of DialogInput and DialogCreate in 6.0.1
  • Previous by thread: Packages without packages
  • Next by thread: Re: Mathlink: How do I pass arbitrary data from Mathematica to C?