Re: NetLink with float* as argument
- To: mathgroup at smc.vnet.net
- Subject: [mg71830] Re: [mg71800] NetLink with float* as argument
- From: Todd Gayley <tgayley at wolfram.com>
- Date: Thu, 30 Nov 2006 06:05:38 -0500 (EST)
At 01:56 AM 11/29/2006, lamoucheg at gmail.com wrote: >Hi, > my collegue wrote a dll (written in C++) to provide tools to read >a custom data file format. > >With NETLink I can use most of his functions (opening a file, closing >a file, reading some simple specific data), but I have trouble with >the following. > >He has a function like: > > long ReadPatch (float *Buf,long PosX, long PosY, long SizeX, long >SizeY, long Channel); > >It waits for a Buf pointer with space allocated for an array of float, >reads the data in a file and put it where Buf points to. To be >precise, the array in fact does not need to be initialized, but space >must be reserved. > >After reading the documentation in the help file, my naive attempt at >using the function is: > >readPatch = > DefineDLLFunction["ReadPatch", > "TheNeeded.dll", > "long", {"float*", "long", "long", "long", "long", "long"}] > >Then, I define: >AA = Table[0., {5}, {5}] > >to reserve space. > >Then when I try the followint, hoping that AA upon return contains the >read data, > >readPatch[AA, 0, 0, 5, 5, 0] > >I get the message >NET::methodargs: Improper arguments supplied for method named >readPatch. > >I tried various things, looked around and cannot figure out how >achieve what I want. Is it related to the fact that I need a pointer >to a float array and that internally AA is an array of double? > >Note that I don't have any doubt about the efficiency of my >colleague's DLL, since it works perfectly whenused with Labview. > >Thanks in advance for any input. Guy, The reason you are getting the "improper arguments" message is that AA is a 2-D array, and the function prototype (float*) expects a flat array. Even though the C code might treat this buffer space as a multidimensional array, it is just a flat block of memory. If you flatten AA to be just Table[0., {25}], the function call will now succeed, but it still won't do what you want. Although a block of memory was indeed created in a native buffer and filled by the DLL function, you have no way to get the data back into Mathematica. As discussed in the docs, to get values back out you have to create a .NET array object of the appropriate size and pass that object into the DLL function. That array object will have its contents modified, and afterward you can use NETObjectToExpresion to get the values as a Mathematica list. buf = NETNew["System.Single[]", 25] readPatch[buf, 0, 0, 5, 5, 0] Partition[NETObjectToExpression[buf], 5] Todd Gayley Wolfram Research