473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

passing a vector of doubles

Hi

Kind of newbie question:

We have a DLL with a function with a prototype like

void mult( int mtxhandle, double* x, double* y, int size );

where "size" is the size of both x and y vectors. What should be the correct
sintax to define its respective DllImport wrapper in order to use it like:

Double x[] = new Double[mysize];
Double y[] = new Double[mysize];

mult( handle, x, y, mysize );

Thank you

Henrique
Nov 16 '05 #1
4 1402
Henrique,
What should be the correct
sintax to define its respective DllImport wrapper


static extern void mult(int mtxhandle, double[] x, double[] y, int
size);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2
Hi Mattias,

That does not work since the compiler complains with
C2146: syntax error : missing ')' before identifier 'x'

if you try
double x[] instead of double[] x
then you get
C2664: 'mult':cannot convert parameter 2 from 'double __gc[]' to ' double
[]'

I'm looking into
[DllImport("libbmc")]
extern "C" int mult( int handle, System::IntPtr x, System::IntPtr y, int
sign );

GCHandle xh = GCHandle::Alloc( x, GCHandleType::Pinned );
IntPtr xptr = Marshal::UnsafeAddrOfPinnedArrayElement( x, 0 );
(same for yh)

int result = mult( handle, xptr, yptr, sign );

xh.Free();
yh.Free();

but not sure if this is the right way to do it. Indeed, it looks like the
pair GCHandle::Alloc and GCHandle::Free will have a big impact on
performance.

Henrique

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:O7**************@TK2MSFTNGP11.phx.gbl...
Henrique,
What should be the correct
sintax to define its respective DllImport wrapper


static extern void mult(int mtxhandle, double[] x, double[] y, int
size);

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 16 '05 #3
Henrique,

Since the name of this group is
microsoft.public.dotnet.languages.csharp, I assumed you were coding in
C#. Now I see you are actually writing managed C++ code.

I believe the MC++ syntax would be

int mult( int handle, Double x[], Double y[], int sign );

or

int mult( int handle, double x __gc[], Double y __gc[], int sign );

But you may want to post your question in
microsoft.public.dotnet.languages.vc instead to get a definite answer.

I'm looking into
[DllImport("libbmc")]
extern "C" int mult( int handle, System::IntPtr x, System::IntPtr y, int
sign );

GCHandle xh = GCHandle::Alloc( x, GCHandleType::Pinned );
IntPtr xptr = Marshal::UnsafeAddrOfPinnedArrayElement( x, 0 );
(same for yh)

int result = mult( handle, xptr, yptr, sign );

xh.Free();
yh.Free();


Pinning can be easier than that C++. You should also be able to do

extern "C" int mult( int handle, double *x, double *y, int sign );

and then

Double __pin *xp = &x[0];
Double __pin *yp = &y[0];
int result = mult( handle, xp, yp, sign );

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4
So, there's no need for explicitly acquiring a handle to this memory?
The simplicity of the c# wrapping was simply astonishing. I was expecting
something a lot more complicated.... :)
that's good though
thanks a lot!
Henrique
Nov 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: harry | last post by:
I have 2 multi-dim arrays double subTotals = null; String rowTitles = null; I want to pass them to a function that initialises & populates them like so - loadData( rowTitles, subTotals);
8
by: Alex Vinokur | last post by:
Various forms of argument passing ================================= C/C++ Performance Tests ======================= Using C/C++ Program Perfometer...
5
by: {AGUT2} {H}-IWIK | last post by:
Hi, I'm trying to pass my vector to a function external to my main(), but my compiler is complaining. FYI, the struct for the facetInfo works perfectly, it's just the vector passing. A quick...
12
by: BCC | last post by:
If I create a vector of vectors of double: std::vector< std::vector<double> > table1; Are my vectors of doubles uninitialized? Do I have to loop through table1 and initialize each vector of...
8
by: Markus Dehmann | last post by:
I am wondering what is the "best" code to print out the elements of a vector. Assume they contain strings. With "best", I mean shortest and most readable at the same time. Of course, a for...
5
by: lugal | last post by:
This might be more appropriate here. I'm new to C++, coming from a background in another languages that allowed a similar solution to work (Python). I wrote the following code in C++ based on the...
5
by: Michael | last post by:
Hi, once I read here that it is not 'a good idea' to pass variables that are not initialized to a function. I have void something ( double *vector ); ....
2
by: fred_stevens | last post by:
Hi all you C boffins: I need to sort a vector of doubles is ascending order. Qsort will return the sorted vector, but I need a vector of the indices of the sorted vector, not the actual sorted...
14
by: ds | last post by:
Hi all, I have to pass an array of doubles to a legacy C function that copies some data using memcpy. The code would look like this: extern "C"{ void legacyCFunctionFill(void* arg); } .......
4
by: Anonymous | last post by:
I ahve a vector of doubles taht I need to extract values from. I was just about to use the STL find() algo, but I have a couple of questions: first: can you specify the tolerance threshold to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.