Services & Resources / Wolfram Forums / MathGroup Archive
-----

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: [mg86863] Re: Mathlink: How do I pass arbitrary data from Mathematica to C?
  • From: Jens-Peer Kuska <kuska at informatik.uni-leipzig.de>
  • Date: Mon, 24 Mar 2008 01:43:58 -0500 (EST)
  • References: <fs4rmb$p3s$1@smc.vnet.net>

Hi,

a) MathLink is a protocol, i.e., it *must* transmit data
b) MathLink is always buffered, i.e., you will *never*
    get the true address of a pointer in the Mathematica kernel
c) only the kernel developer know how a data structure is organized
    in the kernel, i.e., a array of integers *may* be a memory block
    of successive integers but it can be also an array of pointers
    to integers
d) for a general expression you have to build your own tree like
    structure of the Mathematica expression by setting the argument
    type to Manual

Regards
   Jens


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.
> 


  • Prev by Date: Re: Re: Re: Re: smallest
  • Next by Date: Re: Bug: symbol recreates itself suddenly
  • Previous by thread: 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?